Skip to content

Commit

Permalink
Studentmail validation working, ul staff validation not
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnabola committed Aug 9, 2020
1 parent b9a5ea9 commit 4df35cd
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions pytition/petition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .helpers import sanitize_html

import html
import re


# ----------------------------------- PytitionUser ----------------------------
Expand Down Expand Up @@ -419,18 +420,20 @@ class Signature(models.Model):
first_name = models.CharField(max_length=50, verbose_name=ugettext_lazy("First name"))
last_name = models.CharField(max_length=50, verbose_name=ugettext_lazy("Last name"))
phone = models.CharField(max_length=20, blank=True, verbose_name=ugettext_lazy("Phone number"))
email = models.EmailField(verbose_name=ugettext_lazy("Email address"))#validators=[validator_func]-val_func would be written inside this class
email = models.EmailField(verbose_name=ugettext_lazy("Email address"))#validators=[validate_UL_email]
confirmation_hash = models.CharField(max_length=128)
confirmed = models.BooleanField(default=False, verbose_name=ugettext_lazy("Confirmed"))
petition = models.ForeignKey(Petition, on_delete=models.CASCADE, verbose_name=ugettext_lazy("Petition"))
subscribed_to_mailinglist = models.BooleanField(default=False, verbose_name=ugettext_lazy("Subscribed to mailing list"))
date = models.DateTimeField(blank=True, auto_now_add=True, verbose_name=ugettext_lazy("Date"))
ipaddress = models.TextField(blank=True, null=True)

def clean(self):
def clean(self):#validate email here
if self.petition.already_signed(self.email):
if self.petition.signature_set.filter(email = self.email).get(confirmed = True).id != self.id:
raise ValidationError(_("You already signed the petition"))
if (self.validate_UL_email() == True):
raise ValidationError(_("Invalid UL email"))

def save(self, *args, **kwargs):
self.clean()
Expand All @@ -443,9 +446,26 @@ def save(self, *args, **kwargs):
def confirm(self):
self.confirmed = True

"""def validate_UL_email(value):take care uncommenting as it is tab/space sensitive.
if (if_test_fails_Fail):
raise ValidationError('Not a valid University of Limerick email')"""
def validate_UL_email(self):#take care uncommenting as it is tab/space sensitive.
fail = True
value = self.email
studentmailRegex = "^([0-9]{8}$)"
lecturerRegex = "^([\.a-zA-Z]){4,}$)"
studentmail = "@studentmail.ul.ie"
lecturer = "@ul.ie"

x = value.find(studentmail)
if(int(x) == -1):
x = value.find(lecturer)
if (int(x) == -1):
fail = True #pass
else:
if (re.match(lecturerRegex, value[0:int(x)]) is not None):
fail = False
else:
if (re.search(studentmailRegex, value[0:int(x)]) is not None):
fail = False
return (fail)

def __str__(self):
return html.unescape("[{}:{}] {} {}".format(self.petition.id, "OK" if self.confirmed else "..", self.first_name,
Expand Down

0 comments on commit 4df35cd

Please sign in to comment.