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.
114 lines
3.9 KiB
114 lines
3.9 KiB
# -*- coding: utf-8 -*-
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
import xlrd
|
|
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_ru.xls'
|
|
|
|
class Command(BaseCommand):
|
|
def handle(self, *args, **options):
|
|
|
|
f = open(PLACE_FILE, 'r')
|
|
book = xlrd.open_workbook(file_contents=f.read())
|
|
sheet = book.sheet_by_index(0)
|
|
row_list = [sheet.row_values(row_number) for row_number in range(sheet.nrows)]
|
|
labels = [label for label in row_list[0]]
|
|
existing = 0
|
|
|
|
for row_number, row in enumerate(row_list[1:]):
|
|
new = 0
|
|
# go through all rows in file
|
|
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')
|
|
new = 1
|
|
|
|
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
|
|
|
|
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')
|
|
|
|
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))
|
|
if field_name == 'city' and new == 0:
|
|
pass
|
|
else:
|
|
try:
|
|
setattr(object, field_name, value)
|
|
except ValueError, e:
|
|
print(value, field_name)
|
|
|
|
|
|
|
|
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 |