Enforce company constraints 31/70831/2
authorSean Smith <ssmith@iol.unh.edu>
Wed, 19 Aug 2020 15:58:56 +0000 (11:58 -0400)
committerSean Smith <ssmith@iol.unh.edu>
Wed, 19 Aug 2020 19:01:20 +0000 (15:01 -0400)
Signed-off-by: Sean Smith <ssmith@iol.unh.edu>
Change-Id: Id5726e5c9930e684c23cebafb98d5fbcb95e67bc

src/account/models.py

index 6828ee1..2c133bb 100644 (file)
@@ -11,6 +11,8 @@
 from django.contrib.auth.models import User
 from django.db import models
 from django.apps import apps
+from django.core.exceptions import ValidationError
+import re
 import json
 import random
 
@@ -50,13 +52,27 @@ class UserProfile(models.Model):
     oauth_token = models.CharField(max_length=1024, blank=False)
     oauth_secret = models.CharField(max_length=1024, blank=False)
 
-    jira_url = models.CharField(max_length=100, default='')
-    full_name = models.CharField(max_length=100, default='')
+    jira_url = models.CharField(max_length=100, null=True, blank=True, default='')
+    full_name = models.CharField(max_length=100, null=True, blank=True, default='')
     booking_privledge = models.BooleanField(default=False)
 
     class Meta:
         db_table = 'user_profile'
 
+    def clean(self, *args, **kwargs):
+        company = self.company
+        regex = r'[a-z\_\-\.\$]*'
+        pattern = re.compile(regex)
+
+        if not pattern.fullmatch(company):
+            raise ValidationError('Company may only include lowercase letters, _, -, . and $')
+
+        super().clean(*args, **kwargs)
+
+    def save(self, *args, **kwargs):
+        self.full_clean()
+        super().save(*args, **kwargs)
+
     def __str__(self):
         return self.user.username