Skip to content

Commit

Permalink
Merge pull request #15 from AdityaK1729/adityaRollNoFix
Browse files Browse the repository at this point in the history
Update models.py (fixes roll numbers)
  • Loading branch information
coldicedcoffee authored Oct 20, 2024
2 parents 083e51d + 1792549 commit e76d6a0
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions socbackend/accounts/models.py
Original file line number Diff line number Diff line change
@@ -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.")



Expand All @@ -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.",
},
Expand Down

0 comments on commit e76d6a0

Please sign in to comment.