summaryrefslogtreecommitdiff
path: root/app/lttr/forms.py
diff options
context:
space:
mode:
authorlxf <sng@luxagraf.net>2022-05-14 16:38:07 -0400
committerlxf <sng@luxagraf.net>2022-05-14 16:38:07 -0400
commitbb3973ffb714c932e9ec6dd6a751228dc71fe1d3 (patch)
tree6fa32f9392ad2ec32271349b86a4c1388fd6ba95 /app/lttr/forms.py
initial commit
Diffstat (limited to 'app/lttr/forms.py')
-rw-r--r--app/lttr/forms.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/app/lttr/forms.py b/app/lttr/forms.py
new file mode 100644
index 0000000..e3fb272
--- /dev/null
+++ b/app/lttr/forms.py
@@ -0,0 +1,100 @@
+from django import forms
+from django.forms.utils import ValidationError
+
+from .validators import validate_email_nouser
+from .models import Subscriber
+
+
+class SubscribeForm(forms.ModelForm):
+
+ class Meta:
+ model = Subscriber
+ fields = ['user']
+
+
+class NewsletterForm(forms.ModelForm):
+ """ This is the base class for all forms managing subscriptions. """
+ email_field = forms.EmailField(label='Email Address:', widget=forms.EmailInput(attrs={'placeholder': 'Your email address'}))
+
+ class Meta:
+ model = Subscriber
+ fields = ('email_field',)
+
+ def __init__(self, *args, **kwargs):
+
+ assert 'newsletter' in kwargs, 'No newsletter specified'
+
+ newsletter = kwargs.pop('newsletter')
+
+ if 'ip' in kwargs:
+ ip = kwargs['ip']
+ del kwargs['ip']
+ else:
+ ip = None
+
+ super(NewsletterForm, self).__init__(*args, **kwargs)
+
+ self.instance.newsletter = newsletter
+
+ if ip:
+ self.instance.ip = ip
+
+
+class SubscribeRequestForm(NewsletterForm):
+ """
+ Request subscription to the newsletter. Will result in an activation email
+ being sent with a link where one can edit, confirm and activate one's
+ subscription.
+ """
+ email_field = forms.EmailField(
+ label=("e-mail"), validators=[validate_email_nouser], widget=forms.EmailInput(attrs={'placeholder': 'Your email address'})
+ )
+
+ def clean_email_field(self):
+ data = self.cleaned_data['email_field']
+
+ # Check whether we have already been subscribed to
+ try:
+ subscription = Subscriber.objects.get(
+ email_field__exact=data,
+ newsletter=self.instance.newsletter
+ )
+
+ if subscription.subscribed and not subscription.unsubscribed:
+ raise ValidationError(
+ "I appreciate the effort, but you're already subscribed. (if you're not receiving newsletters, email me at sng@luxagraf.net and I will see what's going on)"
+ )
+ else:
+ self.instance = subscription
+
+ self.instance = subscription
+
+ except Subscriber.DoesNotExist:
+ pass
+
+ return data
+
+
+class UpdateForm(NewsletterForm):
+ """
+ This form allows one to actually update to or unsubscribe from the
+ newsletter. To do this, a correct activation code is required.
+ """
+
+ email_field = forms.EmailField(
+ label=("e-mail"), validators=[validate_email_nouser], disabled=True
+ )
+
+ def clean_user_activation_code(self):
+ data = self.cleaned_data['user_activation_code']
+
+ if data != self.instance.activation_code:
+ raise ValidationError(
+ ('The validation code supplied by you does not match.')
+ )
+
+ return data
+
+ user_activation_code = forms.CharField(
+ label=("Activation code"), max_length=40
+ )