Subversion Repositories SmartDukaan

Rev

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