Subversion Repositories SmartDukaan

Rev

Rev 172 | Rev 241 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

# -*- coding: utf-8 -*-
"""Main Controller"""

from tg import expose, flash, require, url, request, redirect
from pylons.i18n import ugettext as _, lazy_ugettext as l_

from wiki20.lib.base import BaseController
from wiki20.model import DBSession, metadata
from wiki20.controllers.error import ErrorController
from wiki20.processors.RequestProcessors import process_gaurav
from datastore.DataAccessor import DataHelper
from datastore.DataCodeAccessor import *
from xml.etree.ElementTree import *
import urllib
from tg import tmpl_context
from wiki20.widgets.infibeam_form import create_infibeam_form



__all__ = ['RootController']


class RootController(BaseController):
    """
    The root controller for the Wiki-20 application.
    
    All the other controllers and WSGI applications should be mounted on this
    controller. For example::
    
        panel = ControlPanelController()
        another_app = AnotherWSGIApplication()
    
    Keep in mind that WSGI applications shouldn't be mounted directly: They
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
    
    """
    
    error = ErrorController()

    @expose('wiki20.templates.index')
    def index(self):
        """Handle the front-page."""
        return dict(page='index')

    @expose('wiki20.templates.about')
    def about(self):
        """Handle the 'about' page."""
        return dict(page='about')
    
    @expose('wiki20.templates.gaurav')
    def gaurav(self, **data):
        """ Handle gaurav url"""
        print data
        ret_val = process_gaurav(data)
        return ret_val
    
    
    @expose('wiki20.templates.compare')
    def compare(self):
        """ Handle compare url"""
        str1 = ""
        da = DataHelper()
        models = da.get_all_models()
        ret_val = {}
        for m in models:
            name = m.model
            str1 = str1 + "<option value="
            #str1 = str1 + "/'"
            str1 = str1 + unicode(name, "iso-8859-1")
            #str1 = str1 + "/'"
            str1 = str1 + ">"
            str1 = str1 + unicode(name, "iso-8859-1")
            str1 = str1 + "</option>"
            
        #print data
        #ret_val = process_compare(data)
        ret_val['val1'] = str1
        return ret_val
    
    @expose('wiki20.templates.compare_model')
    def compare_model(self,**data):
        model = data['model']
        da = DataHelper()
        mid = da.get_modbyModel(model).id
        models_list = da.get_prbyMid(mid)
        sorted(models_list, key=lambda prices: prices.supplier_id)
        leng = len(models_list)
        print "length is :"
        print leng
        price_list = []
        vendor_list = []
        while leng>0:
            price_list.append(0)
             #['infibeam','indiaplaza','mobilestore','univercell']
            leng = leng -1
        max = 0
        index = 0
        for m in models_list:
            price_list[index] = m.final_price
            index = index + 1
            s_info = ""
            s_info = s_info + str(da.get_supp_byId(m.supplier_id).name) 
            if str(m.extra_info) != "":
                s_info = s_info + " with " 
                s_info = s_info + str(m.extra_info)
            
            vendor_list.append(s_info)
            if(m.final_price > max):
                max = m.final_price
        range = 0
        if max<3000:
            range = 3000 
        elif max>=3000 and max<5000:
            range = 5000   
        elif max>=5000 and max<7000:
            range = 7000   
        elif max>=7000 and max<10000:
            range = 10000   
        elif max>=10000 and max<15000:
            range = 15000   
        elif max>=15000 and max<20000:
            range = 20000   
        elif max>=20000 and max<25000:
            range = 25000   
        elif max>=25000 and max<30000:
            range = 30000   
        elif max>=30000 and max<50000:
            range = 50000   
        str3 = ""
        for p in price_list:
            str3 = str3 + str(p) 
            str3 = str3 + ","
        str3 = str3[0:len(str3)-1]
        str4 = ""
        for v in vendor_list:
            str4 = str4 + v 
            str4 = str4 + "|"
        str4 = str4[0:len(str4)-1]
                         
        ret_val = {}
        ret_val['val1'] = str3
        ret_val['val2'] = str4
        #ret_val['val1'] = price_list
        #ret_val['val2'] = vendor_list
        str1 = ""
        models = da.get_all_models()
        for m in models:
            name = m.model
            str1 = str1 + "<option value="
            #str1 = str1 + "/'"
            str1 = str1 + unicode(name, "iso-8859-1")
            #str1 = str1 + "/'"
            if str(name) == str(model):
                str1 = str1 + " selected=yes"
            str1 = str1 + ">"
            str1 = str1 + unicode(name, "iso-8859-1")
            str1 = str1 + "</option>"
        ret_val['val3'] = str1
        ret_val['val4'] = range
        str2 = ""
        str2 = str2 + '<table align = "right"><tr><th>' 
        str2 = str2 + "vendor"
        str2 = str2 + "</th><th>"
        str2 = str2 + "price"
        str2 = str2 + "</th></tr>"
        leng = len(vendor_list)
        j = 0
        while(j<leng):
            str2 = str2 + "<tr><td>"
            str2 = str2 + str(vendor_list[j]) 
            str2 = str2 + "</td><td>"
            str2 = str2 + str(price_list[j])
            str2 = str2 + "</td></tr>"
            j = j+1
        str2 = str2 + "</table>"
        ret_val['val5'] = str2    
        return ret_val
    
            
    @expose(template="wiki20.templates.model" , content_type='text/xml')
    def model(self, **data):   
        #print " I m in "
        id = int(data['id'])
        str1 = brandname = modelname =  extra_i = sup_name = sup_site = sup_lastcrawled = ""
        quoted_p = final_p = 0
        da = DataHelper()
        mo = da.get_modbyId(id)
        brandname = mo.brand
        modelname = mo.model
        prlist = da.get_prbyMid(id)
        #print len(prlist)
        
        str1 = str1 + "<Mobile><mid>" 
        str1 = str1 + str(id)
        str1 = str1 + "</mid><barandname>" 
        str1 = str1 + unicode(brandname, "iso-8859-1")           
        str1 = str1 + "</barandname><modelname>" 
        str1 = str1 + unicode(modelname, "iso-8859-1")
        str1 = str1 + "</modelname>"
        sorted(prlist, key=lambda prices: prices.supplier_id)
        prev_sid = -1    
        ct = 0
        for pr in prlist:
            quoted_p = pr.quoted_price
            final_p = pr.final_price
            extra_i = pr.extra_info
            pid = pr.id
            sid = pr.supplier_id
            su = da.get_supp_byId(sid)
            sup_name = su.name
            sup_site = su.site
            sup_lastcrawled = su.last_crawled
            '''
            print brandname
            print modelname 
            print quoted_p
            print final_p 
            print extra_i 
            print sup_name 
            print sup_site
            print sup_lastcrawled
            '''
            
            if sid != prev_sid:
                if ct != 0:
                     str1 = str1 + "</Supplier>";
                str1 = str1 + "<Supplier>";
                str1 = str1 + "<name>" 
                str1 = str1 + unicode(sup_name, "iso-8859-1") 
                str1 = str1 + "</name><sid>" 
                str1 = str1 + str(sid)
                str1 = str1 + "</sid><site>" 
                str1 = str1 +  unicode(sup_site, "iso-8859-1")
                str1 = str1 + "</site><last_updated>"
                str1 = str1 + str(sup_lastcrawled) 
                str1 = str1 + "</last_updated>"
                prev_sid = sid
            ct = ct+1    
            str1 = str1 + "<Price><pid>"
            str1 = str1 + str(pid) 
            str1 = str1 + "</pid><quotedprice>"
            str1 = str1 + str(quoted_p) 
            str1 = str1 + "</quotedprice><finalprice>"
            str1 = str1 + str(final_p) 
            str1 = str1 + "</finalprice><extrainfo>"
            str1 = str1 + unicode(extra_i, "iso-8859-1") 
            str1 = str1 + "</extrainfo></Price>" 
            #str1 = str1 + "</Supplier>"
            #root = Element(str1)
            #xml_elem = xml.etree.ElementTree.fromstring(str1)
        str1 = str1 + "</Supplier>";
        str1 = str1 + "</Mobile>"
        ret_val = {}
        ret_val['val1'] = urllib.unquote(str1)#root
        #tree = ElementTree(root)
        #tree.write("/home/gaurav/code/TG2Test/src/wiki20/templates/model.html")
        print str1 
        return ret_val
    
    @expose(template="wiki20.templates.modelIds" , content_type='text/xml')
    def mobileIds(self):   
        da = DataHelper()
        mobiles = da.get_all_models()
        str1 = ""
        for m in mobiles:
            id = m.id
            model = m.model
            brand = m.brand
            str1 = str1 + "<Mobile><id>"
            str1 = str1 + str(id)
            str1 = str1 + "</id><model>"
            str1 = str1 + unicode(model, "iso-8859-1")
            str1 = str1 + "</model><vendor>"
            str1 = str1 + unicode(brand, "iso-8859-1")
            str1 = str1 + "</vendor></Mobile>"
        ret_val = {}
        print str1
        ret_val['val1'] = urllib.unquote(str1)#root
        return ret_val
    
    @expose('wiki20.templates.infibeam_form')
    def form_infibeam(self, **kw):
    #"""Show form to add data record for infibeam."""
        tmpl_context.form = create_infibeam_form
        return dict(modelname='code_words',
        page='Infibeam Data')

    @expose()
    def save_infibeam(self, **kw):
        """Create a infibeam object and save it to the database."""
        initialize_table()
        set_code_word("INFIBEAM_DOMAINNAME",kw['INFIBEAM_DOMAINNAME'],"")  
        print kw['INFIBEAM_DOMAINNAME']
        set_code_word("INFIBEAM_NO",kw['INFIBEAM_NO'],"")
        print kw['INFIBEAM_NO']
        set_code_word("INFIBEAM_CT",kw['INFIBEAM_CT'],"")
        set_code_word("INFIBEAM_URL",kw['INFIBEAM_URL'],"")
        set_code_word("INFIBEAM_HOMEPAGE",kw['INFIBEAM_HOMEPAGE'],"")
        set_code_word("INFIBEAM_REFERER",kw['INFIBEAM_REFERER'],"")
        set_code_word("INFIBEAM_VATPLUSTAX",kw['INFIBEAM_VATPLUSTAX'],"")
        set_code_word("INFIBEAM_REMOVELIST",kw['INFIBEAM_REMOVELIST'],"")
        set_code_word("INFIBEAM_XPATH1",kw['INFIBEAM_XPATH1'],"")
        set_code_word("INFIBEAM_XPATH2",kw['INFIBEAM_XPATH2'],"")
        set_code_word("INFIBEAM_XPATH3",kw['INFIBEAM_XPATH3'],"")
        flash("Data successfully saved.")
        redirect("form_infibeam")