You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
341 lines
9.6 KiB
341 lines
9.6 KiB
from django.core.mail import mail_admins
|
|
import requests
|
|
from django.core.cache import cache
|
|
|
|
|
|
class AlemTat(object):
|
|
COUNTRY_CODE = '0001'
|
|
CITY_CODE = '000019'
|
|
API_KEY = '677a8773-c647-4b8f-8968-32a67d55e0d3'
|
|
CONTRACT = '05828/ИМ'
|
|
|
|
ZONES_FROM_UK_E = (
|
|
('000019', 0),
|
|
('000003', 2),
|
|
('000008', 2),
|
|
('000009', 5),
|
|
('000011', 2),
|
|
('000006', 2),
|
|
('000078', 5),
|
|
('000004', 2),
|
|
('000106', 4),
|
|
('000064', 4),
|
|
('000005', 2),
|
|
('000014', 2),
|
|
('000016', 2),
|
|
('000010', 2),
|
|
('000017', 2),
|
|
('000018', 2),
|
|
('000020', 2),
|
|
('000055', 2),
|
|
('000015', 2),
|
|
('000007', 2),
|
|
('000012', 2),
|
|
('000024', 2),
|
|
('003000', 5),
|
|
('000229', 5),
|
|
('000198', 5),
|
|
('000098', 5),
|
|
('000100', 5),
|
|
('000080', 5),
|
|
('000116', 2),
|
|
('000119', 5),
|
|
('000114', 5),
|
|
('000069', 5),
|
|
('000082', 5),
|
|
('000263', 2),
|
|
('000077', 5),
|
|
('000070', 5),
|
|
('000036', 5),
|
|
('000157', 5),
|
|
('000071', 5),
|
|
('000076', 5),
|
|
('000096', 5),
|
|
('000205', 5),
|
|
('000183', 5),
|
|
)
|
|
|
|
def _build_url(self, url):
|
|
# url = url.replace('\{ext\}', API_KEY)
|
|
retval = url
|
|
if '?' in url:
|
|
retval = url + '&ApiKey={}'.format(self.API_KEY)
|
|
else:
|
|
retval = url + '?ApiKey={}'.format(self.API_KEY)
|
|
return retval
|
|
|
|
def get_cities(self):
|
|
cache_key = 'alemtat_cities'
|
|
retval = cache.get(cache_key, None)
|
|
if not retval:
|
|
url = self._build_url(
|
|
'http://api.alemtat.kz/web/{ext}/Catalog/getCitiesByCountry?CountryLocalCode={code}'.format(
|
|
code=self.COUNTRY_CODE, ext=self.API_KEY))
|
|
r = requests.get(url)
|
|
retval = r.json()
|
|
cache.set(cache_key, retval, 60 * 60 * 24 * 7)
|
|
return retval
|
|
|
|
def get_cities_tuple(self):
|
|
if self.get_cities()['Message'] != 'Для этого запроса отказано в авторизации.':
|
|
retval = map(lambda cities: (cities['LocalCode'], '{} - {}'.format(cities['LocalityName'].title(), cities[
|
|
'Region'].capitalize())), self.get_cities())
|
|
return tuple(retval)
|
|
else:
|
|
return tuple()
|
|
|
|
def get_services(self):
|
|
cache_key = 'alemtat_services'
|
|
retval = cache.get(cache_key, None)
|
|
if not retval:
|
|
url = self._build_url(
|
|
'http://api.alemtat.kz/web/{ext}/Catalog/getServices'.format(ext=self.API_KEY))
|
|
r = requests.get(url)
|
|
retval = r.json()
|
|
if retval['Message'] != 'Для этого запроса отказано в авторизации.':
|
|
cache.set(cache_key, retval, 60 * 60 * 24 * 7)
|
|
return filter(lambda s: s['LocalCode'] in ['E', 'T'], retval)
|
|
else:
|
|
return list()
|
|
|
|
def get_services_tuple(self):
|
|
retval = map(lambda services: (services['LocalCode'], services['Name'],), self.get_services())
|
|
return tuple(retval)
|
|
|
|
# def get_services_tuple(self):
|
|
# retval = map(lambda cities: (cities['LocalCode'], '{} - {}'.format(cities['LocalityName'].title(), cities[
|
|
# 'Region'].capitalize())), self.get_cities())
|
|
# return tuple(retval)
|
|
|
|
def get_amount(self, to, places, weight, service):
|
|
# mail_admins('deliv', 'to:{}, places:{}, weight:{}, service:{}'.format(to, places, weight, service))
|
|
url = self._build_url(
|
|
'http://api.alemtat.kz/web/{ext}/Calc/getAmount'.format(ext=self.API_KEY))
|
|
post_data = dict(
|
|
FromCountryCode=self.COUNTRY_CODE,
|
|
FromLocalCode=self.CITY_CODE,
|
|
ToCountryCode=self.COUNTRY_CODE,
|
|
ToLocalCode=to,
|
|
ServiceLocalCode=service,
|
|
Places=places,
|
|
Weight=weight,
|
|
Contract=self.CONTRACT,
|
|
)
|
|
r = requests.post(url, data=post_data)
|
|
try:
|
|
if r.json()['AmountPlusFSAmount'] > 0:
|
|
return r.json()
|
|
else:
|
|
return self.get_amount_alternative(to, places, weight, service)
|
|
except KeyError:
|
|
return 0
|
|
|
|
ZONES_E = (
|
|
(0.3, (
|
|
(1, 1480),
|
|
(2, 1590),
|
|
(3, 1920),
|
|
(4, 2430),
|
|
(5, 2900),
|
|
),),
|
|
(0.5, (
|
|
(1, 1900),
|
|
(2, 2280),
|
|
(3, 2450),
|
|
(4, 2850),
|
|
(5, 3100),
|
|
),),
|
|
(1, (
|
|
(1, 2432),
|
|
(2, 2440),
|
|
(3, 2700),
|
|
(4, 3300),
|
|
(5, 3510),
|
|
),),
|
|
(1.5, (
|
|
(1, 2584),
|
|
(2, 2700),
|
|
(3, 2830),
|
|
(4, 3500),
|
|
(5, 3780),
|
|
),),
|
|
(2, (
|
|
(1, 2704),
|
|
(2, 2830),
|
|
(3, 2950),
|
|
(4, 3650),
|
|
(5, 3900),
|
|
),),
|
|
(2.5, (
|
|
(1, 2824),
|
|
(2, 2950),
|
|
(3, 3070),
|
|
(4, 3780),
|
|
(5, 4020),
|
|
),),
|
|
(3, (
|
|
(1, 2944),
|
|
(2, 3070),
|
|
(3, 3200),
|
|
(4, 3900),
|
|
(5, 4150),
|
|
),),
|
|
(3.5, (
|
|
(1, 3064),
|
|
(2, 3200),
|
|
(3, 3300),
|
|
(4, 4020),
|
|
(5, 4550),
|
|
),),
|
|
(4, (
|
|
(1, 3184),
|
|
(2, 3300),
|
|
(3, 3430),
|
|
(4, 4150),
|
|
(5, 4680),
|
|
),),
|
|
(4.5, (
|
|
(1, 3304),
|
|
(2, 3430),
|
|
(3, 3560),
|
|
(4, 4250),
|
|
(5, 4810),
|
|
),),
|
|
(5, (
|
|
(1, 3424),
|
|
(2, 3560),
|
|
(3, 3640),
|
|
(4, 4380),
|
|
(5, 4890),
|
|
),),
|
|
)
|
|
|
|
ZONES_T = (
|
|
(5, (
|
|
(1, 1300),
|
|
(2, 1500),
|
|
(3, 1700),
|
|
(4, 2500),
|
|
(5, 2500),
|
|
),),
|
|
(10, (
|
|
(1, 1550),
|
|
(2, 2050),
|
|
(3, 2550),
|
|
(4, 2750),
|
|
(5, 3750),
|
|
),),
|
|
(30, (
|
|
(1, 4550),
|
|
(2, 6050),
|
|
(3, 7550),
|
|
(4, 8750),
|
|
(5, 11750),
|
|
),),
|
|
(60, (
|
|
(1, 9050),
|
|
(2, 12050),
|
|
(3, 15050),
|
|
(4, 17750),
|
|
(5, 23750),
|
|
),),
|
|
(100, (
|
|
(1, 15050),
|
|
(2, 20050),
|
|
(3, 25050),
|
|
(4, 31750),
|
|
(5, 41750),
|
|
),),
|
|
)
|
|
|
|
def get_weight_price(self, zone, weight, service):
|
|
retval = 0
|
|
|
|
if zone == 0:
|
|
return retval
|
|
|
|
if service == 'E':
|
|
|
|
zones_prices = dict(self.ZONES_E)
|
|
if weight <= 0.3:
|
|
retval = dict(zones_prices[0.3])[zone]
|
|
elif weight <= 0.5:
|
|
retval = dict(zones_prices[0.5])[zone]
|
|
elif weight <= 1:
|
|
retval = dict(zones_prices[1])[zone]
|
|
elif weight <= 1.5:
|
|
retval = dict(zones_prices[1.5])[zone]
|
|
elif weight <= 2:
|
|
retval = dict(zones_prices[2])[zone]
|
|
elif weight <= 2.5:
|
|
retval = dict(zones_prices[2.5])[zone]
|
|
elif weight <= 3:
|
|
retval = dict(zones_prices[3])[zone]
|
|
elif weight <= 3.5:
|
|
retval = dict(zones_prices[3.5])[zone]
|
|
elif weight <= 4:
|
|
retval = dict(zones_prices[4])[zone]
|
|
elif weight <= 4.5:
|
|
retval = dict(zones_prices[4.5])[zone]
|
|
elif weight <= 5:
|
|
retval = dict(zones_prices[5])[zone]
|
|
elif weight > 5:
|
|
retval = dict(zones_prices[5])[zone]
|
|
plusweight = (
|
|
(1, 456),
|
|
(2, 460),
|
|
(3, 465),
|
|
(4, 630),
|
|
(5, 650),
|
|
)
|
|
retval += int(weight - 5) * dict(plusweight)[zone]
|
|
|
|
if service == 'T':
|
|
zones_prices = dict(self.ZONES_T)
|
|
plusweight = (
|
|
(1, 150),
|
|
(2, 200),
|
|
(3, 250),
|
|
(4, 300),
|
|
(5, 400),
|
|
)
|
|
if weight <= 5:
|
|
retval = dict(zones_prices[5])[zone]
|
|
elif weight <= 10:
|
|
retval = dict(zones_prices[10])[zone]
|
|
elif weight > 10 and weight < 30:
|
|
retval = dict(zones_prices[10])[zone]
|
|
retval += int(weight - 10) * dict(plusweight)[zone]
|
|
elif weight >= 30 and weight < 60:
|
|
retval = dict(zones_prices[30])[zone]
|
|
retval += int(weight - 30) * dict(plusweight)[zone]
|
|
elif weight >= 60 and weight < 100:
|
|
retval = dict(zones_prices[60])[zone]
|
|
retval += int(weight - 60) * dict(plusweight)[zone]
|
|
elif weight >= 100:
|
|
retval = dict(zones_prices[100])[zone]
|
|
retval += int(weight - 100) * dict(plusweight)[zone]
|
|
|
|
return retval
|
|
|
|
def get_amount_alternative(self, to, places, weight, service):
|
|
retval = dict(AmountPlusFSAmount=0)
|
|
zone = 5
|
|
try:
|
|
zone = dict(self.ZONES_FROM_UK_E)[to]
|
|
except:
|
|
pass
|
|
|
|
retval['AmountPlusFSAmount'] = self.get_weight_price(zone, weight, service)
|
|
|
|
return retval
|
|
|
|
|
|
def alemtat_get_cities_tuple():
|
|
a = AlemTat()
|
|
return a.get_cities_tuple()
|
|
|
|
|
|
def alemtat_get_services_tuple():
|
|
a = AlemTat()
|
|
return a.get_services_tuple()
|
|
|