|
|
|
|
@ -5,7 +5,7 @@ from place_exposition.models import PlaceExposition |
|
|
|
|
from import_xls.excel_settings import import_settings, place_exp_sett |
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
PLACE_FILE = settings.MEDIA_ROOT+'/import/places.xlsx' |
|
|
|
|
PLACE_FILE = settings.MEDIA_ROOT+'import/places_ru.xls' |
|
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
|
|
def handle(self, *args, **options): |
|
|
|
|
@ -17,90 +17,98 @@ class Command(BaseCommand): |
|
|
|
|
labels = [label for label in row_list[0]] |
|
|
|
|
existing = 0 |
|
|
|
|
|
|
|
|
|
for row_number, row in enumerate(row_list): |
|
|
|
|
for row_number, row in enumerate(row_list[1:]): |
|
|
|
|
new = 0 |
|
|
|
|
# go through all rows in file |
|
|
|
|
if row_number > 0: |
|
|
|
|
# first field is label |
|
|
|
|
if row[0] != '': |
|
|
|
|
# in first column ids |
|
|
|
|
if row[0] != '': |
|
|
|
|
# in first column ids |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
object = PlaceExposition.objects.language('ru').get(id=int(row[0])) |
|
|
|
|
existing += 1 |
|
|
|
|
except ValueError: |
|
|
|
|
object = PlaceExposition() |
|
|
|
|
object.translate('ru') |
|
|
|
|
|
|
|
|
|
except PlaceExposition.DoesNotExist: |
|
|
|
|
object = PlaceExposition(id= int(row[0])) |
|
|
|
|
object.translate('ru') |
|
|
|
|
existing += 1 |
|
|
|
|
else: |
|
|
|
|
# if id blank - its a new place |
|
|
|
|
object = PlaceExposition |
|
|
|
|
try: |
|
|
|
|
object = PlaceExposition.objects.language('ru').get(id=int(row[0])) |
|
|
|
|
existing += 1 |
|
|
|
|
except ValueError: |
|
|
|
|
object = PlaceExposition() |
|
|
|
|
object.translate('ru') |
|
|
|
|
methods = [] |
|
|
|
|
for col_number, cell in enumerate(row): |
|
|
|
|
# go through row cells |
|
|
|
|
# field name current cell |
|
|
|
|
label = labels[col_number] |
|
|
|
|
setting = place_exp_sett.get(label) |
|
|
|
|
|
|
|
|
|
if setting is None: |
|
|
|
|
continue |
|
|
|
|
new = 1 |
|
|
|
|
|
|
|
|
|
if setting.get('method'): |
|
|
|
|
if cell != "": |
|
|
|
|
methods.append({'func': setting['func'], 'value': cell, 'purpose': setting.get('purpose')}) |
|
|
|
|
continue |
|
|
|
|
except PlaceExposition.DoesNotExist: |
|
|
|
|
object = PlaceExposition(id= int(row[0])) |
|
|
|
|
object.translate('ru') |
|
|
|
|
existing += 1 |
|
|
|
|
new = 1 |
|
|
|
|
else: |
|
|
|
|
# if id blank - its a new place |
|
|
|
|
object = PlaceExposition |
|
|
|
|
object.translate('ru') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
methods = [] |
|
|
|
|
for col_number, cell in enumerate(row): |
|
|
|
|
# go through row cells |
|
|
|
|
# field name current cell |
|
|
|
|
label = labels[col_number] |
|
|
|
|
setting = place_exp_sett.get(label) |
|
|
|
|
|
|
|
|
|
if setting is None: |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
field_name = setting['field'] |
|
|
|
|
if setting.get('method'): |
|
|
|
|
if cell != "": |
|
|
|
|
methods.append({'func': setting['func'], 'value': cell, 'purpose': setting.get('purpose')}) |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
field_name = setting['field'] |
|
|
|
|
func = setting.get('func') |
|
|
|
|
|
|
|
|
|
func = setting.get('func') |
|
|
|
|
if func is not None: |
|
|
|
|
extra_value = setting.get('extra_values') |
|
|
|
|
if extra_value is not None: |
|
|
|
|
# if setting has extra value then |
|
|
|
|
# it is some field like city, theme, tag |
|
|
|
|
# that has relation and can be created |
|
|
|
|
if func is not None: |
|
|
|
|
extra_value = setting.get('extra_values') |
|
|
|
|
if extra_value is not None: |
|
|
|
|
# if setting has extra value then |
|
|
|
|
# it is some field like city, theme, tag |
|
|
|
|
# that has relation and can be created |
|
|
|
|
|
|
|
|
|
# in function we add language(need for relation fields) |
|
|
|
|
# and extra value from object (like for city need country) |
|
|
|
|
value = func(cell, 'ru', getattr(object, extra_value)) |
|
|
|
|
else: |
|
|
|
|
value = func(cell) |
|
|
|
|
#if field_name =='adress': |
|
|
|
|
# setattr(object, 'address', google_address(value)) |
|
|
|
|
setattr(object, field_name, value) |
|
|
|
|
# in function we add language(need for relation fields) |
|
|
|
|
# and extra value from object (like for city need country) |
|
|
|
|
value = func(cell, 'ru', getattr(object, extra_value)) |
|
|
|
|
|
|
|
|
|
#object.save() |
|
|
|
|
print('post save %s'% str(object)) |
|
|
|
|
""" |
|
|
|
|
try: |
|
|
|
|
print(object) |
|
|
|
|
#object.save() |
|
|
|
|
|
|
|
|
|
except IntegrityError: |
|
|
|
|
continue |
|
|
|
|
#url = object.url + translit_with_separator(object.city.name) |
|
|
|
|
#object.url = url |
|
|
|
|
#object.save() |
|
|
|
|
""" |
|
|
|
|
""" |
|
|
|
|
for method in methods: |
|
|
|
|
func = method['func'] |
|
|
|
|
if method.get('purpose'): |
|
|
|
|
try: |
|
|
|
|
func(object, method['value'], method['purpose']) |
|
|
|
|
except: |
|
|
|
|
continue |
|
|
|
|
else: |
|
|
|
|
value = func(cell) |
|
|
|
|
#if field_name =='adress': |
|
|
|
|
# setattr(object, 'address', google_address(value)) |
|
|
|
|
if field_name == 'city' and new == 0: |
|
|
|
|
pass |
|
|
|
|
else: |
|
|
|
|
try: |
|
|
|
|
func(object, method['value']) |
|
|
|
|
except: |
|
|
|
|
continue |
|
|
|
|
setattr(object, field_name, value) |
|
|
|
|
except ValueError, e: |
|
|
|
|
print(value, field_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
print(existing) |
|
|
|
|
|
|
|
|
|
object.save() |
|
|
|
|
print('post save %s'% str(object)) |
|
|
|
|
""" |
|
|
|
|
try: |
|
|
|
|
print(object) |
|
|
|
|
#object.save() |
|
|
|
|
|
|
|
|
|
except IntegrityError: |
|
|
|
|
continue |
|
|
|
|
#url = object.url + translit_with_separator(object.city.name) |
|
|
|
|
#object.url = url |
|
|
|
|
#object.save() |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
for method in methods: |
|
|
|
|
func = method['func'] |
|
|
|
|
if method.get('purpose'): |
|
|
|
|
try: |
|
|
|
|
func(object, method['value'], method['purpose']) |
|
|
|
|
except: |
|
|
|
|
continue |
|
|
|
|
else: |
|
|
|
|
try: |
|
|
|
|
func(object, method['value']) |
|
|
|
|
except: |
|
|
|
|
continue |