From 179254935fcfaed98343cfc3ec632b7e9b5f5ef5 Mon Sep 17 00:00:00 2001 From: Aditya Khambete Date: Fri, 18 Oct 2024 00:16:20 +0530 Subject: [PATCH] Update models.py Added the additional roll number constraints accd to https://asc.iitb.ac.in/acadmenu/notices/New_Roll_Number_Syntax_2022.pdf --- socbackend/accounts/models.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/socbackend/accounts/models.py b/socbackend/accounts/models.py index 14877ce..f9e9693 100644 --- a/socbackend/accounts/models.py +++ b/socbackend/accounts/models.py @@ -1,12 +1,24 @@ from django.db import models from django.contrib.auth.models import User from .options import DepartmentChoices, YearChoices +from django.core.exceptions import ValidationError def upload_to_location(instance, filename): return "profile_pictures/{filename}".format(filename=filename) +def validate_roll_number(value): + # Extract the first two digits (batch/year) to determine the format + batch_number = int(value[:2]) + if batch_number >= 22: + # For batches >= 22, the format is BBPXXXX (no specific constraint on P) + if not value[2:3].isalpha() or not value[3:].isdigit() or len(value) != 7: + raise ValidationError("Enter a valid roll number.") + else: + # For batches < 22, the format is YYPDDCNNN (no specific constraint on P, DD, or C) + if not (len(value) == 9 and value[2].isalpha() and value[3:5].isalpha() and value[5:6].isdigit() and value[6:].isdigit()): + raise ValidationError("Enter a valid roll number.") @@ -24,9 +36,10 @@ class UserProfile(models.Model): phone_number = models.CharField(max_length=15, blank=False, null=False) roll_number = models.CharField( "roll number", - max_length=20, + max_length=9, unique=True, - help_text="Required. 20 characters or fewer.", + help_text="Required. 9 characters or fewer.", # Adjust max_length to 9 to accommodate the longest format (YYPDDCNNN) + validators=[validate_roll_number], error_messages={ "unique": "A user with that roll number already exists.", },