Fix locatioin

Add decorator to log
This commit is contained in:
littlewolf 2019-12-20 11:52:40 +03:00
parent 1c00cc7eae
commit 819e4366f7
3 changed files with 44 additions and 23 deletions

View File

@ -130,8 +130,6 @@ def transfer_addresses():
queryset = transfer_models.Locations.objects.raw("""SELECT locations.id, locations.zip_code, locations.longitude, queryset = transfer_models.Locations.objects.raw("""SELECT locations.id, locations.zip_code, locations.longitude,
locations.latitude, locations.address, locations.city_id locations.latitude, locations.address, locations.city_id
FROM locations WHERE FROM locations WHERE
locations.address != "" AND
locations.address IS NOT NULL AND
locations.city_id IS NOT NULL""") locations.city_id IS NOT NULL""")
queryset = [vars(query) for query in queryset] queryset = [vars(query) for query in queryset]
@ -734,7 +732,7 @@ data_types = {
# transfer_countries, # transfer_countries,
# transfer_regions, # transfer_regions,
# transfer_cities, # transfer_cities,
# transfer_addresses, transfer_addresses,
transfer_wine_region, transfer_wine_region,
transfer_wine_sub_region, transfer_wine_sub_region,
transfer_wine_village, transfer_wine_village,

View File

@ -189,7 +189,7 @@ class AddressSerializer(serializers.ModelSerializer):
zip_code = serializers.CharField(allow_null=True, allow_blank=True) zip_code = serializers.CharField(allow_null=True, allow_blank=True)
latitude = serializers.DecimalField(max_digits=10, decimal_places=6, allow_null=True) latitude = serializers.DecimalField(max_digits=10, decimal_places=6, allow_null=True)
longitude = serializers.DecimalField(max_digits=10, decimal_places=6, allow_null=True) longitude = serializers.DecimalField(max_digits=10, decimal_places=6, allow_null=True)
address = serializers.CharField() address = serializers.CharField(allow_null=True, allow_blank=True)
class Meta: class Meta:
model = models.Address model = models.Address
@ -234,25 +234,29 @@ class AddressSerializer(serializers.ModelSerializer):
return data return data
def set_address(self, data): def set_address(self, data):
address_list = data.pop('address').split(' ') if "address" in data and data['address'] is not None and data['address'] != "":
is_first_street = False address_list = data.pop('address').split(' ')
data['street_name_1'] = [] is_first_street = False
data['street_name_2'] = [] data['street_name_1'] = []
while len(address_list) > 0: data['street_name_2'] = []
address_part = address_list.pop() while len(address_list) > 0:
try: address_part = address_list.pop()
address_part = int(address_part) try:
data['number'] = address_part address_part = int(address_part)
is_first_street = True data['number'] = address_part
except: is_first_street = True
if is_first_street: except:
data['street_name_1'].append(address_part) if is_first_street:
else: data['street_name_1'].append(address_part)
data['street_name_2'].append(address_part) else:
data['street_name_2'].append(address_part)
data['street_name_1'] = " ".join(data['street_name_1']) data['street_name_1'] = " ".join(data['street_name_1'])
data['street_name_2'] = " ".join(data['street_name_2']) data['street_name_2'] = " ".join(data['street_name_2'])
if "number" not in data: if "number" not in data:
data['number'] = 0
else:
del(data['address'])
data['number'] = 0 data['number'] = 0
return data return data

View File

@ -1,13 +1,17 @@
from os.path import exists from os.path import exists
from os import makedirs
from importlib.machinery import SourceFileLoader from importlib.machinery import SourceFileLoader
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
from django.db.models import Count, Q from django.db.models import Count, Q
from tqdm import tqdm from tqdm import tqdm
import sys
import timeit
def transfer_objects(data_type): def transfer_objects(data_type):
start_t = timeit.default_timer()
for app in apps.get_app_configs(): for app in apps.get_app_configs():
if exists(f"{app.path}/transfer_data.py"): if exists(f"{app.path}/transfer_data.py"):
card_module = SourceFileLoader("transfer", f"{app.path}/transfer_data.py").load_module() card_module = SourceFileLoader("transfer", f"{app.path}/transfer_data.py").load_module()
@ -20,7 +24,10 @@ def transfer_objects(data_type):
if data_type == module_data_type: if data_type == module_data_type:
for transfer_func in transfer_funcs: for transfer_func in transfer_funcs:
print(f"========================== FUNCTION {transfer_func.__name__} ================================") print(f"========================== FUNCTION {transfer_func.__name__} ================================")
transfer_func = file_log(transfer_func)
transfer_func() transfer_func()
end_t = timeit.default_timer()
print(f"Transfer time: {end_t - start_t}")
def clean_old_records(model, conditions): def clean_old_records(model, conditions):
@ -166,3 +173,15 @@ def clean_old_region_records(model, conditions):
counter = len(to_delete) counter = len(to_delete)
old_records.filter(id__in=to_delete).delete() old_records.filter(id__in=to_delete).delete()
print(f'Deleted {counter} objects.') print(f'Deleted {counter} objects.')
def file_log(f):
directory = f"{settings.PROJECT_ROOT}/apps/transfer/log"
if not exists(directory):
makedirs(directory)
sys.stdout = open(f"{directory}/{f.__name__}.log","w+")
def res_func(*args, **kwargs):
return f(*args, **kwargs)
return res_func