1457: Этап №5: Со скайпа - Импорт подписчиков

Не импортировались тематики, страны и города (параметры фильтрации) - исправлено
remotes/origin/stage5
Alexander Burdeiny 10 years ago
parent a33aca03d0
commit 8cb0b93611
  1. 74
      emencia/django/newsletter/admin_forms.py
  2. 8
      emencia/django/newsletter/utils/excel.py

@ -1,5 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import xlrd import xlrd
from collections import namedtuple
import re
from city.models import City from city.models import City
from ckeditor.widgets import CKEditorWidget from ckeditor.widgets import CKEditorWidget
from country.models import Area, Country from country.models import Area, Country
@ -102,33 +105,64 @@ class MailingListForm(forms.ModelForm):
# ml.save() # ml.save()
return ml return ml
def get_contact_list(self, qs): def get_contact_list(self, qs):
headers_map = namedtuple("Headers", ['email', 'first_name', 'exponent_practicum', 'organiser_practicum', 'geo', 'country', 'city', 'theme', 'time1', 'time2'])
excel_file = self.cleaned_data['excel_file'] excel_file = self.cleaned_data['excel_file']
contact_items = dict(qs.values_list('email', 'pk')) contact_items = dict(qs.values_list('email', 'pk'))
ids = set(contact_items.values())
if excel_file: if excel_file:
book = xlrd.open_workbook(file_contents=excel_file.read()) book = xlrd.open_workbook(file_contents=excel_file.read())
sheet = book.sheet_by_index(0) sheet = book.sheet_by_index(0)
countries = {}
cities = {}
themes = {}
field_models = {
'country': {'model': Country, 'cached': {}},
'city': {'model': City, 'cached': {}},
'theme': {'model': Theme, 'cached': {}}
}
for i in range(1, sheet.nrows): for i in range(1, sheet.nrows):
email = sheet.row_values(i)[0] item = headers_map(*sheet.row(i))
first_name = sheet.row_values(i)[1] # email = sheet.row_values(i)[0]
exponent_practicum = sheet.row_values(i)[2] # first_name = sheet.row_values(i)[1]
organiser_practicum = sheet.row_values(i)[3] # exponent_practicum = sheet.row_values(i)[2]
if email not in contact_items: # organiser_practicum = sheet.row_values(i)[3]
try: # if email not in contact_items:
c = Contact.objects.get(email=email) c, created = Contact.objects.get_or_create(email=item.email.value.strip())
c.contactsettings.exponent_practicum = exponent_practicum c.first_name = item.first_name.value.strip() or c.first_name
c.contactsettings.organiser_practicum = organiser_practicum c.activated = True
c.contactsettings.save() c.save()
except Contact.DoesNotExist: ids.add(c.pk)
c = Contact.objects.create(email=email, first_name=first_name, activated=True) contactsettings, _created = ContactSettings.objects.get_or_create(contact_id=c.pk)
cs = ContactSettings( contactsettings.exponent_practicum = item.exponent_practicum.value
contact=c, contactsettings.organiser_practicum = item.organiser_practicum.value
exponent_practicum=exponent_practicum, for field, data in field_models.iteritems():
organiser_practicum=organiser_practicum, # model = data['model']
) # cached = data['cached']
cs.save() cell = getattr(item, field)
contact_items[email] = c.pk values = cell.value.split(';')
ids = set(contact_items.values()) names_to_fetch = []
pks = set()
for val in values:
val = val.strip()
_pk = data['cached'].get(val, None)
if _pk is None:
names_to_fetch.append(val)
else:
pks.add(_pk)
if names_to_fetch:
_pks = dict(data['model'].objects.language().filter(name__in=values).values_list('name', 'pk'))
data['cached'].update(_pks)
pks.update(_pks.values())
if pks:
_field = getattr(contactsettings, field)
_field.add(*pks)
contactsettings.save()
# p = re.compile(r'([^\(pk=\d+\)]*\(pk=(?P<pk>\d+)\))')
# p.finditer():
# ...
if self.cleaned_data['add_from_themes']: if self.cleaned_data['add_from_themes']:
ct_from_themes = Contact.objects\ ct_from_themes = Contact.objects\
.filter( .filter(

@ -40,10 +40,10 @@ class ExcelResponse(HttpResponse):
item.contact.first_name, item.contact.first_name,
item.exponent_practicum, item.exponent_practicum,
item.organiser_practicum, item.organiser_practicum,
', '.join([areas.get(x) for x in set(item.area.values_list('pk', flat=True))]), '; '.join([areas.get(x) for x in set(item.area.values_list('pk', flat=True))]),
', '.join([countries.get(x) for x in set(item.country.values_list('pk', flat=True))]), '; '.join([countries.get(x) for x in set(item.country.values_list('pk', flat=True))]),
', '.join([cities.get(x) for x in set(item.city.values_list('pk', flat=True))]), '; '.join([cities.get(x) for x in set(item.city.values_list('pk', flat=True))]),
', '.join([themes.get(x) for x in set(item.theme.values_list('pk', flat=True))]), '; '.join([themes.get(x) for x in set(item.theme.values_list('pk', flat=True))]),
item.contact.creation_date.strftime('%d.%m.%Y'), item.contact.creation_date.strftime('%d.%m.%Y'),
item.contact.modification_date.strftime('%d.%m.%Y'), item.contact.modification_date.strftime('%d.%m.%Y'),
]) ])

Loading…
Cancel
Save