| Line 14... |
Line 14... |
| 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
|
17 |
from shop2020.utils.caching.SimpleCaching import memoized
|
| 18 |
|
18 |
|
| - |
|
19 |
warehouse_allocation_cache = {}
|
| - |
|
20 |
serviceable_location_cache = {}
|
| - |
|
21 |
delivery_estimate_cache = {}
|
| - |
|
22 |
|
| 19 |
def initialize(dbname="logistics", db_hostname="localhost"):
|
23 |
def initialize(dbname="logistics", db_hostname="localhost"):
|
| 20 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
24 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 21 |
DataService.initialize(dbname, db_hostname)
|
25 |
DataService.initialize(dbname, db_hostname)
|
| - |
|
26 |
print "Starting cache population at: " + datetime.datetime.now()
|
| - |
|
27 |
__cache_warehouse_allocation_table()
|
| - |
|
28 |
__cache_serviceable_location_details_table()
|
| - |
|
29 |
__cache_delivery_estimate_table()
|
| - |
|
30 |
close_session()
|
| - |
|
31 |
print "Done cache population at: " + datetime.datetime.now()
|
| - |
|
32 |
|
| - |
|
33 |
def __cache_delivery_estimate_table():
|
| - |
|
34 |
delivery_estimates = DeliveryEstimate.query.all()
|
| - |
|
35 |
for delivery_estimate in delivery_estimates:
|
| - |
|
36 |
delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
|
| - |
|
37 |
=delivery_estimate.delivery_time, delivery_estimate.reliability
|
| - |
|
38 |
|
| - |
|
39 |
def __cache_serviceable_location_details_table():
|
| - |
|
40 |
serviceable_locations = ServiceableLocationDetails.query.all()
|
| - |
|
41 |
for serviceable_location in serviceable_locations:
|
| - |
|
42 |
try:
|
| - |
|
43 |
provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
|
| - |
|
44 |
except:
|
| - |
|
45 |
provider_pincodes = {}
|
| - |
|
46 |
serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes
|
| - |
|
47 |
provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.station_type
|
| - |
|
48 |
|
| - |
|
49 |
def __cache_warehouse_allocation_table():
|
| - |
|
50 |
warehouse_allocations = WarehouseAllocation.query.all()
|
| - |
|
51 |
for warehouse_allocation in warehouse_allocations:
|
| - |
|
52 |
warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
|
| 22 |
|
53 |
|
| 23 |
def get_provider(provider_id):
|
54 |
def get_provider(provider_id):
|
| 24 |
provider = Provider.get_by(id=provider_id)
|
55 |
provider = Provider.get_by(id=provider_id)
|
| 25 |
return provider
|
56 |
return provider
|
| 26 |
|
57 |
|
| 27 |
def get_providers():
|
58 |
def get_providers():
|
| 28 |
providers = Provider.query.all()
|
59 |
providers = Provider.query.all()
|
| 29 |
return providers
|
60 |
return providers
|
| 30 |
|
61 |
|
| 31 |
def is_cod_allowed(destination_pin):
|
62 |
def is_cod_allowed(destination_pin):
|
| - |
|
63 |
cod = False
|
| 32 |
try:
|
64 |
try:
|
| 33 |
sld = ServiceableLocationDetails.get_by(dest_pincode = destination_pin, cod = True)
|
65 |
for value in serviceable_location_cache.itervalues():
|
| - |
|
66 |
cod ||= value[destination_pin][2]
|
| 34 |
except Exception as ex:
|
67 |
except Exception as ex:
|
| 35 |
print "Unexpected error:", sys.exc_info()[0]
|
68 |
print "Unexpected error:", sys.exc_info()[0]
|
| 36 |
|
69 |
|
| 37 |
if sld:
|
- |
|
| 38 |
return True
|
70 |
return cod
|
| 39 |
else:
|
- |
|
| 40 |
return False
|
- |
|
| 41 |
|
71 |
|
| 42 |
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
|
72 |
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
|
| 43 |
if warehouse_location is None:
|
73 |
if warehouse_location is None:
|
| 44 |
try:
|
74 |
try:
|
| 45 |
warehouse_location = WarehouseAllocation.get_by(pincode = destination_pin).primary_warehouse_location
|
75 |
warehouse_location = warehouse_allocation_cache[destination_pin]
|
| 46 |
except:
|
76 |
except:
|
| 47 |
print "Unexpected error:", sys.exc_info()[0]
|
77 |
print "Unexpected error:", sys.exc_info()[0]
|
| 48 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
78 |
raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
|
| 49 |
|
79 |
|
| 50 |
provider = __get_logistics_provider_for_destination_pincode(type, destination_pin)
|
80 |
provider_id = __get_logistics_provider_for_destination_pincode(type, destination_pin)
|
| 51 |
|
81 |
|
| 52 |
if not provider:
|
82 |
if not provider_id:
|
| 53 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
83 |
raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
| 54 |
", and warehouse location: " + str(warehouse_location))
|
84 |
", and warehouse location: " + str(warehouse_location))
|
| 55 |
|
85 |
|
| 56 |
# query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
|
86 |
# query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
|
| 57 |
# query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
|
87 |
# query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
|
| Line 64... |
Line 94... |
| 64 |
# print ex
|
94 |
# print ex
|
| 65 |
# print "Unexpected error:", sys.exc_info()[0]
|
95 |
# print "Unexpected error:", sys.exc_info()[0]
|
| 66 |
# raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
96 |
# raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
|
| 67 |
# ", and warehouse location: " + str(warehouse_location))
|
97 |
# ", and warehouse location: " + str(warehouse_location))
|
| 68 |
|
98 |
|
| 69 |
query = DeliveryEstimate.query.filter_by(warehouse_location = warehouse_location)
|
- |
|
| 70 |
query = query.filter_by(destination_pin = destination_pin)
|
- |
|
| 71 |
query = query.filter_by(provider = provider)
|
- |
|
| 72 |
try:
|
99 |
try:
|
| 73 |
return query.first()
|
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
|
| 74 |
except Exception as ex:
|
101 |
except Exception as ex:
|
| 75 |
print ex
|
102 |
print ex
|
| 76 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
103 |
raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
|
| 77 |
|
104 |
|
| 78 |
def __get_logistics_provider_for_destination_pincode(type, destination_pin):
|
105 |
def __get_logistics_provider_for_destination_pincode(type, destination_pin):
|
| - |
|
106 |
for provider_id, value in serviceable_location_cache.iteritems():
|
| 79 |
try:
|
107 |
try:
|
| 80 |
sld = ServiceableLocationDetails.get_by(dest_pincode = destination_pin)
|
108 |
dest_code, exp, cod, station_type = value[destination_pin]
|
| - |
|
109 |
except:
|
| 81 |
if type == DeliveryType.PREPAID and sld.exp:
|
110 |
print "Unexpected error:", sys.exc_info()[0]
|
| 82 |
provider = sld.provider
|
111 |
continue
|
| 83 |
if type == DeliveryType.COD and sld.cod:
|
112 |
if type == DeliveryType.PREPAID and exp:
|
| 84 |
provider = sld.provider
|
113 |
return provider_id
|
| 85 |
except Exception as ex:
|
114 |
if type == DeliveryType.COD and cod:
|
| 86 |
print "Unexpected error:", sys.exc_info()[0]
|
115 |
return provider_id
|
| 87 |
return provider
|
116 |
return None
|
| 88 |
|
117 |
|
| 89 |
def add_empty_AWBs(numbers, provider_id, type):
|
118 |
def add_empty_AWBs(numbers, provider_id, type):
|
| 90 |
for number in numbers:
|
119 |
for number in numbers:
|
| 91 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
120 |
query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
|
| 92 |
try:
|
121 |
try:
|
| Line 130... |
Line 159... |
| 130 |
awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
|
159 |
awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
|
| 131 |
query = AwbUpdate.query.filter_by(awb = awb)
|
160 |
query = AwbUpdate.query.filter_by(awb = awb)
|
| 132 |
info = query.all()
|
161 |
info = query.all()
|
| 133 |
return info
|
162 |
return info
|
| 134 |
|
163 |
|
| 135 |
'''
|
- |
|
| 136 |
Cache the return values of the function for one day
|
- |
|
| 137 |
'''
|
- |
|
| 138 |
@memoized(86400)
|
- |
|
| 139 |
def get_holidays(start_date, end_date):
|
164 |
def get_holidays(start_date, end_date):
|
| 140 |
query = PublicHolidays.query
|
165 |
query = PublicHolidays.query
|
| 141 |
if start_date != -1:
|
166 |
if start_date != -1:
|
| 142 |
query = query.filter(PublicHolidays.date >= to_py_date(start_date))
|
167 |
query = query.filter(PublicHolidays.date >= to_py_date(start_date))
|
| 143 |
if end_date != -1:
|
168 |
if end_date != -1:
|