Project

General

Profile

Download (2.54 KB) Statistics
| Branch: | Tag: | Revision:

root / corbo / forms.py @ 8e8b10ae

1
import csv
2

    
3
from django import forms
4
from django.utils.translation import ugettext_lazy as _
5
from django.utils.text import slugify
6
from django.core.exceptions import ObjectDoesNotExist
7
from django.core import validators
8
from django.core.exceptions import ValidationError
9

    
10
from .models import Announce, Category, Broadcast, channel_choices
11
from . import widgets
12

    
13

    
14
DATETIME_OPTIONS = {
15
        'weekStart': 1,
16
        'autoclose': True,
17
}
18

    
19

    
20
class DateTimeWidget(widgets.DateTimeWidget):
21
    def __init__(self, *args, **kwargs):
22
        super(DateTimeWidget, self).__init__(*args, options=DATETIME_OPTIONS, **kwargs)
23

    
24

    
25
class AnnounceForm(forms.ModelForm):
26

    
27
    class Meta:
28
        model = Announce
29
        exclude = ('identifier',)
30
        widgets = {
31
            'publication_time': DateTimeWidget(),
32
            'expiration_time': DateTimeWidget(),
33
            'category': forms.HiddenInput()
34
        }
35

    
36
    def save(self, *args, **kwargs):
37
        instance = super(AnnounceForm, self).save(*args, **kwargs)
38
        if instance:
39
            Broadcast.objects.get_or_create(announce=instance)
40
        return instance
41

    
42

    
43
class CategoryForm(forms.ModelForm):
44
    class Meta:
45
        fields = ('name', 'rss_feed_url')
46
        model = Category
47

    
48
    def save(self, commit=True):
49
        slug = slugify(self.instance.name)
50
        base_slug = slug
51
        if not self.instance.slug:
52
            i = 1
53
            while True:
54
                try:
55
                    c = Category.objects.get(slug=slug)
56
                except ObjectDoesNotExist:
57
                    break
58
                i += 1
59
                slug = '%s-%s' % (base_slug, i)
60
            self.instance.slug = slug
61
        return super(CategoryForm, self).save(commit=commit)
62

    
63

    
64
class SubscriptionsImportForm(forms.Form):
65
    subscribers = forms.FileField(_('Subscribers'),
66
                    help_text=_('utf-8 encoded, comma separated file with email addresses on first column'))
67

    
68
    def clean_subscribers(self, *args, **kwargs):
69
        subscribers = []
70
        reader = csv.reader(self.cleaned_data['subscribers'])
71
        for idx, row in enumerate(reader, 1):
72
            if not row or not row[0]:
73
                continue
74
            try:
75
                validators.validate_email(row[0])
76
            except ValidationError:
77
                raise ValidationError(_('Invalid email address at line %d' % idx))
78
            subscribers.append(row[0])
79
        return subscribers
80

    
81

    
82
class SendTestEmailForm(forms.Form):
83
    email = forms.EmailField()
84

    
85

    
86
class SendTestSMSForm(forms.Form):
87
    mobile = forms.CharField(label=_('Mobile number'))
(4-4/13)