@@ -1415,9 +1415,22 @@ class Profile(models.Model):
14151415 Additional information attached to a user account.
14161416 """
14171417
1418+ UNDERGRADUATE = 0
1419+ GRADUATE = 1
1420+ STAFF = 2
1421+
1422+ AFFILIATION_CHOICES = (
1423+ (UNDERGRADUATE , "Undergraduate" ),
1424+ (GRADUATE , "Graduate" ),
1425+ (STAFF , "Staff" ),
1426+ )
1427+
14181428 user = models .OneToOneField (
14191429 get_user_model (), on_delete = models .CASCADE , primary_key = True
14201430 )
1431+ affiliation = models .PositiveSmallIntegerField (
1432+ choices = AFFILIATION_CHOICES , default = UNDERGRADUATE
1433+ )
14211434 image = models .ImageField (upload_to = get_user_file_name , null = True , blank = True )
14221435 uuid_secret = models .UUIDField (default = uuid .uuid4 )
14231436
@@ -1428,6 +1441,36 @@ class Profile(models.Model):
14281441 school = models .ManyToManyField (School , blank = True )
14291442 major = models .ManyToManyField (Major , blank = True )
14301443
1444+ def detect_information (self ):
1445+ """
1446+ Try to detect appropriate values for profile fields based on what platform has
1447+ returned. Currently only supports detecting the affiliation.
1448+ This method is not very accurate and should only be used to provide the user
1449+ an initial guess.
1450+
1451+ Overwrites existing information.
1452+ """
1453+
1454+ # detect the affilation
1455+ if self .user .groups .filter (name = "platform_student" ):
1456+ # if the user has the student group from platform, they're probably student
1457+ domain = self .user .email .split ("@" , 1 )[- 1 ].lower ()
1458+ if domain in {
1459+ "nursing.upenn.edu" ,
1460+ "sas.upenn.edu" ,
1461+ "seas.upenn.edu" ,
1462+ "upenn.edu" ,
1463+ "wharton.upenn.edu" ,
1464+ }:
1465+ # domains commonly associated with undergrad schools marked as undergrad
1466+ self .affiliation = Profile .UNDERGRADUATE
1467+ else :
1468+ self .affiliation = Profile .GRADUATE
1469+ else :
1470+ self .affiliation = Profile .STAFF
1471+
1472+ self .save (update_fields = ["affiliation" ])
1473+
14311474 def __str__ (self ):
14321475 return self .user .username
14331476
0 commit comments