Projet

Général

Profil

« Précédent | Suivant » 

Révision 8759c981

Ajouté par Serghei Mihai il y a presque 8 ans

api: subscriptions management endpoint (#10794)

Voir les différences:

corbo/forms.py
5 5

  
6 6

  
7 7
class AnnounceForm(forms.ModelForm):
8
    transport_channel = forms.MultipleChoiceField(required=False,
9
                                                  choices=channel_choices,
10
                                                  widget=forms.CheckboxSelectMultiple())
11 8

  
12 9
    class Meta:
13 10
        model = Announce
......
20 17
        }
21 18
        fields = '__all__'
22 19

  
23
    def __init__(self, *args, **kwargs):
24
        super(AnnounceForm, self).__init__(*args, **kwargs)
25
        self.fields['transport_channel'].initial = [b.channel for \
26
                                                    b in self.instance.broadcast_set.all()]
27

  
28
    def clean_transport_channel(self):
29
        channels = self.cleaned_data['transport_channel']
30
        limit = 160
31
        if 'sms' in channels and \
32
           len(self.cleaned_data['text']) > limit:
33
            raise forms.ValidationError(_('Announce content exceeds %s chars'
34
                                          ' and cannot be sent by SMS' % limit))
35
        return channels
36

  
37 20
    def save(self, *args, **kwargs):
38 21
        instance = super(AnnounceForm, self).save(*args, **kwargs)
39 22
        if instance:
40
            print "CD: %s" % self.cleaned_data
41
            channels = self.cleaned_data['transport_channel']
42
            for channel in channels:
43
                Broadcast.objects.get_or_create(announce=instance,
44
                                                channel=channel)
45
            self.instance.broadcast_set.exclude(announce=instance,
46
                                                channel__in=channels).delete()
23
            Broadcast.objects.get_or_create(announce=instance)
47 24
        return instance
48 25

  
26

  
49 27
class CategoryForm(forms.ModelForm):
50 28
    class Meta:
51 29
        fields = ('name', )

Formats disponibles : Unified diff