class CustomizePluggetSettingsForm(forms.ModelForm): """A form to customize the plugget appearance source-specific settings. Setting fields are added dynamically based on source registered form. """ class Meta: model = Plugget fields = ['title'] def __init__(self, *args, **kwargs): self.region = kwargs.pop("region", None) super(CustomizePluggetSettingsForm, self).__init__(*args, **kwargs) def clean_title(self): title = self.cleaned_data['title'].strip(" ") try: plugget = Plugget.objects.get(title=title, region=self.region) if plugget != self.instance: raise forms.ValidationError(_("This title is already in use.")) except Plugget.DoesNotExist: pass return title enrich_form(SelectPluggetSourceForm) enrich_form(CustomizePluggetSettingsForm)
self.menu = self.instance.menu except Menu.DoesNotExist: pass def clean_title(self): title = self.cleaned_data['title'] try: bookmark = Bookmark.objects.get(title=title, menu=self.menu) if bookmark != self.instance: raise forms.ValidationError(_("This title is already in use.")) except Bookmark.DoesNotExist: pass return title def save(self, commit=True): from django.template.defaultfilters import slugify obj = super(BookmarkForm, self).save(commit=False) obj.menu = self.menu or obj.menu obj.slug = slugify(("%s_%s" % (obj.title, self.menu.slug))[:-1]) if commit: obj.save() return obj enrich_form(BookmarkForm)
"""Form for notification subscriptions. """ def __init__(self, subscriber, *args, **kwargs): self.subscriber = subscriber signatures = kwargs.pop("signatures", Signature.objects.all()) super(SubscriptionsForm, self).__init__(*args, **kwargs) for signature in signatures: if isinstance(signature, Signature): name = signature.slug is_subscriber = (Subscription.objects.filter(signature=signature, subscriber=self.subscriber).count() > 0) send_email = (Subscription.objects.filter(signature=signature, subscriber=self.subscriber, send_email=True).count() > 0) field = SubscriptionField(label=_(signature.title), initial=self.initial.get(name, {'subscribe': is_subscriber, 'email': send_email})) self.fields[name] = field def save(self): self.full_clean() data = self.cleaned_data for key, (subscribe, email) in data.items(): signature = Signature.objects.get(slug=key) is_subscriber = (Subscription.objects.filter(subscriber=self.subscriber, signature=signature).count() > 0) if subscribe: subscription = Subscription.objects.get_or_create(subscriber=self.subscriber, signature=signature)[0] subscription.email = self.subscriber.email subscription.send_email = email subscription.save() elif is_subscriber: Subscription.objects.filter(subscriber=self.subscriber, signature=signature).delete() self.fields[key].initial = (False, False) enrich_form(SubscriptionsForm)
subscriber=self.subscriber, send_email=True).count() > 0) field = SubscriptionField(label=_(signature.title), initial=self.initial.get( name, { 'subscribe': is_subscriber, 'email': send_email })) self.fields[name] = field def save(self): self.full_clean() data = self.cleaned_data for key, (subscribe, email) in data.items(): signature = Signature.objects.get(slug=key) is_subscriber = (Subscription.objects.filter( subscriber=self.subscriber, signature=signature).count() > 0) if subscribe: subscription = Subscription.objects.get_or_create( subscriber=self.subscriber, signature=signature)[0] subscription.email = self.subscriber.email subscription.send_email = email subscription.save() elif is_subscriber: Subscription.objects.filter(subscriber=self.subscriber, signature=signature).delete() self.fields[key].initial = (False, False) enrich_form(SubscriptionsForm)