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.
 
 
 
 
 
 

64 lines
2.4 KiB

# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError
from city.models import City
from country.models import Country
from place_exposition.models import PlaceExposition
import xlrd, xlwt
from import_xls.excel_settings import place_exp_sett
from django.conf import settings
class Command(BaseCommand):
def handle(self, *args, **options):
f = open(settings.MEDIA_ROOT+'/import/places_en.xlsx', '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]]
for row_number, row in enumerate(row_list):
# go through all rows in file
if row_number > 0:
# first field is label
if row[0] != '':
# in first column ids
try:
object = PlaceExposition.objects.language('en').get(id=int(row[0]))
except:
continue
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']
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, 'en', getattr(object, extra_value))
else:
value = func(cell)
#if field_name =='adress':
# setattr(object, 'address', google_address(value))
setattr(object, field_name, value)
print(object.description)
object.save()
print('post save %s'% str(object))