Subversion Repositories SmartDukaan

Rev

Rev 202 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
172 ashish 1
# -*- coding: utf-8 -*-
2
"""Main Controller"""
3
 
4
from tg import expose, flash, require, url, request, redirect
5
from pylons.i18n import ugettext as _, lazy_ugettext as l_
6
 
7
from wiki20.lib.base import BaseController
8
from wiki20.model import DBSession, metadata
9
from wiki20.controllers.error import ErrorController
10
from wiki20.processors.RequestProcessors import process_gaurav
11
from datastore.DataAccessor import DataHelper
12
from xml.etree.ElementTree import *
13
import urllib
14
 
15
 
16
 
17
__all__ = ['RootController']
18
 
19
 
20
class RootController(BaseController):
21
    """
22
    The root controller for the Wiki-20 application.
23
 
24
    All the other controllers and WSGI applications should be mounted on this
25
    controller. For example::
26
 
27
        panel = ControlPanelController()
28
        another_app = AnotherWSGIApplication()
29
 
30
    Keep in mind that WSGI applications shouldn't be mounted directly: They
31
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
32
 
33
    """
34
 
35
    error = ErrorController()
36
 
37
    @expose('wiki20.templates.index')
38
    def index(self):
39
        """Handle the front-page."""
40
        return dict(page='index')
41
 
42
    @expose('wiki20.templates.about')
43
    def about(self):
44
        """Handle the 'about' page."""
45
        return dict(page='about')
46
 
47
    @expose('wiki20.templates.gaurav')
48
    def gaurav(self, **data):
49
        """ Handle gaurav url"""
50
        print data
51
        ret_val = process_gaurav(data)
52
        return ret_val
53
 
54
 
55
    @expose('wiki20.templates.compare')
56
    def compare(self):
57
        """ Handle compare url"""
58
        str1 = ""
59
        da = DataHelper()
60
        models = da.get_all_models()
61
        ret_val = {}
62
        for m in models:
63
            name = m.model
64
            str1 = str1 + "<option value="
65
            #str1 = str1 + "/'"
66
            str1 = str1 + unicode(name, "iso-8859-1")
67
            #str1 = str1 + "/'"
68
            str1 = str1 + ">"
69
            str1 = str1 + unicode(name, "iso-8859-1")
70
            str1 = str1 + "</option>"
71
 
72
        #print data
73
        #ret_val = process_compare(data)
74
        ret_val['val1'] = str1
75
        return ret_val
76
 
77
    @expose('wiki20.templates.compare_model')
78
    def compare_model(self,**data):
79
        model = data['model']
80
        da = DataHelper()
81
        mid = da.get_modbyModel(model).id
82
        models_list = da.get_prbyMid(mid)
83
        sorted(models_list, key=lambda prices: prices.supplier_id)
84
        leng = len(models_list)
85
        print "length is :"
86
        print leng
87
        price_list = []
88
        vendor_list = []
89
        while leng>0:
90
            price_list.append(0)
91
             #['infibeam','indiaplaza','mobilestore','univercell']
92
            leng = leng -1
93
        max = 0
94
        index = 0
95
        for m in models_list:
96
            price_list[index] = m.final_price
97
            index = index + 1
98
            s_info = ""
99
            s_info = s_info + str(da.get_supp_byId(m.supplier_id).name) 
100
            if str(m.extra_info) != "":
101
                s_info = s_info + " with " 
102
                s_info = s_info + str(m.extra_info)
103
 
104
            vendor_list.append(s_info)
105
            if(m.final_price > max):
106
                max = m.final_price
107
        range = 0
108
        if max<3000:
109
            range = 3000 
110
        elif max>=3000 and max<5000:
111
            range = 5000   
112
        elif max>=5000 and max<7000:
113
            range = 7000   
114
        elif max>=7000 and max<10000:
115
            range = 10000   
116
        elif max>=10000 and max<15000:
117
            range = 15000   
118
        elif max>=15000 and max<20000:
119
            range = 20000   
120
        elif max>=20000 and max<25000:
121
            range = 25000   
122
        elif max>=25000 and max<30000:
123
            range = 30000   
124
        elif max>=30000 and max<50000:
125
            range = 50000   
126
        str3 = ""
127
        for p in price_list:
128
            str3 = str3 + str(p) 
129
            str3 = str3 + ","
130
        str3 = str3[0:len(str3)-1]
131
        str4 = ""
132
        for v in vendor_list:
133
            str4 = str4 + v 
134
            str4 = str4 + "|"
135
        str4 = str4[0:len(str4)-1]
136
 
137
        ret_val = {}
138
        ret_val['val1'] = str3
139
        ret_val['val2'] = str4
140
        #ret_val['val1'] = price_list
141
        #ret_val['val2'] = vendor_list
142
        str1 = ""
143
        models = da.get_all_models()
144
        for m in models:
145
            name = m.model
146
            str1 = str1 + "<option value="
147
            #str1 = str1 + "/'"
148
            str1 = str1 + unicode(name, "iso-8859-1")
149
            #str1 = str1 + "/'"
150
            if str(name) == str(model):
151
                str1 = str1 + " selected=yes"
152
            str1 = str1 + ">"
153
            str1 = str1 + unicode(name, "iso-8859-1")
