Révision 46bbe5fe
Ajouté par Serghei Mihai (congés, retour 15/05) il y a plus de 9 ans
corbo/forms.py | ||
---|---|---|
1 | 1 |
from django import forms |
2 |
from django.utils.translation import ugettext_lazy as _ |
|
2 | 3 |
|
3 |
from .models import Announce, Category |
|
4 |
from .models import Announce, Category, Broadcast |
|
5 |
from .channels import get_channel_choices |
|
4 | 6 |
|
5 | 7 |
class AnnounceForm(forms.ModelForm): |
8 |
transport_channel = forms.MultipleChoiceField(required=False, |
|
9 |
choices=get_channel_choices(), |
|
10 |
widget=forms.CheckboxSelectMultiple()) |
|
6 | 11 |
|
7 | 12 |
class Meta: |
8 | 13 |
model = Announce |
... | ... | |
13 | 18 |
'size': 8}) |
14 | 19 |
} |
15 | 20 |
|
21 |
def __init__(self, *args, **kwargs): |
|
22 |
super(AnnounceForm, self).__init__(*args, **kwargs) |
|
23 |
self.fields['transport_channel'].initial = [b.channel for \ |
|
24 |
b in self.instance.broadcast_set.all()] |
|
25 |
|
|
26 |
def save(self, *args, **kwargs): |
|
27 |
instance = super(AnnounceForm, self).save(*args, **kwargs) |
|
28 |
if instance: |
|
29 |
channels = self.cleaned_data['transport_channel'] |
|
30 |
for channel in channels: |
|
31 |
Broadcast.objects.get_or_create(announce=instance, |
|
32 |
channel=channel) |
|
33 |
self.instance.broadcast_set.exclude(announce=instance, |
|
34 |
channel__in=channels).delete() |
|
35 |
return instance |
|
36 |
|
|
16 | 37 |
class CategoryForm(forms.ModelForm): |
17 | 38 |
class Meta: |
18 | 39 |
fields = ('name', ) |
Formats disponibles : Unified diff
announce broadcasting channels added