From 718d797d9a21b8b8c7e89f978eba378d7e930808 Mon Sep 17 00:00:00 2001
From: msb <mike@msb.me.uk>
Date: Tue, 23 Apr 2019 10:44:54 +0100
Subject: [PATCH] Add validate_groupid_list() (and validate_groupids() for
 completeness). Also refactored validate_crsids() into validate_crsids() and
 validate_crsid_list().

---
 ucamlookup/utils.py | 73 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 66 insertions(+), 7 deletions(-)

diff --git a/ucamlookup/utils.py b/ucamlookup/utils.py
index edcb37f..3cc2a15 100644
--- a/ucamlookup/utils.py
+++ b/ucamlookup/utils.py
@@ -155,17 +155,32 @@ def get_or_create_user_by_crsid(crsid):
 
 
 def validate_crsids(crsids_text):
-    """ Validates the list of authorsied users from input
-        :param crsids_text: list of crsids from the form
-        :return: The list of users
     """
+    Validates a comma seperated list of crsids returning a list of User objects
 
-    users = ()
+    This is now deprecated in favour of validate_crsid_list() which should be used in conjunction
+    with the <select> element.
 
+    :param crsids_text: a comma seperated list of crsids
+    :return: The list of User objects
+    """
     if crsids_text is None:
-        return users
+        return ()
+
+    return validate_crsid_list(crsids_text.split(','))
+
+
+def validate_crsid_list(crsids):
+    """
+    Validates the list of crsids returning a list of User objects
+
+    :param crsids: list of crsids
+    :return: The list of User objects
+    """
+    users = ()
 
-    crsids = crsids_text.split(',')
+    if crsids is None:
+        return users
 
     if len(crsids) == 1 and crsids[0] == '':
         return users
@@ -175,7 +190,7 @@ def validate_crsids(crsids_text):
         if crsid_re.match(crsid):
             users += (get_or_create_user_by_crsid(crsid),)
         else:
-            raise ValidationError("The list of users contains an invalid user")
+            raise ValidationError("The list of users contains an invalid user: {}".format(crsid))
 
     return users
 
@@ -188,3 +203,47 @@ def get_users_of_a_group(group):
 
     return list(map(lambda user: get_or_create_user_by_crsid(user.identifier),
                     GroupMethods(get_connection()).getMembers(groupid=group.groupid)))
+
+
+def validate_groupids(groupids_text):
+    """
+    Validates a comma seperated list of groupids returning a list of LookupGroup objects
+
+    This is now deprecated in favour of validate_groupid_list() which should be used in conjunction
+    with the <select> element.
+
+    :param crsids_text: a comma seperated list of groupids
+    :return: The list of LookupGroup objects
+    """
+    if groupids_text is None:
+        return ()
+
+    return validate_groupid_list(groupids_text.split(','))
+
+
+def validate_groupid_list(groupids):
+    """
+    Validates a comma seperated list of groupids returning a list of LookupGroup objects
+
+    :param crsids_text: a list of groupids
+    :return: The list of LookupGroup objects
+    """
+    groups = ()
+
+    if groupids is None:
+        return groups
+
+    if len(groupids) == 1 and groupids[0] == '':
+        return groups
+
+    groupid_re = re.compile(r'^[0-9]{1,6}$')
+
+    for groupid in groupids:
+        if groupid_re.match(groupid):
+            groups += (get_or_create_group_by_groupid(int(groupid)),)
+        else:
+            raise ValidationError("The list of groups contains an invalid group")
+
+    return groups
+
+
-- 
GitLab