@ -1,12 +1,12 @@ |
||||
# -*- coding: utf-8 -*- |
||||
from django.conf.urls import patterns, url |
||||
from admin import UserListView |
||||
|
||||
urlpatterns = patterns('', |
||||
#url(r'^registration/$', 'accounts.admin.registration'), |
||||
#url(r'^create_admin/$', 'accounts.admin.create_admin'), |
||||
#url(r'^create_admin/$', 'accounts.admin.create_admin'), |
||||
#url(r'^create_md5user/$', 'accounts.admin.create_md5'), |
||||
url(r'^change/(.*)/$', 'accounts.admin.user_change'), |
||||
# url(r'^change/(?P<user_id>\d+).*/$', 'accounts.views.user_change'), |
||||
url(r'^all/$', 'accounts.admin.user_all'), |
||||
url(r'^all/$', UserListView.as_view()), |
||||
url(r'^reset_password_email/$', 'accounts.admin.reset_password_email'), |
||||
) |
||||
@ -1 +0,0 @@ |
||||
|
||||
@ -1,11 +1,16 @@ |
||||
# -*- coding: utf-8 -*- |
||||
from django.conf.urls import patterns, include, url |
||||
from admin import CompanyListView |
||||
from admin import CompanyListView, CompanyView |
||||
|
||||
urlpatterns = patterns('company.admin', |
||||
url(r'^add.*/$', 'company_add'), |
||||
url(r'^delete/(?P<url>.*)/$', 'company_delete'), |
||||
url(r'^change/(?P<url>.*).*/$', 'company_change'), |
||||
#url(r'^all/$', 'company_all'), |
||||
url(r'^all/$', CompanyListView.as_view()), |
||||
url(r'^$', CompanyView.as_view()), |
||||
url(r'^(?P<url>.*)/$', CompanyView.as_view()), |
||||
|
||||
#url(r'^add.*/$', 'company_add'), |
||||
#url(r'^delete/(?P<url>.*)/$', 'company_delete'), |
||||
#url(r'^change/(?P<url>.*).*/$', 'company_change'), |
||||
#url(r'^all/$', 'company_all'), |
||||
#url(r'^all/$', CompanyListView.as_view()), |
||||
|
||||
) |
||||
@ -1,46 +1,38 @@ |
||||
# -*- coding: utf-8 -*- |
||||
from haystack import indexes |
||||
from models import Company |
||||
""" |
||||
class CompanyIndex(indexes.SearchIndex, indexes.Indexable): |
||||
from functions.search_mixin import ExpoSearchMixin |
||||
|
||||
|
||||
class CompanyExpositionIndex(indexes.SearchIndex, indexes.Indexable, ExpoSearchMixin): |
||||
text = indexes.CharField(document=True, use_template=True) |
||||
where = indexes.MultiValueField() |
||||
content_auto = indexes.EdgeNgramField() |
||||
area_id = indexes.IntegerField() |
||||
country_id = indexes.IntegerField() |
||||
city_id = indexes.IntegerField() |
||||
theme = indexes.MultiValueField() |
||||
tag = indexes.MultiValueField() |
||||
country = indexes.CharField(model_attr='country', null=True) |
||||
city = indexes.CharField(model_attr='city', null=True) |
||||
|
||||
def prepare_country(self, obj): |
||||
if obj.country: |
||||
return '%s'%obj.country.id |
||||
return '' |
||||
|
||||
def prepare_city(self, obj): |
||||
if obj.city: |
||||
return '%s'%obj.city.id |
||||
return '' |
||||
url = indexes.CharField() |
||||
form_name = indexes.CharField() |
||||
# translated fields |
||||
name_en = indexes.CharField() |
||||
name_ru = indexes.CharField() |
||||
catalog_name_en = indexes.CharField() |
||||
catalog_name_ru = indexes.CharField() |
||||
|
||||
def prepare_theme(self, obj): |
||||
def prepare_form_name(self, obj): |
||||
return None |
||||
|
||||
return [str(th.id) for th in obj.theme.filter()] |
||||
def prepare_catalog_name_en(self, obj): |
||||
return u'Members' |
||||
|
||||
def prepare_tag(self, obj): |
||||
return [str(tag.id) for tag in obj.tag.filter()] |
||||
|
||||
def prepare_where(self, obj): |
||||
country = [] |
||||
city = [] |
||||
if obj.country: |
||||
country = [tr.name for tr in obj.country.translations.all()] |
||||
if obj.city: |
||||
city = [tr.name for tr in obj.city.translations.all()] |
||||
|
||||
return country + city |
||||
def prepare_catalog_name_ru(self, obj): |
||||
return u'Участники' |
||||
|
||||
def get_model(self): |
||||
return Company |
||||
|
||||
def index_queryset(self, using=None): |
||||
|
||||
return self.get_model().objects.filter() |
||||
|
||||
""" |
||||
return self.get_model().objects.filter() |
||||
@ -1,396 +1,85 @@ |
||||
# -*- 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 |
||||
import xlrd |
||||
from django.core.management.base import BaseCommand |
||||
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}, |
||||
from exposition.models import Exposition |
||||
from import_xls.excel_settings import event_sett |
||||
|
||||
} |
||||
CHINA_FILE = settings.MEDIA_ROOT+'/import/expo_china_ru.xlsx' |
||||
GERMANY_FILE = settings.MEDIA_ROOT+'/import/expo_germany_ru.xlsx' |
||||
# 391 row not imported(same url) |
||||
ITALY_FILE = settings.MEDIA_ROOT+'/import/expo_italy_ru.xlsx' |
||||
# moscow 3 exps |
||||
F = settings.MEDIA_ROOT+'/import/exp.xlsx' |
||||
|
||||
|
||||
class Command(BaseCommand): |
||||
def handle(self, *args, **options): |
||||
|
||||
f = open(settings.MEDIA_ROOT+'/import/Places Eng Upd.xlsx', 'r') |
||||
f = open(F, '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[1:]): |
||||
|
||||
|
||||
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 |
||||
if row[0] != '': |
||||
# in first column ids |
||||
try: |
||||
object = Exposition.objects.language('ru').get(id=int(row[0])) |
||||
except ValueError: |
||||
object = Exposition() |
||||
object.translate('ru') |
||||
|
||||
except Exposition.DoesNotExist: |
||||
object = Exposition(id= int(row[0])) |
||||
object.translate('ru') |
||||
else: |
||||
# if id blank - its a new place |
||||
object = Exposition() |
||||
object.translate('ru') |
||||
methods = [] |
||||
for col_number, cell in enumerate(row): |
||||
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, 'ru', getattr(object, extra_value)) |
||||
elif setting.get('bitfield'): |
||||
value = func(object, cell, setting['label']) |
||||
|
||||
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 |
||||
else: |
||||
value = func(cell) |
||||
setattr(object, field_name, value) |
||||
object.save() |
||||
|
||||
|
||||
print('post save %s'% str(object)) |
||||
|
||||
if not flag: |
||||
object.save() |
||||
""" |
||||
for method in methods: |
||||
func = method['func'] |
||||
if method.get('purpose'): |
||||
try: |
||||
print('pre save %s'% str(object)) |
||||
object.save() |
||||
func(object, method['value'], method['purpose']) |
||||
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 |
||||
func(object, method['value']) |
||||
@ -0,0 +1,65 @@ |
||||
# -*- coding: utf-8 -*- |
||||
from haystack import indexes |
||||
|
||||
class ExpoSearchMixin(object): |
||||
""" |
||||
|
||||
""" |
||||
|
||||
def prepare_where(self, obj): |
||||
if obj.country: |
||||
country = [tr.name for tr in obj.country.translations.all()] |
||||
else: |
||||
country = '' |
||||
if obj.city: |
||||
city = [tr.name for tr in obj.city.translations.all()] |
||||
else: |
||||
city = '' |
||||
|
||||
return country + city |
||||
|
||||
def prepare_content_auto(self, obj): |
||||
""" |
||||
object must have get_index_text method which generate text for searching |
||||
""" |
||||
return obj.get_index_text() |
||||
|
||||
|
||||
def prepare_area_id(self, obj): |
||||
if obj.country: |
||||
return obj.country.area.id |
||||
return None |
||||
|
||||
def prepare_country_id(self, obj): |
||||
if obj.country: |
||||
return obj.country.id |
||||
return None |
||||
|
||||
def prepare_city_id(self, obj): |
||||
if obj.city: |
||||
return obj.city.id |
||||
return None |
||||
|
||||
def prepare_theme(self, obj): |
||||
return [th.id for th in obj.theme.filter()] |
||||
|
||||
def prepare_tag(self, obj): |
||||
return [th.id for th in obj.tag.filter()] |
||||
|
||||
|
||||
def prepare_url(self, obj): |
||||
return obj.get_permanent_url() |
||||
|
||||
def prepare_name_en(self, obj): |
||||
try: |
||||
return obj.translations.get(language_code = 'en').name |
||||
except: |
||||
return '' |
||||
|
||||
def prepare_name_ru(self, obj): |
||||
try: |
||||
return obj.translations.get(language_code = 'ru').name |
||||
except: |
||||
return '' |
||||
|
||||
|
||||
@ -0,0 +1,22 @@ |
||||
from hvad.models import TranslationManager |
||||
|
||||
|
||||
class ExpoImportManager(TranslationManager): |
||||
def create_by_dict(self, d): |
||||
model = self.model |
||||
id = d['id'] |
||||
lang = d['lang'] |
||||
if id: |
||||
expo = self.language(lang).get(id=id) |
||||
else: |
||||
expo = model() |
||||
expo.translate(lang) |
||||
|
||||
|
||||
aaaa |
||||
|
||||
# save simple values |
||||
# save relations |
||||
|
||||
|
||||
|
||||
@ -0,0 +1,172 @@ |
||||
# -*- coding: utf-8 -*- |
||||
import urllib2 |
||||
import time, xlrd |
||||
from django.conf import settings |
||||
from django.utils import translation |
||||
from hvad.utils import get_translation_aware_manager |
||||
from bitfield import BitHandler |
||||
from place_exposition.models import PlaceExposition |
||||
from exposition.models import Exposition |
||||
from country.models import Country |
||||
from city.models import City |
||||
from theme.models import Theme, Tag |
||||
from functions.files import get_alternative_filename |
||||
from exposition.models import BIT_AUDIENCE |
||||
|
||||
|
||||
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_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_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 = City.objects.filter(translations__name=value, country=country)[0] |
||||
# print(city) |
||||
return city |
||||
except IndexError: |
||||
# print('---------------------') |
||||
# print(value.encode('utf8')) |
||||
# print('---------------------') |
||||
return None |
||||
|
||||
|
||||
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(',') |
||||
|
||||
obj.theme.add(*Theme.objects.filter(id__in=theme_ids)) |
||||
|
||||
def to_tag(obj,value): |
||||
if value == [""]: |
||||
return None |
||||
names = value.split(',') |
||||
translation.activate('en') |
||||
if names: |
||||
obj.tag.add(*Tag.objects.filter(translations__name__in=names, theme__in=obj.theme.all())) |
||||
else: |
||||
return |
||||
|
||||
|
||||
|
||||
def to_place(value): |
||||
try: |
||||
place = PlaceExposition.objects.get(url=value) |
||||
return place |
||||
except PlaceExposition.DoesNotExist: |
||||
return None |
||||
|
||||
|
||||
def to_periodic(value): |
||||
periodic = {'': 0, u'Ежегодно': 1.0, u'2 раза в год': 2.0, u'3 раза в год': 3.0, |
||||
u'4 раза в год': 4.0, u'5 раз в год': 5.0, u'Раз в 2 года': 0.5, |
||||
u'Раз в 3 года': 0.33, u'Раз в 4 года': 0.25} |
||||
|
||||
return periodic.get(value, 0) |
||||
|
||||
|
||||
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 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 save_logo(obj, path): |
||||
if not path: |
||||
return None |
||||
file_name = path.split('/')[-1] |
||||
logo_path = obj.logo.field.upload_to |
||||
full_path = settings.MEDIA_ROOT + logo_path |
||||
|
||||
|
||||
alt_name = get_alternative_filename(full_path, file_name) |
||||
|
||||
download_to = full_path+alt_name |
||||
|
||||
if path.startswith('http://') or path.startswith('https://'): |
||||
|
||||
url = path |
||||
else: |
||||
url = 'http://expomap.ru' + path |
||||
|
||||
try: |
||||
response = urllib2.urlopen(url, timeout=10) |
||||
except: |
||||
return None |
||||
|
||||
with open(download_to,'wb') as f: |
||||
try: |
||||
f.write(response.read()) |
||||
f.close() |
||||
except: |
||||
# can be timeout |
||||
return None |
||||
|
||||
obj.logo = logo_path + alt_name |
||||
obj.save() |
||||
|
||||
|
||||
def check_quality_label(obj, value, label): |
||||
bit = obj.quality_label |
||||
try: |
||||
value = int(value) |
||||
except: |
||||
return bit |
||||
if value: |
||||
setattr(bit, label, True) |
||||
return bit |
||||
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 1.5 KiB |
|
Before Width: | Height: | Size: 7.5 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 7.5 KiB |
|
Before Width: | Height: | Size: 5.6 KiB |
|
Before Width: | Height: | Size: 6.2 KiB |
|
Before Width: | Height: | Size: 4.6 KiB |
|
Before Width: | Height: | Size: 5.1 KiB |
|
Before Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 7.5 KiB |
|
Before Width: | Height: | Size: 4.4 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 6.5 KiB |
|
Before Width: | Height: | Size: 8.4 KiB |
|
Before Width: | Height: | Size: 6.2 KiB |
|
Before Width: | Height: | Size: 9.8 KiB |
|
Before Width: | Height: | Size: 2.1 KiB |
|
Before Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 8.6 KiB |
|
Before Width: | Height: | Size: 7.8 KiB |
|
Before Width: | Height: | Size: 7.7 KiB |
|
Before Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 5.5 KiB |
|
Before Width: | Height: | Size: 7.3 KiB |
|
Before Width: | Height: | Size: 6.8 KiB |
|
Before Width: | Height: | Size: 7.0 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 6.9 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 5.0 KiB |
|
Before Width: | Height: | Size: 2.8 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 2.4 KiB |
|
Before Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 6.1 KiB |
|
Before Width: | Height: | Size: 4.5 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 1.9 KiB |