Subversion Repositories SmartDukaan

Rev

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

Rev 3217 Rev 3218
Line 4... Line 4...
4
@author: rajveer
4
@author: rajveer
5
'''
5
'''
6
 
6
 
7
from shop2020.logistics.service.impl import DataService
7
from shop2020.logistics.service.impl import DataService
8
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
8
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
9
     DeliveryEstimate, WarehouseAllocation, DestinationProviderAllocation,\
9
     DeliveryEstimate, WarehouseAllocation, \
10
    PublicHolidays, ServiceableLocationDetails
10
    PublicHolidays, ServiceableLocationDetails
11
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
11
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
12
    DeliveryType
12
    DeliveryType
13
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
13
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
14
from elixir import session
14
from elixir import session
15
import datetime, time
15
import datetime, time
16
import sys
16
import sys
17
from shop2020.utils.caching.SimpleCaching import memoized
-
 
18
 
17
 
19
warehouse_allocation_cache = {}
18
warehouse_allocation_cache = {}
20
serviceable_location_cache = {}
19
serviceable_location_cache = {}
21
delivery_estimate_cache = {}
20
delivery_estimate_cache = {}
22
 
21
 
-
 
22
'''
-
 
23
This class is for only data transfer. Never used outside this module.
-
 
24
'''
-
 
25
class _DeliveryEstimateObject:
-
 
26
    def __init__(self, delivery_time, reliability, provider_id, warehouse_location):
-
 
27
        self.delivery_time = delivery_time
-
 
28
        self.reliability = reliability
-
 
29
        self.provider_id = provider_id
-
 
30
        self.warehouse_location = warehouse_location 
-
 
31
    
23
def initialize(dbname="logistics", db_hostname="localhost"):
32
def initialize(dbname="logistics", db_hostname="localhost"):
24
    log_entry("initialize@DataAccessor", "Initializing data service")
33
    log_entry("initialize@DataAccessor", "Initializing data service")
25
    DataService.initialize(dbname, db_hostname)
34
    DataService.initialize(dbname, db_hostname)
26
    print "Starting cache population at: " + datetime.datetime.now()
35
    print "Starting cache population at: " + str(datetime.datetime.now())
27
    __cache_warehouse_allocation_table()
36
    __cache_warehouse_allocation_table()
28
    __cache_serviceable_location_details_table()
37
    __cache_serviceable_location_details_table()
29
    __cache_delivery_estimate_table()
38
    __cache_delivery_estimate_table()
30
    close_session()
39
    close_session()
31
    print "Done cache population at: " + datetime.datetime.now()
40
    print "Done cache population at: " + str(datetime.datetime.now())
32
 
41
 
33
def __cache_delivery_estimate_table():
42
def __cache_delivery_estimate_table():
34
    delivery_estimates = DeliveryEstimate.query.all()
43
    delivery_estimates = DeliveryEstimate.query.all()
35
    for delivery_estimate in delivery_estimates:
44
    for delivery_estimate in delivery_estimates:
36
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
45
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
Line 61... Line 70...
61
 
70
 
62
def is_cod_allowed(destination_pin):
71
def is_cod_allowed(destination_pin):
63
    cod = False
72
    cod = False
64
    try:
73
    try:
65
        for value in serviceable_location_cache.itervalues():
74
        for value in serviceable_location_cache.itervalues():
66
            cod ||= value[destination_pin][2]
75
            cod = cod or value[destination_pin][2]
67
    except Exception as ex:
76
    except Exception as ex:
68
        print "Unexpected error:", sys.exc_info()[0]
77
        print "Unexpected error:", sys.exc_info()[0]
69
    
78
    
70
    return cod
79
    return cod
71
 
80
 
Line 95... Line 104...
95
#        print "Unexpected error:", sys.exc_info()[0]
104
#        print "Unexpected error:", sys.exc_info()[0]
96
#        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
105
#        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
97
#                                        ", and warehouse location: " + str(warehouse_location))
106
#                                        ", and warehouse location: " + str(warehouse_location))
98
    
107
    
99
    try:
108
    try:
100
        return delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0], delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1], provider_id, warehouse_location
109
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
-
 
110
        reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
-
 
111
        delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location)
-
 
112
        return delivery_estimate
101
    except Exception as ex:
113
    except Exception as ex:
102
        print ex
114
        print ex
103
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
115
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
104
 
116
 
105
def __get_logistics_provider_for_destination_pincode(type, destination_pin):
117
def __get_logistics_provider_for_destination_pincode(type, destination_pin):