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.
172 lines
4.4 KiB
172 lines
4.4 KiB
# -*- 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 |