Subversion Repositories SmartDukaan

Rev

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

Rev 15123 Rev 15132
Line 2... Line 2...
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
4
from dtr import main
4
from dtr import main
5
from dtr.config import PythonPropertyReader
5
from dtr.config import PythonPropertyReader
6
from dtr.storage import Mongo
6
from dtr.storage import Mongo
7
from dtr.storage.DataService import Retailers, Users, CallHistory, RetryConfig
7
from dtr.storage.DataService import Retailers, Users, CallHistory, RetryConfig, \
-
 
8
    RetailerLinks
8
from dtr.utils import FetchLivePrices, DealSheet as X_DealSheet, \
9
from dtr.utils import FetchLivePrices, DealSheet as X_DealSheet, \
9
    UserSpecificDeals
10
    UserSpecificDeals
10
from elixir import *
11
from elixir import *
11
from sqlalchemy.sql.expression import or_
12
from sqlalchemy.sql.expression import or_
-
 
13
from urllib import urlencode
-
 
14
import contextlib
12
import falcon
15
import falcon
13
import json
16
import json
14
import traceback
17
import traceback
-
 
18
import urllib
-
 
19
import urllib2
-
 
20
import uuid
15
global RETAILER_DETAIL_CALL_COUNTER
21
global RETAILER_DETAIL_CALL_COUNTER
16
RETAILER_DETAIL_CALL_COUNTER = 0
22
RETAILER_DETAIL_CALL_COUNTER = 0
17
 
23
 
18
DEALER_RETRY_FACTOR = int(PythonPropertyReader.getConfig('DEALER_RETRY_FACTOR'))
24
DEALER_RETRY_FACTOR = int(PythonPropertyReader.getConfig('DEALER_RETRY_FACTOR'))
19
class CategoryDiscountInfo(object):
25
class CategoryDiscountInfo(object):
Line 545... Line 551...
545
            print traceback.print_exc()
551
            print traceback.print_exc()
546
        finally:
552
        finally:
547
            session.close()
553
            session.close()
548
        return retailer
554
        return retailer
549
    
555
    
550
    def on_get(self, req, resp, agentId, callType):
556
    def on_get(self, req, resp, agentId, callType=None, retailerId=None):
551
        global RETAILER_DETAIL_CALL_COUNTER
557
        global RETAILER_DETAIL_CALL_COUNTER
552
        RETAILER_DETAIL_CALL_COUNTER += 1
558
        RETAILER_DETAIL_CALL_COUNTER += 1
553
        print "RETAILER_DETAIL_CALL_COUNTER", RETAILER_DETAIL_CALL_COUNTER
559
        print "RETAILER_DETAIL_CALL_COUNTER", RETAILER_DETAIL_CALL_COUNTER
554
        print "I am here"
560
        print "I am here"
555
        self.agentId = int(agentId)
561
        self.agentId = int(agentId)
556
        self.callType = callType
562
        self.callType = callType
-
 
563
        if callType is None:
-
 
564
            retailerLink = session.query(RetailerLinks).filter_by(id=retailerId)
-
 
565
            if retailerLink is not None:
-
 
566
                code = retailerLink.code
-
 
567
            else: 
-
 
568
                code = self.getCode()
-
 
569
                retailerLink = RetailerLinks()
-
 
570
                retailerLink.code = code
-
 
571
                retailerLink.is_activated = False
-
 
572
                retailerLink.retailer_id = retailerId
-
 
573
                session.commit()
-
 
574
            session.close()
-
 
575
            resp.body =  json.dumps({"result":{"code":code}}, encoding='utf-8')
-
 
576
            return 
557
        retryFlag = False 
577
        retryFlag = False 
558
        agentId = int(agentId)
578
        agentId = int(agentId)
559
        if RETAILER_DETAIL_CALL_COUNTER % DEALER_RETRY_FACTOR ==0:
579
        if RETAILER_DETAIL_CALL_COUNTER % DEALER_RETRY_FACTOR ==0:
560
            retryFlag=True
580
            retryFlag=True
561
        
581
        
Line 563... Line 583...
563
            retailer = self.getRetryRetailer()
583
            retailer = self.getRetryRetailer()
564
        else:
584
        else:
565
            retailer = self.getNewRetailer()
585
            retailer = self.getNewRetailer()
566
        
586
        
567
        if retailer is None:
587
        if retailer is None:
568
            resp.body = "{\"result\":\"{}\"}"
588
            resp.body = "{\"result\":{}}"
569
        else:
589
        else:
570
            resp.body = json.dumps(todict(getRetailerObj(retailer)), encoding='utf-8')
590
            resp.body = json.dumps(todict(getRetailerObj(retailer)), encoding='utf-8')
571
        
591
        
572
    def on_post(self, req, resp, agentId, callType):
592
    def on_post(self, req, resp, agentId, callType):
573
        returned = False
593
        returned = False
Line 633... Line 653...
633
                self.callHistory.disposition_description = 'NA'
653
                self.callHistory.disposition_description = 'NA'
634
            self.callHistory.disposition_description = 'Reason Retailer Not Interested ' + self.callHistory.disposition_description        
654
            self.callHistory.disposition_description = 'Reason Retailer Not Interested ' + self.callHistory.disposition_description        
635
        session.commit()
655
        session.commit()
636
        return True   
656
        return True   
637
        
657
        
-
 
658
    def getCode(self,):
-
 
659
        prefix = None
-
 
660
        notFound = True
-
 
661
        while notFound:
-
 
662
            code = uuid.uuid4().hex[:10] if prefix is None else prefix + uuid.uuid4().hex[:6]
-
 
663
            notFound = '0' in code and session.query(RetailerLinks).filter_by(code=code).first() is not None
638
    
664
        return code
639
    
665
    
640
    def callLater(self,):
666
    def callLater(self,):
641
        self.retailer.status = 'retry' if self.callType == 'fresh' else 'fretry'
667
        self.retailer.status = 'retry' if self.callType == 'fresh' else 'fretry'
642
        self.retailer.call_priority = None
668
        self.retailer.call_priority = None
643
        if self.callDisposition == 'call_later':
669
        if self.callDisposition == 'call_later':
Line 714... Line 740...
714
    obj.address = retailer.address
740
    obj.address = retailer.address
715
    obj.id = retailer.id
741
    obj.id = retailer.id
716
    obj.scheduled = (retailer.call_priority is not None)
742
    obj.scheduled = (retailer.call_priority is not None)
717
    return obj
743
    return obj
718
 
744
 
-
 
745
def make_tiny(code):
-
 
746
    url = 'https://play.google.com/store/apps/details?id=com.saholic.profittill&referrer=utm_source%3D0%26utm_medium%3DCRM%26utm_term%3D001%26utm_campaign%3D' + code
-
 
747
    request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url}))
-
 
748
    filehandle = urllib2.Request(request_url)
-
 
749
    x= urllib2.urlopen(filehandle)
-
 
750
    return str(x.read())
-
 
751
    
719
    
752
    
720
class Mock(object):
753
class Mock(object):
721
    pass
754
    pass
722
    
755
    
723
def main():
756
def main():
724
    ret = RetailerDetail()
-
 
725
    resp = Mock()
-
 
726
    ret.on_get(None, resp, 1, 'fresh')
757
    print make_tiny("http://www.google.com")
727
 
758
 
728
if __name__ == '__main__':
759
if __name__ == '__main__':
729
        main()
760
        main()
730
        
761
        
731
762