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 -*-
import xlrd
from collections import namedtuple
import re
from city.models import City
from ckeditor.widgets import CKEditorWidget
from country.models import Area, Country
@ -102,33 +105,64 @@ class MailingListForm(forms.ModelForm):
# ml.save()
return ml
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']
contact_items = dict(qs.values_list('email', 'pk'))
ids = set(contact_items.values())
if excel_file:
book = xlrd.open_workbook(file_contents=excel_file.read())
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):
email = sheet.row_values(i)[0]
first_name = sheet.row_values(i)[1]
exponent_practicum = sheet.row_values(i)[2]
organiser_practicum = sheet.row_values(i)[3]
if email not in contact_items:
try:
c = Contact.objects.get(email=email)
c.contactsettings.exponent_practicum = exponent_practicum
c.contactsettings.organiser_practicum = organiser_practicum
c.contactsettings.save()
except Contact.DoesNotExist:
c = Contact.objects.create(email=email, first_name=first_name, activated=True)
cs = ContactSettings(
contact=c,
exponent_practicum=exponent_practicum,
organiser_practicum=organiser_practicum,
)
cs.save()
contact_items[email] = c.pk
ids = set(contact_items.values())
item = headers_map(*sheet.row(i))
# email = sheet.row_values(i)[0]
# first_name = sheet.row_values(i)[1]
# exponent_practicum = sheet.row_values(i)[2]
# organiser_practicum = sheet.row_values(i)[3]
# if email not in contact_items:
c, created = Contact.objects.get_or_create(email=item.email.value.strip())
c.first_name = item.first_name.value.strip() or c.first_name
c.activated = True
c.save()
ids.add(c.pk)
contactsettings, _created = ContactSettings.objects.get_or_create(contact_id=c.pk)
contactsettings.exponent_practicum = item.exponent_practicum.value
contactsettings.organiser_practicum = item.organiser_practicum.value
for field, data in field_models.iteritems():
# model = data['model']
# cached = data['cached']
cell = getattr(item, field)
values = cell.value.split(';')
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']:
ct_from_themes = Contact.objects\
.filter(

@ -40,10 +40,10 @@ class ExcelResponse(HttpResponse):
item.contact.first_name,
item.exponent_practicum,
item.organiser_practicum,
', '.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([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([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([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))]),
item.contact.creation_date.strftime('%d.%m.%Y'),
item.contact.modification_date.strftime('%d.%m.%Y'),
])

Loading…
Cancel
Save