Subversion Repositories SmartDukaan

Rev

Rev 19654 | Rev 19678 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 19654 Rev 19666
Line 1... Line 1...
1
from bson import json_util
1
from bson import json_util
2
from bson.json_util import dumps
2
from bson.json_util import dumps
3
from datetime import datetime, timedelta
3
from datetime import datetime, timedelta, date
4
from pyshorteners.shorteners  import Shortener
4
from pyshorteners.shorteners  import Shortener
5
from dtr import main
5
from dtr import main
6
from dtr.config import PythonPropertyReader
6
from dtr.config import PythonPropertyReader
7
from dtr.storage import Mongo
7
from dtr.storage import Mongo
8
from dtr.storage.DataService import Retailers, Users, CallHistory, RetryConfig, \
8
from dtr.storage.DataService import Retailers, Users, CallHistory, RetryConfig, \
9
    RetailerLinks, Activation_Codes, Agents, Agent_Roles, AgentLoginTimings, \
9
    RetailerLinks, Activation_Codes, Agents, Agent_Roles, AgentLoginTimings, \
10
    FetchDataHistory, RetailerContacts, Orders, OnboardedRetailerChecklists,\
10
    FetchDataHistory, RetailerContacts, Orders, OnboardedRetailerChecklists,\
11
    RetailerAddresses, Pincodeavailability, app_offers, appmasters, user_app_cashbacks, user_app_installs,\
11
    RetailerAddresses, Pincodeavailability, app_offers, appmasters, user_app_cashbacks, user_app_installs,\
12
    Postoffices, UserCrmCallingData, CallHistoryCrm, ProductPricingInputs
12
    Postoffices, UserCrmCallingData, CallHistoryCrm, ProductPricingInputs,\
-
 
13
    tinxys_stats
13
from dtr.storage.Mongo import get_mongo_connection
14
from dtr.storage.Mongo import get_mongo_connection
14
from dtr.storage.Mysql import fetchResult
15
from dtr.storage.Mysql import fetchResult
15
from dtr.utils import DealSheet as X_DealSheet, \
16
from dtr.utils import DealSheet as X_DealSheet, \
16
    UserSpecificDeals, utils, ThriftUtils
17
    UserSpecificDeals, utils, ThriftUtils
17
from dtr.utils.utils import getLogger,encryptMessage,decryptMessage,\
18
from dtr.utils.utils import getLogger,encryptMessage,decryptMessage,\
Line 1563... Line 1564...
1563
        result = getTinInfo(tin)
1564
        result = getTinInfo(tin)
1564
        resp.body = json.dumps(result, encoding='utf-8')
1565
        resp.body = json.dumps(result, encoding='utf-8')
1565
        
1566
        
1566
 
1567
 
1567
def getTinInfo(tin):
1568
def getTinInfo(tin):
1568
    tinInfo = {"isError":False}
-
 
1569
    try:
1569
    try:
-
 
1570
        curData = session.query(tinxys_stats).filter_by(req_date=datetime.date.today()).first()
-
 
1571
        if not curData:
-
 
1572
            curData = tinxys_stats()
-
 
1573
        curData.total += 1
-
 
1574
        tinInfo = {"isError":False}
-
 
1575
        try:
1570
        params = ['tin','cst','counter_name','counter_address','state','pan','registered_on','cst_status','valid_since']
1576
            params = ['tin','cst','counter_name','counter_address','state','pan','registered_on','cst_status','valid_since']
1571
        url  = "http://www.tinxsys.com/TinxsysInternetWeb/dealerControllerServlet?tinNumber=" + tin + "&searchBy=TIN"
1577
            url  = "http://www.tinxsys.com/TinxsysInternetWeb/dealerControllerServlet?tinNumber=" + tin + "&searchBy=TIN"
1572
        req = urllib2.Request(url)
1578
            req = urllib2.Request(url)
1573
        try:
1579
            try:
1574
            connection = urllib2.urlopen(req, timeout=10)
