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.
 
 
 
 
 
 

396 lines
14 KiB

# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError
from city.models import City
from country.models import Country
from exposition.models import Exposition
import xlrd, xlwt
#from import_xls.excel_settings import event_sett
from django.conf import settings
from import_xls.import_forms import google_address
def to_int(val):
"""
Reverse function to get_int
return None if value isnt integer
"""
try:
return int(val)
except ValueError:
return None
def to_country(value):
try:
#query = get_translation_aware_manager(Country)
#country = query.filter(name=value)[0]
country = Country.objects.filter(translations__name=value.strip())[0]
return country
except IndexError:
return None
def to_city(value, lang, country):
try:
# get city by name
#objects = get_translation_aware_manager(City)
# except IndexError if no found
city = City.objects.filter(translations__name=value.strip(), country=country)[0]
return city
except IndexError:
return None
from theme.models import Theme, Tag
def to_theme(obj, value):
if isinstance(value, float) or isinstance(value, int):
if (value - int(value) > 0):
value = str(value)
else:
value = str(int(value))
theme_ids = value.split('.')
else:
theme_ids = value.split(',')
theme_objects = []
for id in theme_ids:
try:
theme = Theme.objects.language('ru').get(id=int(id))
theme_objects.append(theme)
obj.theme.add(theme)
except Theme.DoesNotExist, ValueError:
pass
return theme_objects
from hvad.utils import get_translation_aware_manager
def to_tag(obj,value):
if value == [""]:
return None
names = value.split(',')
tags = []
for name in names:
objects = get_translation_aware_manager(Tag)
tag = objects.filter(name=name)
#tag = Tag.objects.language('ru').filter(transations__name=name)
if tag:
tags.append(tag[0])
obj.tag.add(tag[0])
else:
continue
return tags
import time
def to_date(value):
if not value:
return None
if isinstance(value, unicode) or isinstance(value, str):
t = time.strptime(value, "%d.%m.%Y")
if isinstance(value, float):
t = xlrd.xldate_as_tuple(value, 0)+(0,0,0)
return time.strftime("%Y-%m-%d", t)
def to_currency(value):
if value=='USD':
return value
if value=='EUR':
return value
if value=='RUB':
return 'RUB'
return 'USD'
from django.core.validators import validate_email, URLValidator
def to_url(url):
validate = URLValidator()
try:
validate(url)
except:
return ''
return url
def to_email(email):
try:
validate_email(email)
except:
return ''
return email
from file.models import FileModel
import urllib2
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from functions.files import get_alternative_filename
def save_logo(obj, value, purpose):
"""
if not obj.id:
return None
"""
urls = value.split(';')
for url in urls:
if url[0]!=('/'):
url = '/'+url
url = 'http://www.expomap.ru'+url
file_name = url.split('/')[-1]
alt_name = get_alternative_filename(settings.MEDIA_ROOT+'imgs/', file_name)
download_to = settings.MEDIA_ROOT+'imgs/'+alt_name
try:
response = urllib2.urlopen(url, timeout=3)
except:
continue
with open(download_to,'wb') as f:
f.write(response.read())
f.close()
file_name ='imgs/'+alt_name
content_type = ContentType.objects.get_for_model(obj)
file = FileModel(file_path=file_name, file_type='JPG', purpose=purpose,
content_type=content_type, object_id=obj.id)
file.save()
from place_exposition.models import EXPOSITION_TYPE
def to_type(value):
for t in EXPOSITION_TYPE:
if value == t[1]:
return t[0]
return 'Exposition complex'
def to_phone(value):
if value:
if isinstance(value, float) or isinstance(value, int):
return value
else:
deduct = ('-','(',')','.',' ', '+')
for elem in deduct:
value = value.replace(elem, '')
value = to_int(value)
return value
from place_exposition.models import PlaceExposition
def to_place(value):
try:
place = PlaceExposition.objects.get(id=value)
return place
except:
return None
from exposition.models import BIT_AUDIENCE
def to_audience(value, model=Exposition):
if value:
l = value.split(', ')
aud = {'Trade visitors':'experts', 'Trade visitors and Public':'experts and consumers',
'General public': 'general public'}
if l:
new_list = []
for value in l:
for item1, item2 in BIT_AUDIENCE:
if aud.get(value) == item2:
new_list.append(item1)
if new_list:
return reduce(lambda x,y: x|y, (getattr(model.audience, item) for item in new_list))
return 0
def to_periodic(value):
per = {'Оnce a year': 1.0, 'Every 2 years':0.5, 'Twice a year':2.0}
return per.get(value, 0)
from organiser.models import Organiser
def to_organiser(obj, value):
org = Organiser.objects.filter(translations__name=value)
if org:
obj.organiser.add(org[0])
else:
org = Organiser()
org.translate('ru')
org.name = value
org.save()
event_sett = {
u'ID':{u'field': u'id', u'func': to_int},
u'Название':{u'field': u'name', u'func': unicode},
u'Краткое описание':{u'field': u'main_title', u'func': unicode},
u'Дата начала:(YYYY-MM-DD)':{u'field': u'data_begin', u'func': to_date},
u'Дата окончания:(YYYY-MM-DD)':{u'field': u'data_end', u'func': to_date},
u'Страна':{u'field': u'country', u'func': to_country},
u'Город':{u'field': u'city', u'func': to_city, 'extra_values': 'country'},
u'Место проведения':{u'field': u'place', u'func': to_place},#####
u'ID Тематики':{u'field': u'theme', u'func': to_theme, u'method': True},
u'Теги':{u'field': u'tag', u'func': to_tag, u'method': True},
u'Организатор №1':{u'field': u'organiser', u'func': to_organiser, u'method': True},####
u'Организатор №2':{u'field': u'organiser', u'func': to_organiser, u'method': True},####
u'Описание события':{u'field': u'description', u'func': unicode},
u'Периодичность':{u'field': u'periodic', u'func': to_periodic},
u'Аудитория':{u'field': u'audience', u'func': to_audience},
u'Логотип':{u'field': u'logo', u'func': save_logo, u'method': True, u'purpose': 'logo'},
u'Официальный веб-сайт':{u'field': u'web_page', u'func': to_url},
u'Экспонируемые продукты':{u'field': u'products', u'func': unicode},
u'Время работы':{u'field': u'time', u'func': unicode},
u'Валюта':{u'field': u'currency', u'func': to_currency},
#
u'Посетители_билет (1 день)':{u'field': u'price_day', u'func': to_int},
u'Посетители_билет (все дни)':{u'field': u'price_all', u'func': to_int},
#u'Условия':{u'field': u'periodic', u'func': unicode},
u'Посетители_билет (1 день)(на стойке)':{u'field': u'price_day_bar', u'func': to_int},##??
u'Посетители_билет (все дни)(на стойке)':{u'field': u'price_all_bar', u'func': to_int},##??
#u'Условия':{u'field': u'periodic', u'func': unicode},##??
#u'Примечание':{u'field': u'periodic', u'func': unicode},
u'Каталог':{u'field': u'price_catalog', u'func': to_int},
u'Налог включен':{u'field': u'tax', u'func': bool},
u'Год основания':{u'field': u'foundation_year', u'func': to_int},
#u'Данные за год':{u'field': u'periodic', u'func': to_int},
u'Посетители':{u'field': u'visitors', u'func': to_int},
u'Участники':{u'field': u'members', u'func': to_int},
#u'Страны':{u'field': u'periodic', u'func': unicode},##??
u'Площадь':{u'field': u'periodic', u'func': to_int},
u'Min_Raw кв.м.':{u'field': u'min_closed_area', u'func': to_int},
u'Max_Raw кв.м.':{u'field': u'max_closed_area', u'func': to_int},
u'Min_Pack кв.м.':{u'field': u'min_closed_equipped_area', u'func': to_int},
u'Max_Pack кв.м.':{u'field': u'max_closed_equipped_area', u'func': to_int},
u'Открытая площадь':{u'field': u'max_open_area', u'func': to_int},
u'Мин. Площадь кв.м.':{u'field': u'min_open_area', u'func': to_int},
u'Регистрационный взнос':{u'field': u'registration_payment', u'func': to_int},
#u'Примечание':{u'field': u'periodic', u'func': unicode},
u'Крайний срок подачи заявки':{u'field': u'application_deadline', u'func': to_date},
u'UFI':{u'field': u'quality_label', u'func': bool, },##
u'РСВЯ':{u'field': u'quality_label', u'func': bool},##
u'EXPORATING':{u'field': u'quality_label', u'func': bool},##
u'Отменена':{u'field': u'canceled', u'func': bool},
u'ExpoHIT':{u'field': u'expohit', u'func': bool},
#u'Фото':{u'field': u'photo', u'func': save_photo, u'method': True},
}
class Command(BaseCommand):
def handle(self, *args, **options):
f = open(settings.MEDIA_ROOT+'/import/Places Eng Upd.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]]
print(123)
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 = Exposition.objects.language('en').get(id=int(row[0]))
except ValueError:
object = Exposition()
object.translate('en')
except Exposition.DoesNotExist:
object = Exposition(id= int(row[0]))
object.translate('en')
else:
# if id blank - its a new place
object = Exposition()
object.translate('en')
methods = []
flag = False
for col_number, cell in enumerate(row):
# go through row cells
# field name current cell
label = labels[col_number]
setting = event_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, 'en', getattr(object, extra_value))
else:
value = func(cell)
#if field_name =='adress':
# setattr(object, 'address', google_address(value))
if field_name=='city' and not value:
print('error city')
flag = True
continue
if not field_name=='quality_label':
setattr(object, field_name, value)
else:
if value:
if setting.get('l') == 'ufi':
object.quality_label.ufi = True
if setting.get('l') == 'rsva':
object.quality_label.rsva = True
if setting.get('l') == 'exporating':
object.quality_label.exporating = True
if not flag:
object.save()
"""
try:
print('pre save %s'% str(object))
object.save()
except:
print('saving error')
continue
"""
print('post save %s'% str(object))
else:
print('bad city')
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