154
            str1 = str1 + "</option>"
155
        ret_val['val3'] = str1
156
        ret_val['val4'] = range
157
        str2 = ""
158
        str2 = str2 + '<table align = "right"><tr><th>' 
159
        str2 = str2 + "vendor"
160
        str2 = str2 + "</th><th>"
161
        str2 = str2 + "price"
162
        str2 = str2 + "</th></tr>"
163
        leng = len(vendor_list)
164
        j = 0
165
        while(j<leng):
166
            str2 = str2 + "<tr><td>"
167
            str2 = str2 + str(vendor_list[j]) 
168
            str2 = str2 + "</td><td>"
169
            str2 = str2 + str(price_list[j])
170
            str2 = str2 + "</td></tr>"
171
            j = j+1
172
        str2 = str2 + "</table>"
173
        ret_val['val5'] = str2    
174
        return ret_val
175
 
176
 
177
    @expose(template="wiki20.templates.model" , content_type='text/xml')
178
    def model(self, **data):   
179
        #print " I m in "
180
        id = int(data['id'])
181
        str1 = brandname = modelname =  extra_i = sup_name = sup_site = sup_lastcrawled = ""
182
        quoted_p = final_p = 0
183
        da = DataHelper()
184
        mo = da.get_modbyId(id)
185
        brandname = mo.brand
186
        modelname = mo.model
187
        prlist = da.get_prbyMid(id)
188
        #print len(prlist)
189
 
190
        str1 = str1 + "<Mobile><mid>" 
191
        str1 = str1 + str(id)
192
        str1 = str1 + "</mid><barandname>" 
193
        str1 = str1 + unicode(brandname, "iso-8859-1")           
194
        str1 = str1 + "</barandname><modelname>" 
195
        str1 = str1 + unicode(modelname, "iso-8859-1")
196
        str1 = str1 + "</modelname>"
197
        sorted(prlist, key=lambda prices: prices.supplier_id)
198
        prev_sid = -1    
199
        ct = 0
200
        for pr in prlist:
201
            quoted_p = pr.quoted_price
202
            final_p = pr.final_price
203
            extra_i = pr.extra_info
204
            pid = pr.id
205
            sid = pr.supplier_id
206
            su = da.get_supp_byId(sid)
207
            sup_name = su.name
208
            sup_site = su.site
209
            sup_lastcrawled = su.last_crawled
210
            '''
211
            print brandname
212
            print modelname 
213
            print quoted_p
214
            print final_p 
215
            print extra_i 
216
            print sup_name 
217
            print sup_site
218
            print sup_lastcrawled
219
            '''
220
 
221
            if sid != prev_sid:
222
                if ct != 0:
223
                     str1 = str1 + "</Supplier>";
224
                str1 = str1 + "<Supplier>";
225
                str1 = str1 + "<name>" 
226
                str1 = str1 + unicode(sup_name, "iso-8859-1") 
227
                str1 = str1 + "</name><sid>" 
228
                str1 = str1 + str(sid)
229
                str1 = str1 + "</sid><site>" 
230
                str1 = str1 +  unicode(sup_site, "iso-8859-1")
231
                str1 = str1 + "</site><last_updated>"
232
                str1 = str1 + str(sup_lastcrawled) 
233
                str1 = str1 + "</last_updated>"
234
                prev_sid = sid
235
            ct = ct+1    
236
            str1 = str1 + "<Price><pid>"
237
            str1 = str1 + str(pid) 
238
            str1 = str1 + "</pid><quotedprice>"
239
            str1 = str1 + str(quoted_p) 
240
            str1 = str1 + "</quotedprice><finalprice>"
241
            str1 = str1 + str(final_p) 
242
            str1 = str1 + "</finalprice><extrainfo>"
243
            str1 = str1 + unicode(extra_i, "iso-8859-1") 
244
            str1 = str1 + "</extrainfo></Price>" 
245
            #str1 = str1 + "</Supplier>"
246
            #root = Element(str1)
247
            #xml_elem = xml.etree.ElementTree.fromstring(str1)
248
        str1 = str1 + "</Supplier>";
249
        str1 = str1 + "</Mobile>"
250
        ret_val = {}
251
        ret_val['val1'] = urllib.unquote(str1)#root
252
        #tree = ElementTree(root)
253
        #tree.write("/home/gaurav/code/TG2Test/src/wiki20/templates/model.html")
254
        print str1 
255
        return ret_val
256
 
257
    @expose(template="wiki20.templates.modelIds" , content_type='text/xml')
258
    def mobileIds(self):   
259
        da = DataHelper()
260
        mobiles = da.get_all_models()
261
        str1 = ""
262
        for m in mobiles:
263
            id = m.id
264
            model = m.model
265
            brand = m.brand
266
            str1 = str1 + "<Mobile><id>"
267
            str1 = str1 + str(id)
268
            str1 = str1 + "</id><model>"
269
            str1 = str1 + unicode(model, "iso-8859-1")
270
            str1 = str1 + "</model><vendor>"
271
            str1 = str1 + unicode(brand, "iso-8859-1")
272
            str1 = str1 + "</vendor></Mobile>"
273
        ret_val = {}
274
        print str1
275
        ret_val['val1'] = urllib.unquote(str1)#root
276
        return ret_val