1580
                connection = urllib2.urlopen(req, timeout=10)
-
 
1581
            except:
-
 
1582
                curData.request_failed += 1
-
 
1583
                tinInfo = {"isError":True,
-
 
1584
                        "errorMsg": "Some problem occurred. Try again after some time"
-
 
1585
                }
-
 
1586
                return tinInfo
-
 
1587
            pageHtml = connection.read()
-
 
1588
            connection.close()        
-
 
1589
            doc = pq(pageHtml)
-
 
1590
            index=-1
-
 
1591
            for ele in pq(doc.find('table')[2])('tr td:nth-child(2)'):
-
 
1592
                index += 1
-
 
1593
                text = pq(ele).text().strip()
-
 
1594
                if not text:
-
 
1595
                    text = pq(ele)('div').text().strip()
-
 
1596
                tinInfo[params[index]] = text
-
 
1597
            print index, "index"
-
 
1598
            if index != 8:
-
 
1599
                raise
-
 
1600
            if not get_mongo_connection().Dtr.tin.find({"tin":tinInfo['tin']}):
-
 
1601
                get_mongo_connection().Dtr.tin.insert(tinInfo)
-
 
1602
            address = tinInfo['counter_address']
-
 
1603
            m = re.match(".*?(\d{6}).*?", address)
-
 
1604
            if m:
-
 
1605
                tinInfo['pin'] = m.group(1)
-
 
1606
                tinInfo['pin_required'] = False
-
 
1607
            else:
-
 
1608
                tinInfo['pin_required'] = True
-
 
1609
    
-
 
1610
            print "TinNumber: Successfully fetched details", tin
1575
        except:
1611
        except:
-
 
1612
            curData.invalid_tin = 1
-
 
1613
            traceback.print_exc()
1576
            tinInfo = {"isError":True,
1614
            tinInfo = {"isError":True,
1577
                    "errorMsg": "Some problem occurred. Try again after some time"
1615
                        "errorMsg": "Could not find tin"
1578
            }
1616
            }
1579
            return tinInfo
-
 
1580
        pageHtml = connection.read()
-
 
1581
        connection.close()        
-
 
1582
        doc = pq(pageHtml)
-
 
1583
        index=-1
-
 
1584
        for ele in pq(doc.find('table')[2])('tr td:nth-child(2)'):
1617
            print "TinNumber: Problem fetching details", tin
1585
            index += 1
-
 
1586
            text = pq(ele).text().strip()
-
 
1587
            if not text:
1618
        session.commit()
1588
                text = pq(ele)('div').text().strip()
-
 
1589
            tinInfo[params[index]] = text
-
 
1590
        print index, "index"
1619
        return tinInfo
1591
        if index != 8:
-
 
1592
            raise
-
 
1593
        if not get_mongo_connection().Dtr.tin.find({"tin":tinInfo['tin']}):
-
 
1594
            get_mongo_connection().Dtr.tin.insert(tinInfo)
-
 
1595
        address = tinInfo['counter_address']
-
 
1596
        m = re.match(".*?(\d{6}).*?", address)
-
 
1597
        if m:
-
 
1598
            tinInfo['pin'] = m.group(1)
-
 
1599
            tinInfo['pin_required'] = False
-
 
1600
        else:
1620
    finally:
1601
            tinInfo['pin_required'] = True
-
 
1602
 
-
 
1603
        print "TinNumber: Successfully fetched details", tin
-
 
1604
    except:
-
 
1605
        traceback.print_exc()
1621
        session.close()
1606
        tinInfo = {"isError":True,
-
 
1607
                    "errorMsg": "Could not find tin"
-
 
1608
        }
-
 
1609
        print "TinNumber: Problem fetching details", tin
-
 
1610
    return tinInfo
-
 
1611
        
1622
        
1612
        
1623
        
1613
class UpdateUser:
1624
class UpdateUser:
1614
    def on_post(self,req,resp):
1625
    def on_post(self,req,resp):
1615
        try:
1626
        try: