# -*- coding: utf-8 -*- from accounts.models import User from country.models import Country from city.models import City from theme.models import Theme, Tag from hvad.utils import get_translation_aware_manager def get_bool(value): if value: return 1 return '' def get_int(value): if not value: return '' return value from bitfield import BitHandler from exposition.models import BIT_AUDIENCE def get_audience(value): if isinstance(value, BitHandler): l = [k for k, v in value.iteritems() if v] if l: new_list = [] for value in l: for item1, item2 in BIT_AUDIENCE: if value == item1: new_list.append(item2) return ', '.join(new_list) return def get_theme(value): objects = value.all() return ','.join(str(obj.id) for obj in objects) def get_tag(tag, theme): tags = tag.all() themes = theme.all() result = '' for theme in themes: # list of tags with current theme tag_list = [str(tag.id) for tag in tags if tag.theme == theme] result += '['+','.join(tag_list)+']' return result from place_exposition.models import EXPOSITION_TYPE def get_place_type(value): for t in EXPOSITION_TYPE: if value == t[0]: return t[1] return t[0][1] place_settings=[ {'name': 'id', 'verbose_name': u'id', 'type': get_int, 'width':1500}, {'name': 'name', 'verbose_name': u'Название', 'type': unicode}, {'name': 'type', 'verbose_name': u'Тип', 'type': get_place_type},########## {'name': 'main_title', 'verbose_name': u'Краткое описание', 'type': unicode}, {'name': 'country', 'verbose_name': u'Страна', 'type': unicode}, {'name': 'city', 'verbose_name': u'Город', 'type': unicode}, {'name': 'address', 'verbose_name': u'Адресс', 'type': unicode}, {'name': 'phone', 'verbose_name': u'Тел.', 'type': get_int}, {'name': 'fax', 'verbose_name': u'Факс', 'type': get_int}, {'name': 'web_page', 'verbose_name': u'Веб-сайт', 'type': unicode}, {'name': 'email', 'verbose_name': u'Email', 'type': unicode}, {'name': 'virtual_tour', 'verbose_name': u'Виртуальный тур', 'type': unicode}, {'name': 'virtual_tour', 'verbose_name': u'Виртуальный тур', 'type': unicode}, {'name': 'foundation_year', 'verbose_name': u'Год основания', 'type': get_int}, {'name': 'event_in_year', 'verbose_name': u'Количество мероприятий в год', 'type': get_int}, {'name': 'total_area', 'verbose_name': u'Общая выставочная площадь, кв. м.', 'type': get_int}, {'name': 'closed_area', 'verbose_name': u'Закрытая выставочная площадь, кв. м.', 'type': get_int}, {'name': 'open_area', 'verbose_name': u'Открытая выставочная площадь, кв. м.', 'type': get_int}, {'name': 'total_pavilions', 'verbose_name': u'Количество павильонов', 'type': get_int}, {'name': 'total_halls', 'verbose_name': u'Конференц-залы', 'type': get_int}, {'name': 'bank', 'verbose_name': u'Банк/Банкоматы/Обмен валюты', 'type': get_bool}, {'name': 'children_room', 'verbose_name': u'Детская комната', 'type': get_bool}, {'name': 'disabled_service', 'verbose_name': u'Сервис для людей с ограниченными физическими возможностями', 'type': get_bool}, {'name': 'conference_centre', 'verbose_name': u'Конгресс-центр', 'type': get_bool}, {'name': 'business_centre', 'verbose_name': u'Бизнес-центр', 'type': get_bool}, {'name': 'online_registration', 'verbose_name': u'On-line регистрация', 'type': get_bool}, {'name': 'wifi', 'verbose_name': u'Wi-Fi', 'type': get_bool}, {'name': 'cafe', 'verbose_name': u'Кафе и рестораны', 'type': get_bool}, {'name': 'terminals', 'verbose_name': u'Информационные терминалы', 'type': get_bool}, {'name': 'parking', 'verbose_name': u'Парковка', 'type': get_bool}, {'name': 'press_centre', 'verbose_name': u'Пресс-центр', 'type': get_bool}, {'name': 'mobile_application', 'verbose_name': u'Мобильное приложение', 'type': get_bool}, ] field_settings = [ {'name': 'id', 'verbose_name': u'id', 'type': get_int, 'width':1500}, {'name': 'url', 'verbose_name': u'url', 'type': unicode}, {'name': 'name', 'verbose_name': u'Название', 'type': unicode}, #{'name': 'type', 'verbose_name': u'Тип', 'type': get_place_type, 'width':8000}, {'name': 'data_begin', 'verbose_name': u'Дата начала', 'type': unicode}, {'name': 'data_end', 'verbose_name': u'Дата окончания', 'type': unicode}, {'name': 'audience', 'verbose_name': u'Аудитория', 'type': get_audience}, {'name': 'main_title', 'verbose_name': u'Краткое описание', 'type': unicode}, {'name': 'description', 'verbose_name': u'Описание', 'type': unicode}, {'name': 'country', 'verbose_name': u'Страна', 'type': unicode}, {'name': 'city', 'verbose_name': u'Город', 'type': unicode}, {'name': 'theme', 'verbose_name': u'Тематика', 'type': get_theme, 'width':8000}, {'name': 'tag', 'verbose_name': u'Теги', 'type': get_tag, 'width':8000}, {'name': 'address', 'verbose_name': u'Адрес', 'type': unicode}, #{'name': 'periodic', 'verbose_name': 'Периодичность', 'type': str}, {'name': 'web_page', 'verbose_name': u'Веб страница', 'type': unicode}, {'name': 'email', 'verbose_name': u'Email', 'type': unicode}, {'name': 'phone', 'verbose_name': u'Телефон', 'type': get_int}, {'name': 'time', 'verbose_name': u'Время проведения', 'type': unicode}, {'name': 'products', 'verbose_name': u'Экспонируемые продукты', 'type': unicode}, {'name': 'foundation', 'verbose_name': u'Год основания', 'type': get_int}, {'name': 'events_number', 'verbose_name': u'Год основания', 'type': get_int}, {'name': 'staff_number', 'verbose_name': u'Год основания', 'type': get_int}, {'name': 'specialization', 'verbose_name': u'Год основания', 'type': unicode}, {'name': 'foundation_year', 'verbose_name': u'Год основания', 'type': get_int}, {'name': 'tax', 'verbose_name': u'Налог включен', 'type': get_bool, 'width':1000}, {'name': 'currency', 'verbose_name': u'Валюта', 'type': unicode}, {'name': 'max_price', 'verbose_name': u'Максимальная цена', 'type': get_int}, {'name': 'min_price', 'verbose_name': u'Минимальная цена', 'type': get_int}, {'name': 'registration_payment', 'verbose_name': u'Регистрационный взнос', 'type': get_int}, {'name': 'min_closed_area', 'verbose_name': u'Минимальная цена закрытой НЕ оборудованной площади', 'type': get_int}, {'name': 'max_closed_area', 'verbose_name': u'Максимальная цена закрытой НЕ оборудованной площади', 'type': get_int}, {'name': 'min_closed_equipped_area', 'verbose_name': u'Минимальная цена закрытой оборудованной площади ', 'type': get_int}, {'name': 'max_closed_equipped_area', 'verbose_name': u'Максимальная цена закрытой оборудованной площади', 'type': get_int}, {'name': 'min_open_area', 'verbose_name': u'Минимальная цена закрытой площади', 'type': get_int}, {'name': 'max_open_area', 'verbose_name': u'Максимальная цена открытой площади', 'type': get_int}, {'name': 'min_area', 'verbose_name': u'Минимальная площадь', 'type': get_int}, {'name': 'max_area', 'verbose_name': u'Максимальная площадь', 'type': get_int}, {'name': 'is_published', 'verbose_name': u'Опубликована', 'type': get_bool}, {'name': 'canceled_by_administrator', 'verbose_name': u'Отменена администратором', 'type': get_bool} ] 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] 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 = objects.filter(name=value)[0] return city except IndexError: return None # not found # try to create new city #city = City(country=country) #city.translate(lang) #city.name = value #city.save() return city from exposition.models import Exposition def to_audience(value, model=Exposition): if value: l = value.split(', ') if l: new_list = [] for value in l: for item1, item2 in BIT_AUDIENCE: if 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_theme(value): if isinstance(value, float): 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().get(id=int(id)) theme_objects.append(theme) except Theme.DoesNotExist, ValueError: pass return theme_objects def to_tag(value, lang, themes): if value == [""]: return None value = value.replace('[', '') themes = themes.all() # list tags by themes value = value.split(']') arr = [] for theme_number, theme in enumerate(themes): for val in value[theme_number].split(','): if val: try: tag = Tag.objects.language(lang).get(id=int(val), theme=theme) arr.append(tag) except Tag.DoesNotExist, ValueError: pass return arr def to_theme_type(st): if not st: return 15 types = st.split(',') flag = 0 flag = reduce(lambda x,y: x|y, (getattr(Theme.types, item) for item in types)) return flag import time, xlrd def to_date(value): if isinstance(value, unicode): 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_logo(url): return None def to_photo(url): return None def to_map(url): return None 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_file(obj, value, purpose): if not obj.id: return None urls = value.split(';') for url in urls: 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 file.models import Photo def save_photo(obj, value): if not obj.id: return None urls = value.split(';') for url in urls: file_name = url.split('/')[-1] alt_name = get_alternative_filename(settings.MEDIA_ROOT+'imgs/', file_name) download_to = settings.MEDIA_ROOT+'photos/'+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 ='photos/'+alt_name content_type = ContentType.objects.get_for_model(obj) photo = Photo(file_path=file_name, file_type='JPG', content_type=content_type, object_id=obj.id) photo.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): deduct = ('-','(',')','.',' ', '+') for elem in deduct: value = value.replace(elem, '') value = to_int(value) return value from place_exposition.models import Hall def save_halls(obj, value): halls = value.split(';') res = [] for hall in halls: if hall: area = None number = None name = '' l = hall.split(u'–') if len(l)>1: area = int(l[1].strip().replace(' ', '')) l = l[0].split(u'№') if len(l)>1: try: number = int(l[1].strip()) except ValueError: pass name = l[0].strip() else: name = l[0].strip() else: name = l[0].strip() res.append({'area': area, 'name':name, 'number': number}) for hall in res: h = Hall(place_exposition = obj, name=hall.get('name'), number=hall.get('number'), capacity=hall.get('area')) h.save() place_exp_sett = { u'ID':{u'field': u'id', u'func': to_int}, u'Название':{u'field': u'name', u'func': unicode}, u'Тип':{u'field': u'type', u'func': to_type}, u'Краткое описание':{u'field': u'main_title', u'func': unicode}, 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'description', u'func': unicode}, u'Адрес':{u'field': u'adress', u'func': unicode}, u'Тел.':{u'field': u'phone', u'func': to_phone}, u'Факс':{u'field': u'fax', u'func': to_phone}, u'Фото':{u'field': u'photo', u'func': save_photo, u'method': True},#сделать u'Лого':{u'field': u'logo', u'func': save_file, u'method': True, u'purpose': 'logo'},#сделать u'Веб-сайт':{u'field': u'web_page', u'func': unicode}, u'Email':{u'field': u'email', u'func': unicode}, u'Карта проезда':{u'field': u'map', u'func': save_file, u'method': True, u'purpose': 'map'},#сделать u'Виртуальный тур':{u'field': u'virtual_tour', u'func': to_url}, u'Год основания':{u'field': u'foundation_year', u'func': to_int}, u'Количество мероприятий в год':{u'field': u'event_in_year', u'func': to_int}, u'Общая выставочная площадь, кв. м.':{u'field': u'total_area', u'func': to_int}, u'Закрытая выставочная площадь, кв. м.':{u'field': u'closed_area', u'func': to_int}, u'Открытая выставочная площадь, кв. м.':{u'field': u'open_area', u'func': to_int}, u'Количество павильонов':{u'field': u'total_pavilions', u'func': to_int}, u'Площадь павильонов/залов (по номерам)':{u'field': u'halls', u'func': save_halls, u'method': True},#!!! u'Конференц-залы':{u'field': u'total_halls', u'func': to_int}, u'Схема территории':{u'field': u'scheme', u'func': save_file, u'method': True, u'purpose': 'scheme teritory'},#сделать u'Банк/Банкоматы/Обмен валюты':{u'field': u'bank', u'func': bool}, u'Детская комната':{u'field': u'children_room', u'func': bool}, u'Сервис для людей с ограниченными физическими возможностями':{u'field': u'disabled_service', u'func': bool}, u'Конгресс-центр':{u'field': u'conference_centre', u'func': bool}, u'Бизнес-центр':{u'field': u'business_centre', u'func': bool}, u'On-line регистрация':{u'field': u'online_registration', u'func': bool}, u'Wi-Fi':{u'field': u'wifi', u'func': bool}, u'Кафе и рестораны':{u'field': u'cafe', u'func': bool}, u'Информационные терминалы':{u'field': u'terminals', u'func': bool}, u'Парковка':{u'field': u'parking', u'func': bool}, u'Пресс-центр':{u'field': u'press_centre', u'func': bool}, u'Мобильное приложение':{u'field': u'mobile_application', u'func': bool}, } import_settings={ 'name': {'func': unicode}, 'url': {'func': unicode}, 'data_begin': {'func': to_date}, 'data_end': {'func': to_date}, 'main_title': {'func': unicode}, 'description': {'func': unicode}, 'audience': {'func': to_audience}, 'country': {'func': to_country}, 'city': {'func': to_city, 'extra_values': 'country'}, 'theme': {'func': to_theme}, 'tag': {'func': to_tag, 'extra_values': 'theme'}, 'periodic': {'func': unicode}, 'web_page': {'func': unicode}, 'time': {'func': unicode}, 'products': {'func': unicode}, 'foundation_year': {'func': to_int}, 'tax': {'func': bool}, 'currency': {'func': unicode}, 'max_price': {'func': to_int}, 'min_price': {'func': to_int}, 'registration_payment': {'func': to_int}, 'min_closed_area': {'func': to_int}, 'max_closed_area': {'func': to_int}, 'min_closed_equipped_area': {'func': to_int}, 'max_closed)equipped_area': {'func': to_int}, 'min_open_area': {'func': to_int}, 'max_open_area': {'func': to_int}, 'min_area': {'func': to_int}, 'max_area': {'func': to_int}, 'is_published': {'func': bool}, 'canceled_by_administrator': {'func': bool}, 'types': {'func': to_theme_type} }