From 2a3079dbd8021c60a2b1b70789fd68cb3af105b4 Mon Sep 17 00:00:00 2001 From: Slava Kyrachevsky Date: Fri, 10 Feb 2017 14:34:53 +0200 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D0=B8=D0=BC=D0=BF?= =?UTF-8?q?=D0=BE=D1=80=D1=82=D0=B0=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/exposition/admin.py | 2 +- apps/import_xls/excel_settings.py | 4 +- apps/import_xls/import_forms.py | 64 +++++++++++++++---------------- apps/import_xls/utils.py | 48 +++++++++++------------ 4 files changed, 56 insertions(+), 62 deletions(-) diff --git a/apps/exposition/admin.py b/apps/exposition/admin.py index ba930e68..a8d23af5 100644 --- a/apps/exposition/admin.py +++ b/apps/exposition/admin.py @@ -278,7 +278,7 @@ class ExpositionView(AdminView): 'expohit': obj.expohit, 'discount': obj.discount, 'canceled': obj.canceled, 'moved': obj.moved, 'logo': obj.logo, 'visitors': obj.visitors, 'members': obj.members, - 'audience':[item for item, bool in obj.audience.all() if bool], + 'audience': [item for item in obj.audience.all()], 'quality_label': [item for item, bool in obj.quality_label if bool], 'place_alt': obj.place_alt} diff --git a/apps/import_xls/excel_settings.py b/apps/import_xls/excel_settings.py index 770e1561..11a7af80 100644 --- a/apps/import_xls/excel_settings.py +++ b/apps/import_xls/excel_settings.py @@ -284,7 +284,7 @@ event_sett = { u'Основные темы': {u'field': u'main_themes', u'func': unicode}, u'Условия и скидка': {u'field': u'discount_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'audience', u'func': to_audience, u'method': True}, u'Официальный веб-сайт': {u'field': u'web_page', u'func': to_url}, u'Линк на регистрацию': {u'field': u'link', u'func': to_url}, u'Экспонируемые продукты': {u'field': u'products', u'func': unicode}, @@ -511,4 +511,4 @@ import_settings={ 'is_published': {'func': bool}, 'canceled_by_administrator': {'func': bool}, 'types': {'func': to_theme_type} -} \ No newline at end of file +} diff --git a/apps/import_xls/import_forms.py b/apps/import_xls/import_forms.py index 39b99f29..35dd6ef3 100644 --- a/apps/import_xls/import_forms.py +++ b/apps/import_xls/import_forms.py @@ -420,7 +420,6 @@ class ImportEventForm(ImportForm): if setting is None: continue - if setting.get('method'): # this cell contains data that must be written after creating object if cell != "": @@ -549,40 +548,39 @@ class ImportEventForm(ImportForm): list_dicts.append(d) + """ + # go through row cells + # field name current cell + field_name = field_names[col_number] + if field_name =='theme': + # need save object before saving manytomany field + #object.save() + setting = import_settings.get(field_name) + d[setting] = cell - """ - # go through row cells - # field name current cell - field_name = field_names[col_number] - if field_name =='theme': - # need save object before saving manytomany field - #object.save() - setting = import_settings.get(field_name) - d[setting] = cell - - if setting is not None: - # if setting exist for this 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) - if cell: - value = func(cell, lang, getattr(object, extra_value)) - else: - value = None - else: - value = func(cell) - if value: - setattr(object, field_name, value) - """ + if setting is not None: + # if setting exist for this 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) + if cell: + value = func(cell, lang, getattr(object, extra_value)) + else: + value = None + else: + value = func(cell) + if value: + setattr(object, field_name, value) + """ - #object.save() + #object.save() diff --git a/apps/import_xls/utils.py b/apps/import_xls/utils.py index 049451a5..3931adad 100644 --- a/apps/import_xls/utils.py +++ b/apps/import_xls/utils.py @@ -6,9 +6,8 @@ from PIL import Image 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 events.models import TargetAudience from country.models import Country from city.models import City from theme.models import Theme, Tag @@ -134,32 +133,29 @@ def to_periodic(value): return periodic.get(value, 0) -def to_audience(value, model=Exposition): - if value: - translation.activate('ru') - 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_audience(obj, value): + # new_list = [] + # if value: + # translation.activate('ru') + # l = value.split(', ') + # target_audience = TargetAudience.objects.all() + # print l + # for value in l: + # for ta in target_audience: + # if value == ta.title: + # new_list.append(ta.pk) + # return new_list + translation.activate('ru') + target_audience = TargetAudience.objects.filter(title__in=value.split(', ')).values_list('pk', flat=True) + obj.audience.clear() + obj.audience.add(*TargetAudience.objects.filter(id__in=target_audience)) + return None + 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(unicode(item2)) - - return ', '.join(new_list) - return '' + new_list = [x.title for x in value.all()] + return ', '.join(new_list) + import types def save_logo(obj, path):