union_auto_test/core/utils.py

1321 lines
45 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import traceback
import random
import imghdr
from django.templatetags.static import static
from mailer import send_mail
from django.conf import settings
from django.http import HttpResponse
from django.template import loader
from core.models import (MainLot, KoreaLot, MachineryLot, TransportBrand, TransportModel,
MachineryBrand, MachineryModel, MachineryGroup,
TransportExperience, MachineryExperience, MachineryFilter, Contact,
Lead, BitrixEmployee)
def is_image(url):
image_format = None
response = requests.get(url)
if response.status_code == 200:
image_format = imghdr.what(None, response.content)
if image_format and image_format != 'gif':
return True
return False
def sql_request_for_api(sql_request):
return sql_request.replace(' ', '%20')
def send_message_on_telegram(message):
"""Отправка сообщения в телеграм."""
endpoint = f'https://api.telegram.org/bot{settings.TELEGRAM_ACCESS_TOKEN}/sendMessage'
params = {
'chat_id': settings.TELEGRAM_CHAT_ID_FEEDBACK,
'text': message,
'parse_mode': 'HTML'
}
response = requests.post(endpoint, params=params)
if not response.ok:
raise Exception(
'[TelegramPublisher] Response is not ok\n'
'-----------\n'
f'Params: {params})\n'
f'Response: {response}'
)
return HttpResponse()
def send_message_to_mail_and_telegram(template, data):
"""Отправка сообщения на почту и в телеграм."""
try:
message_template = loader.get_template(f'messages/{template}')
message = message_template.render({'data': data})
if settings.EMAIL_NOTIFICATIONS_ENABLED:
send_mail(settings.SUBJECT, message, settings.EMAIL_HOST_USER, settings.EMAIL_RECIPIENT, fail_silently=False)
else:
print('\n>>> Уведомления на EMAIL отключены:\n\n', message, '\n')
if settings.TELEGRAM_NOTIFICATIONS_ENABLED:
send_message_on_telegram(message)
else:
print('\n>>> Уведомления в TELEGRAM отключены:\n\n', message, '\n')
except Exception as err:
traceback.print_exc()
def similar_japan(lot: MainLot, ip_address, truck_flag=False):
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
sql_request = f"select * from main where (status LIKE '%25not sold%25' or status='') and (auction not like '%25uss%25') and lot!={lot.lot}"
if not truck_flag:
sql_request += f' and YEAR>={settings.MIN_YEAR}'
if lot.year:
sql_request += f' and YEAR>={lot.year-2} and YEAR<={lot.year+2}'
if lot.avg_price:
sql_request += f' and AVG_PRICE>={lot.avg_price * settings.MIN_PRICE_COEFF} and AVG_PRICE<={lot.avg_price * settings.MAX_PRICE_COEFF}'
sql_request += f' and MARKA_ID={lot.car_model.brand.db_id} and MODEL_ID={lot.car_model.db_id} limit 10'
sql_request = sql_request_for_api(sql_request)
try:
response = requests.get(AUCTION_ENDPOINT + sql_request)
lots = response.json()
except:
lots= None
cars = []
if lots:
for lot_info in lots:
lot_id = lot_info.get('ID')
main_lot, created = MainLot.objects.get_or_create(db_id=lot_id)
if created:
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_id = int(lot_info.get('MODEL_ID'))
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = TransportBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = TransportModel.objects.get_or_create(brand=brand, db_id=model_id, title=model_title, category="1")
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
eng_v = int(lot_info.get('ENG_V'))
except:
eng_v = None
try:
pw = int(lot_info.get('PW'))
except:
try:
pw = int(lot_info.get('PW').split(',')[0])
except:
pw = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
start = int(lot_info.get('START'))
except:
start = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
try:
avg_price = int(lot_info.get('AVG_PRICE'))
except:
avg_price = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.car_model = model
main_lot.year = year
main_lot.eng_v = eng_v
main_lot.pw = pw
main_lot.town = lot_info.get('TOWN')
main_lot.kuzov = lot_info.get('KUZOV')
main_lot.grade = lot_info.get('GRADE')
main_lot.color = lot_info.get('COLOR')
main_lot.kpp = lot_info.get('KPP')
main_lot.kpp_type = lot_info.get('KPP_TYPE')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.equip = lot_info.get('EQUIP')
main_lot.rate = lot_info.get('RATE')
main_lot.start = start
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.time = lot_info.get('TIME')
main_lot.avg_price = avg_price
main_lot.avg_string = lot_info.get('AVG_STRING')
main_lot.cover = cover
main_lot.images = images
main_lot.save()
cars.append(main_lot)
return cars
def similar_korea(lot: KoreaLot, ip_address):
sql_request = f"select * from korea where (status LIKE '%25not sold%25' or status='') and YEAR>={settings.MIN_YEAR}"
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
if lot.year:
sql_request += f' and YEAR>={lot.year-2} and YEAR<={lot.year+2}'
if lot.avg_price:
sql_request += f' and AVG_PRICE>={lot.avg_price * settings.MIN_PRICE_COEFF} and AVG_PRICE<={lot.avg_price * settings.MAX_PRICE_COEFF}'
sql_request += f' and MARKA_ID={lot.car_model.brand.db_id} and MODEL_ID={lot.car_model.db_id} limit 10'
sql_request = sql_request_for_api(sql_request)
try:
response = requests.get(AUCTION_ENDPOINT + sql_request)
lots = response.json()
except:
lots= None
cars = []
if lots:
for lot_info in lots:
lot_id = lot_info.get('ID')
main_lot, created = KoreaLot.objects.get_or_create(db_id=lot_id)
if created:
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_id = int(lot_info.get('MODEL_ID'))
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = TransportBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = TransportModel.objects.get_or_create(brand=brand, db_id=model_id, title=model_title, category="2")
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
eng_v = int(lot_info.get('ENG_V'))
except:
eng_v = None
try:
pw = int(lot_info.get('PW'))
except:
try:
pw = int(lot_info.get('PW').split(',')[0])
except:
pw = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
start = int(lot_info.get('START'))
except:
start = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
try:
avg_price = int(lot_info.get('AVG_PRICE'))
except:
avg_price = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.car_model = model
main_lot.year = year
main_lot.eng_v = eng_v
main_lot.pw = pw
main_lot.kuzov = lot_info.get('KUZOV')
main_lot.grade = lot_info.get('GRADE')
main_lot.color = lot_info.get('COLOR')
main_lot.kpp = lot_info.get('KPP')
main_lot.kpp_type = lot_info.get('KPP_TYPE')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.equip = lot_info.get('EQUIP')
main_lot.rate = lot_info.get('RATE')
main_lot.start = start
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.time = lot_info.get('TIME')
main_lot.avg_price = avg_price
main_lot.avg_string = lot_info.get('AVG_STRING')
main_lot.cover = cover
main_lot.images = images
main_lot.save()
cars.append(main_lot)
return cars
def similar_machinery(lot: MachineryLot, ip_address):
sql_request = f"select * from hdm where (status LIKE '%25not sold%25' or status='')"
MACHINERY_ENDPOINT = settings.MACHINERY_ENDPOINT
if ip_address:
MACHINERY_ENDPOINT = MACHINERY_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
if lot.price:
sql_request += f' and PRICE >={lot.price * settings.MIN_PRICE_COEFF} and PRICE <={lot.price * settings.MAX_PRICE_COEFF}'
if lot.group:
sql_request += f' and GROUP LIKE "%25{lot.group}%25"'
if lot.category:
sql_request += f' and CATEGORY LIKE "%25{lot.category}%25"'
sql_request += ' limit 10'
sql_request = sql_request_for_api(sql_request)
try:
response = requests.get(MACHINERY_ENDPOINT + sql_request)
lots = response.json()
except:
lots= None
cars = []
if lots:
for lot_info in lots:
lot_id = lot_info.get('ID')
main_lot, created = MachineryLot.objects.get_or_create(db_id=lot_id)
if created:
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = MachineryBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = MachineryModel.objects.get_or_create(brand=brand, title=model_title)
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
price = int(lot_info.get('PRICE'))
except:
price = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
machinery_group = lot_info.get('CATEGORY')
machinery_type = None
if machinery_group:
curr_group = MachineryGroup.objects.filter(title=machinery_group.upper()).first()
if curr_group:
machinery_type = curr_group.filters.first()
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.group = lot_info.get('GROUP')
main_lot.category = machinery_group
main_lot.gg = lot_info.get('GG')
main_lot.cc = lot_info.get('CC')
main_lot.machinery_model = model
main_lot.year = year
main_lot.grade = lot_info.get('GRADE')
main_lot.kpp = lot_info.get('KPP')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.rate = lot_info.get('RATE')
main_lot.price = price
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.cover = cover
main_lot.images = images
main_lot.machinery_filter = machinery_type
main_lot.save()
cars.append(main_lot)
return cars
def similar_japan_experience(lot: TransportExperience, ip_address):
sql_request = f"select * from main where (status LIKE '%25not sold%25' or status='') and (auction not like '%25uss%25') and YEAR>={settings.MIN_YEAR}"
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
if lot.year:
sql_request += f' and YEAR>={lot.year-2} and YEAR<={lot.year+2}'
if lot.auction_price:
sql_request += f' and AVG_PRICE>={lot.auction_price * settings.MIN_PRICE_COEFF} and AVG_PRICE<={lot.auction_price * settings.MAX_PRICE_COEFF}'
sql_request += f' and MARKA_ID={lot.brand.db_id} and MODEL_ID={lot.transport_model.db_id} limit 10'
sql_request = sql_request_for_api(sql_request)
try:
response = requests.get(AUCTION_ENDPOINT + sql_request)
lots = response.json()
except:
lots= None
cars = []
if lots:
for lot_info in lots:
lot_id = lot_info.get('ID')
main_lot, created = MainLot.objects.get_or_create(db_id=lot_id)
if created:
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_id = int(lot_info.get('MODEL_ID'))
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = TransportBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = TransportModel.objects.get_or_create(brand=brand, db_id=model_id, title=model_title, category="1")
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
eng_v = int(lot_info.get('ENG_V'))
except:
eng_v = None
try:
pw = int(lot_info.get('PW'))
except:
try:
pw = int(lot_info.get('PW').split(',')[0])
except:
pw = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
start = int(lot_info.get('START'))
except:
start = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
try:
avg_price = int(lot_info.get('AVG_PRICE'))
except:
avg_price = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.car_model = model
main_lot.year = year
main_lot.eng_v = eng_v
main_lot.pw = pw
main_lot.town = lot_info.get('TOWN')
main_lot.kuzov = lot_info.get('KUZOV')
main_lot.grade = lot_info.get('GRADE')
main_lot.color = lot_info.get('COLOR')
main_lot.kpp = lot_info.get('KPP')
main_lot.kpp_type = lot_info.get('KPP_TYPE')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.equip = lot_info.get('EQUIP')
main_lot.rate = lot_info.get('RATE')
main_lot.start = start
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.time = lot_info.get('TIME')
main_lot.avg_price = avg_price
main_lot.avg_string = lot_info.get('AVG_STRING')
main_lot.cover = cover
main_lot.images = images
main_lot.save()
cars.append(main_lot)
return cars
def similar_korea_experience(lot: TransportExperience, ip_address):
sql_request = f"select * from korea where (status LIKE '%25not sold%25' or status='') and YEAR>={settings.MIN_YEAR}"
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
if lot.year:
sql_request += f' and YEAR>={lot.year-2} and YEAR<={lot.year+2}'
if lot.auction_price:
sql_request += f' and AVG_PRICE>={lot.auction_price * settings.MIN_PRICE_COEFF} and AVG_PRICE<={lot.auction_price * settings.MAX_PRICE_COEFF}'
sql_request += f' and MARKA_ID={lot.brand.db_id} and MODEL_ID={lot.transport_model.db_id} limit 10'
sql_request = sql_request_for_api(sql_request)
try:
response = requests.get(AUCTION_ENDPOINT + sql_request)
lots = response.json()
except:
lots= None
cars = []
if lots:
for lot_info in lots:
lot_id = lot_info.get('ID')
main_lot, created = KoreaLot.objects.get_or_create(db_id=lot_id)
if created:
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_id = int(lot_info.get('MODEL_ID'))
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = TransportBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = TransportModel.objects.get_or_create(brand=brand, db_id=model_id, title=model_title, category="2")
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
eng_v = int(lot_info.get('ENG_V'))
except:
eng_v = None
try:
pw = int(lot_info.get('PW'))
except:
try:
pw = int(lot_info.get('PW').split(',')[0])
except:
pw = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
start = int(lot_info.get('START'))
except:
start = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
try:
avg_price = int(lot_info.get('AVG_PRICE'))
except:
avg_price = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.car_model = model
main_lot.year = year
main_lot.eng_v = eng_v
main_lot.pw = pw
main_lot.kuzov = lot_info.get('KUZOV')
main_lot.grade = lot_info.get('GRADE')
main_lot.color = lot_info.get('COLOR')
main_lot.kpp = lot_info.get('KPP')
main_lot.kpp_type = lot_info.get('KPP_TYPE')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.equip = lot_info.get('EQUIP')
main_lot.rate = lot_info.get('RATE')
main_lot.start = start
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.time = lot_info.get('TIME')
main_lot.avg_price = avg_price
main_lot.avg_string = lot_info.get('AVG_STRING')
main_lot.cover = cover
main_lot.images = images
main_lot.save()
cars.append(main_lot)
return cars
def similar_machinery_experience(lot: MachineryExperience, ip_address):
sql_request = f"select * from hdm where (status LIKE '%25not sold%25' or status='')"
MACHINERY_ENDPOINT = settings.MACHINERY_ENDPOINT
if ip_address:
MACHINERY_ENDPOINT = MACHINERY_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
if lot.auction_price:
sql_request += f' and PRICE >={lot.auction_price * settings.MIN_PRICE_COEFF} and PRICE <={lot.auction_price * settings.MAX_PRICE_COEFF}'
sql_request += f' and MARKA_ID={lot.brand.db_id} limit 10'
sql_request = sql_request_for_api(sql_request)
try:
response = requests.get(MACHINERY_ENDPOINT + sql_request)
lots = response.json()
except:
lots= None
cars = []
if lots:
for lot_info in lots:
lot_id = lot_info.get('ID')
main_lot, created = MachineryLot.objects.get_or_create(db_id=lot_id)
if created:
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = MachineryBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = MachineryModel.objects.get_or_create(brand=brand, title=model_title)
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
price = int(lot_info.get('PRICE'))
except:
price = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
machinery_group = lot_info.get('CATEGORY')
machinery_type = None
if machinery_group:
curr_group = MachineryGroup.objects.filter(title=machinery_group.upper()).first()
if curr_group:
machinery_type = curr_group.filters.first()
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.group = lot_info.get('GROUP')
main_lot.category = machinery_group
main_lot.gg = lot_info.get('GG')
main_lot.cc = lot_info.get('CC')
main_lot.machinery_model = model
main_lot.year = year
main_lot.grade = lot_info.get('GRADE')
main_lot.kpp = lot_info.get('KPP')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.rate = lot_info.get('RATE')
main_lot.price = price
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.cover = cover
main_lot.images = images
main_lot.machinery_filter = machinery_type
main_lot.save()
cars.append(main_lot)
return cars
def quality_japan(lot_id, ip_address):
sql_request = f'select * from main where id="{lot_id}"&ajax'
sql_request = sql_request_for_api(sql_request)
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
response = requests.get(AUCTION_ENDPOINT + sql_request)
lot_info = response.json()[0]
main_lot, _ = MainLot.objects.get_or_create(db_id=lot_id)
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_id = int(lot_info.get('MODEL_ID'))
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = TransportBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = TransportModel.objects.get_or_create(brand=brand, db_id=model_id, title=model_title, category="1")
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
eng_v = int(lot_info.get('ENG_V'))
except:
eng_v = None
try:
pw = int(lot_info.get('PW'))
except:
try:
pw = int(lot_info.get('PW').split(',')[0])
except:
pw = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
start = int(lot_info.get('START'))
except:
start = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
try:
avg_price = int(lot_info.get('AVG_PRICE'))
except:
avg_price = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
auction_list = images[0]
else:
auction_list = None
if len(images) > 1:
cover = images[1]
else:
cover = None
if len(images) > 2:
images = '#'.join(images[2::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.car_model = model
main_lot.year = year
main_lot.eng_v = eng_v
main_lot.pw = pw
main_lot.town = lot_info.get('TOWN')
main_lot.kuzov = lot_info.get('KUZOV')
main_lot.grade = lot_info.get('GRADE')
main_lot.color = lot_info.get('COLOR')
main_lot.kpp = lot_info.get('KPP')
main_lot.kpp_type = lot_info.get('KPP_TYPE')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.equip = lot_info.get('EQUIP')
main_lot.rate = lot_info.get('RATE')
main_lot.start = start
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.time = lot_info.get('TIME')
main_lot.avg_price = avg_price
main_lot.avg_string = lot_info.get('AVG_STRING')
main_lot.cover = cover
main_lot.auction_list = auction_list
main_lot.images = images
main_lot.serial = lot_info.get('SERIAL')
main_lot.info = lot_info.get('INFO')
main_lot.full_info = True
main_lot.save()
return main_lot
def quality_korea(lot_id, ip_address):
sql_request = f'select * from korea where id="{lot_id}"&ajax'
sql_request = sql_request_for_api(sql_request)
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
response = requests.get(AUCTION_ENDPOINT + sql_request)
lot_info = response.json()[0]
main_lot, _ = KoreaLot.objects.get_or_create(db_id=lot_id)
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_id = int(lot_info.get('MODEL_ID'))
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = TransportBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = TransportModel.objects.get_or_create(brand=brand, db_id=model_id, title=model_title, category="2")
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
eng_v = int(lot_info.get('ENG_V'))
except:
eng_v = None
try:
pw = int(lot_info.get('PW'))
except:
try:
pw = int(lot_info.get('PW').split(',')[0])
except:
pw = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
start = int(lot_info.get('START'))
except:
start = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
try:
avg_price = int(lot_info.get('AVG_PRICE'))
except:
avg_price = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
cover = images[0]
else:
cover = None
if len(images) > 1:
images = '#'.join(images[1::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.car_model = model
main_lot.year = year
main_lot.eng_v = eng_v
main_lot.pw = pw
main_lot.kuzov = lot_info.get('KUZOV')
main_lot.grade = lot_info.get('GRADE')
main_lot.color = lot_info.get('COLOR')
main_lot.kpp = lot_info.get('KPP')
main_lot.kpp_type = lot_info.get('KPP_TYPE')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.equip = lot_info.get('EQUIP')
main_lot.rate = lot_info.get('RATE')
main_lot.start = start
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.time = lot_info.get('TIME')
main_lot.avg_price = avg_price
main_lot.avg_string = lot_info.get('AVG_STRING')
main_lot.cover = cover
main_lot.images = images
main_lot.serial = lot_info.get('SERIAL')
main_lot.info = lot_info.get('INFO')
main_lot.full_info = True
main_lot.save()
return main_lot
def quality_machinery(lot_id, ip_address):
sql_request = f'select * from hdm where id="{lot_id}"&ajax'
sql_request = sql_request_for_api(sql_request)
MACHINERY_ENDPOINT = settings.MACHINERY_ENDPOINT
if ip_address:
MACHINERY_ENDPOINT = MACHINERY_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
response = requests.get(MACHINERY_ENDPOINT + sql_request)
if response.json():
lot_info = response.json()[0]
main_lot, _ = MachineryLot.objects.get_or_create(db_id=lot_id)
brand_id = int(lot_info.get('MARKA_ID'))
brand_title = lot_info.get('MARKA_NAME').upper()
model_title = lot_info.get('MODEL_NAME').upper()
brand, _ = MachineryBrand.objects.get_or_create(db_id=brand_id, title=brand_title)
model, _ = MachineryModel.objects.get_or_create(brand=brand, title=model_title)
try:
year = int(lot_info.get('YEAR'))
except:
year = None
try:
mileage = int(lot_info.get('MILEAGE'))
except:
mileage = None
try:
price = int(lot_info.get('PRICE'))
except:
price = None
try:
finish = int(lot_info.get('FINISH'))
except:
finish = None
images = lot_info.get('IMAGES').split('#')
if len(images) > 0:
auction_list = images[0]
else:
auction_list = None
cover = static('img/admin/shop/machinery-default.jpg') + '?v=1'
if len(images) > 1:
if is_image(images[1]):
cover = images[1]
if len(images) > 2:
images = '#'.join(images[2::])
else:
images = None
main_lot.lot = lot_info.get('LOT')
main_lot.auction_date = lot_info.get('AUCTION_DATE')
main_lot.auction = lot_info.get('AUCTION')
main_lot.group = lot_info.get('GROUP')
main_lot.category = lot_info.get('CATEGORY')
main_lot.gg = lot_info.get('GG')
main_lot.cc = lot_info.get('CC')
main_lot.machinery_model = model
main_lot.year = year
main_lot.grade = lot_info.get('GRADE')
main_lot.kpp = lot_info.get('KPP')
main_lot.priv = lot_info.get('PRIV')
main_lot.mileage = mileage
main_lot.rate = lot_info.get('RATE')
main_lot.price = price
main_lot.finish = finish
main_lot.status = lot_info.get('STATUS')
main_lot.auction_list = auction_list
main_lot.cover = cover
main_lot.images = images
main_lot.serial = lot_info.get('SERIAL')
main_lot.info = lot_info.get('INFO')
main_lot.full_info = True
machinery_type = None
if main_lot.group:
machinery_type = MachineryFilter.objects.filter(groups__title=main_lot.group.upper()).first()
main_lot.machinery_filter = machinery_type
main_lot.save()
return main_lot
def create_contact(name, phone, employee_id):
url = settings.BITRIX.replace('crm.lead.add.json', 'crm.contact.add.json')
params = {
'fields[ASSIGNED_BY_ID]': employee_id,
'fields[NAME]': name,
'fields[PHONE][0][VALUE]': phone,
'params[DATE_CREATE]': "Y",
}
try:
response = requests.get(url, params=params)
contact_id = int(response.json().get('result'))
except:
contact_id = None
return contact_id
def check_lead(lead_id):
prev_source_description = ''
prev_comment = ''
url = settings.BITRIX.replace('crm.lead.add.json', 'crm.lead.get.json')
params = {
'id': lead_id,
}
try:
response = requests.get(url, params=params)
lead_finish_date = response.json().get('result').get('DATE_CLOSED')
prev_source_description = response.json().get('result').get('SOURCE_DESCRIPTION')
prev_comment = response.json().get('result').get('COMMENTS')
except:
lead_finish_date = True
return lead_finish_date, prev_source_description, prev_comment
def update_lead(lead_id, source, comment):
url = settings.BITRIX.replace('crm.lead.add.json', 'crm.lead.update.json')
params = {
'id': lead_id,
'fields[SOURCE_DESCRIPTION]': source,
'fields[COMMENTS]': comment,
}
try:
response = requests.get(url, params=params)
except:
response = None
return response
def create_lead(name, phone, link, budget, time, users_comment, file):
bitrix_employee = BitrixEmployee.objects.filter(active=True).first()
if bitrix_employee:
employee_id = bitrix_employee.bitrix_id
bitrix_employee.save(update_fields=['updated_at'])
else:
employee_id = random.choice(settings.EMPLOYEES)
contact = Contact.objects.filter(phone=phone).first()
prev_source_description = ''
prev_comment = ''
if contact:
contact_id = contact.bitrix_id
lead = Lead.objects.filter(contact=contact).last()
lead_finish_date = True
if lead:
lead_finish_date, prev_source_description, prev_comment = check_lead(lead.bitrix_id)
else:
lead_finish_date = True
contact_id = create_contact(name, phone, employee_id)
if contact_id:
contact = Contact.objects.create(
phone=phone,
bitrix_id=contact_id,
)
comment = []
if budget:
comment.append(f'бюджет: {budget}')
if comment:
comment.append(f'комментарий: {users_comment}')
if file:
comment.append(f'прикрепленный файл: {file}')
if time:
comment.append(f'часовой пояс: {time}')
params = {
'fields[TITLE]': 'Заявка с сайта',
'fields[SOURCE_DESCRIPTION]': link,
'fields[ASSIGNED_BY_ID]': employee_id,
}
if contact_id:
params['fields[CONTACT_ID]'] = contact_id,
else:
comment.insert(0, f'имя: {name}')
params['fields[PHONE][0][VALUE]'] = phone
text_comment = ', '.join(comment)
params['fields[COMMENTS]'] = text_comment
if lead_finish_date:
try:
response = requests.get(settings.BITRIX, params=params)
lead_id = response.json().get('result')
if lead_id and contact:
Lead.objects.create(
contact=contact,
bitrix_id=lead_id,
)
except:
return 500
else:
if prev_source_description:
source_description = prev_source_description + f', {link}'
else:
source_description = link
if prev_comment:
curr_comment = prev_comment + f'\n{text_comment}'
else:
curr_comment = text_comment
response = update_lead(lead.bitrix_id, source_description, curr_comment)
if not response:
return 500
return response.status_code
def japan_brands_available(ip_address, truck_flag=False):
sql_request = f"select distinct(MARKA_ID) from main where (status LIKE '%25not%20sold%25' or status='') and (auction not like '%25uss%25')"
if not truck_flag:
sql_request += f' and YEAR>={settings.MIN_YEAR}'
sql_request = sql_request_for_api(sql_request)
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
brands = []
try:
start = 0
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
while len(response.json()) > 0:
for brand_id in response.json():
brands.append(brand_id['TAG0'])
start += 250
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
except:
pass
return brands
def japan_models_available(brand_id, ip_address, truck_flag=False):
sql_request = f"select distinct(MODEL_ID) from main where (status LIKE '%25not%20sold%25' or status='') and (auction not like '%25uss%25') and MARKA_ID={brand_id}"
if not truck_flag:
sql_request += f' and YEAR>={settings.MIN_YEAR}'
sql_request = sql_request_for_api(sql_request)
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
models = []
try:
start = 0
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
while len(response.json()) > 0:
for model_id in response.json():
models.append(model_id['TAG0'])
start += 250
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
except:
pass
return models
def korea_brands_available(ip_address):
sql_request = f"select distinct(MARKA_ID) from korea where (status LIKE '%25not sold%25' or status='') and YEAR>={settings.MIN_YEAR}"
sql_request = sql_request_for_api(sql_request)
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
brands = []
try:
start = 0
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
while len(response.json()) > 0:
for brand_id in response.json():
brands.append(brand_id['TAG0'])
start += 250
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
except:
pass
return brands
def korea_models_available(brand_id, ip_address):
sql_request = f"select distinct(MODEL_ID) from korea where (status LIKE '%25not sold%25' or status='') and MARKA_ID={brand_id} and YEAR>={settings.MIN_YEAR}"
sql_request = sql_request_for_api(sql_request)
AUCTION_ENDPOINT = settings.AUCTION_ENDPOINT
if ip_address:
AUCTION_ENDPOINT = AUCTION_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
models = []
try:
start = 0
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
while len(response.json()) > 0:
for model_id in response.json():
models.append(model_id['TAG0'])
start += 250
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(AUCTION_ENDPOINT + final_sql_request)
except:
pass
return models
def machinery_brands_available(ip_address):
sql_request = f"select distinct(MARKA_ID) from hdm where (status LIKE '%25not sold%25' or status='')"
sql_request = sql_request_for_api(sql_request)
brands = []
MACHINERY_ENDPOINT = settings.MACHINERY_ENDPOINT
if ip_address:
MACHINERY_ENDPOINT = MACHINERY_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
try:
start = 0
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(MACHINERY_ENDPOINT + final_sql_request)
while len(response.json()) > 0:
for brand_id in response.json():
brands.append(brand_id['TAG0'])
start += 250
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(MACHINERY_ENDPOINT + final_sql_request)
except:
pass
return brands
def machinery_models_available(brand_id, ip_address):
sql_request = f"select distinct(MODEL_NAME) from hdm where (status LIKE '%25not sold%25' or status='') and MARKA_ID={brand_id}"
sql_request = sql_request_for_api(sql_request)
MACHINERY_ENDPOINT = settings.MACHINERY_ENDPOINT
if ip_address:
MACHINERY_ENDPOINT = MACHINERY_ENDPOINT.replace('?json', f'?json&ip={ip_address}')
models = []
try:
start = 0
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(MACHINERY_ENDPOINT + final_sql_request)
while len(response.json()) > 0:
for model_id in response.json():
models.append(model_id['TAG0'].upper())
start += 250
final_sql_request = sql_request + f'%20limit%20{start},{start+250}'
response = requests.get(MACHINERY_ENDPOINT + final_sql_request)
except:
pass
return models