Révision aa90f191
Ajouté par Serghei Mihai (congés, retour 15/05) il y a environ 7 ans
corbo/forms.py | ||
---|---|---|
1 |
import csv |
|
2 |
|
|
1 | 3 |
from django import forms |
2 | 4 |
from django.utils.translation import ugettext_lazy as _ |
3 | 5 |
from django.utils.text import slugify |
4 | 6 |
from django.core.exceptions import ObjectDoesNotExist |
7 |
from django.core import validators |
|
8 |
from django.core.exceptions import ValidationError |
|
5 | 9 |
|
6 | 10 |
from .models import Announce, Category, Broadcast, channel_choices |
7 | 11 |
|
... | ... | |
45 | 49 |
slug = '%s-%s' % (base_slug, i) |
46 | 50 |
self.instance.slug = slug |
47 | 51 |
return super(CategoryForm, self).save(commit=commit) |
52 |
|
|
53 |
|
|
54 |
class SubscriptionsImportForm(forms.Form): |
|
55 |
subscribers = forms.FileField(_('Subscribers'), |
|
56 |
help_text=_('utf-8 encoded, comma separated file with email addresses on first column')) |
|
57 |
|
|
58 |
def clean_subscribers(self, *args, **kwargs): |
|
59 |
subscribers = [] |
|
60 |
reader = csv.reader(self.cleaned_data['subscribers']) |
|
61 |
for idx, row in enumerate(reader, 1): |
|
62 |
if not row or not row[0]: |
|
63 |
continue |
|
64 |
try: |
|
65 |
validators.validate_email(row[0]) |
|
66 |
except ValidationError: |
|
67 |
raise ValidationError(_('Invalid email address at line %d' % idx)) |
|
68 |
subscribers.append(row[0]) |
|
69 |
return subscribers |
Formats disponibles : Unified diff
import subscribers from csv (#14010)