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.
 
 
 
 
 
 

217 lines
7.7 KiB

# -*- coding: utf-8 -*-
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 0
def get_int(value):
if not value:
return ''
return value
def get_audience(value):
if not value:
return 'Не выбрано'
elif value == 'experts':
return 'Специалисты'
elif value == 'experts and consumers':
return 'Специалисты и потребители'
elif value == 'general public':
return 'Широкая публика'
else:
return ''
def get_theme(value):
objects = value.all()
return ', '.join(str(obj) 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) for tag in tags if tag.theme == theme]
result += '['+', '.join(tag_list)+']'
return result
field_settings = [
{'name': 'id', 'verbose_name': 'id', 'type': get_int, 'width':1500},
{'name': 'url', 'verbose_name': 'url', 'type': str},
{'name': 'name', 'verbose_name': 'Имя', 'type': str, 'width':8000},
{'name': 'data_begin', 'verbose_name': 'Дата начала', 'type': str},
{'name': 'data_end', 'verbose_name': 'Дата окончания', 'type': str},
{'name': 'audience', 'verbose_name': 'Аудитория', 'type': get_audience},
{'name': 'main_title', 'verbose_name': 'Краткое описание', 'type': str},
{'name': 'description', 'verbose_name': 'Описание', 'type': str},
{'name': 'country', 'verbose_name': 'Страна', 'type': str},
{'name': 'city', 'verbose_name': 'Город', 'type': str},
{'name': 'theme', 'verbose_name': 'Тематика', 'type': get_theme, 'width':8000},
{'name': 'tag', 'verbose_name': 'Теги', 'type': get_tag, 'width':8000},
#{'name': 'periodic', 'verbose_name': 'Периодичность', 'type': str},
{'name': 'web_page', 'verbose_name': 'Веб страница', 'type': str},
{'name': 'time', 'verbose_name': 'Время проведения', 'type': str},
{'name': 'products', 'verbose_name': 'Экспонируемые продукты', 'type': str},
{'name': 'foundation_year', 'verbose_name': 'Год основания', 'type': get_int},
{'name': 'tax', 'verbose_name': 'Налог включен', 'type': get_bool, 'width':1000},
{'name': 'currency', 'verbose_name': 'Валюта', 'type': str},
{'name': 'max_price', 'verbose_name': 'Максимальная цена', 'type': get_int},
{'name': 'min_price', 'verbose_name': 'Минимальная цена', 'type': get_int},
{'name': 'registration_payment', 'verbose_name': 'Регистрационный взнос', 'type': get_int},
{'name': 'min_closed_area', 'verbose_name': 'Минимальная цена закрытой НЕ оборудованной площади', 'type': get_int},
{'name': 'max_closed_area', 'verbose_name': 'Максимальная цена закрытой НЕ оборудованной площади', 'type': get_int},
{'name': 'min_closed_equipped_area', 'verbose_name': 'Минимальная цена закрытой оборудованной площади ', 'type': get_int},
{'name': 'max_closed_equipped_area', 'verbose_name': 'Максимальная цена закрытой оборудованной площади', 'type': get_int},
{'name': 'min_open_area', 'verbose_name': 'Минимальная цена закрытой площади', 'type': get_int},
{'name': 'max_open_area', 'verbose_name': 'Максимальная цена открытой площади', 'type': get_int},
{'name': 'min_area', 'verbose_name': 'Минимальная площадь', 'type': get_int},
{'name': 'max_area', 'verbose_name': 'Максимальная площадь', 'type': get_int},
{'name': 'is_published', 'verbose_name': 'Опубликована', 'type': get_bool},
{'name': 'canceled_by_administrator', 'verbose_name': 'Отменена администратором', '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:
# not found
# try to create new city
city = City(country=country)
city.translate(lang)
city.name = value
city.save()
return city
def to_audience(value):
try:
if value == 'Специалисты':
return 'experts'
elif value == 'Специалисты и потребители':
return 'experts and consumers'
elif value == 'Широкая публика':
return 'general public'
else:
return 'None'
except:
return 'None'
def to_theme(value, lang='ru'):
theme_names = value.split(', ')
theme_objects = []
for name in theme_names:
try:
objects = get_translation_aware_manager(Theme)
theme = objects.filter(name=name)[0]
except:
theme = Theme()
theme.translate(lang)
theme.name = name
theme.save()
theme_objects.append(theme)
return theme_objects
def to_tag(value, lang, themes):
value = value.replace('[', '')
themes = themes.all()
# list tags by themes
value = value.split(']')
arr = []
bla = value[0].split(', ')
for theme_number, theme in enumerate(themes):
for val in value[theme_number].split(', '):
if val:
try:
objects = get_translation_aware_manager(Tag)
tag = objects.language(lang).filter(name=val, theme=theme)
tag = tag[0]
except:
tag = Tag()
tag.theme = theme
tag.translate(lang)
tag.name = val
tag.save()
arr.append(tag)
return arr
import_settings={
'name': {'func': str, },
'url': {'func': str},
'data_begin': {'func': str},
'data_end': {'func': str},
'main_title': {'func': str},
'description': {'func': str},
'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': str},
'web_page': {'func': str},
'time': {'func': str},
'products': {'func': str},
'foundation_year': {'func': to_int},
'tax': {'func': bool},
'currency': {'func': str},
'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}
}