diff --git a/backend/clubs/migrations/0078_profile_affiliation.py b/backend/clubs/migrations/0078_profile_affiliation.py new file mode 100644 index 000000000..cc979a686 --- /dev/null +++ b/backend/clubs/migrations/0078_profile_affiliation.py @@ -0,0 +1,27 @@ +# Generated by Django 3.1.6 on 2021-04-11 17:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("clubs", "0077_auto_20210219_2014"), + ] + + operations = [ + migrations.AddField( + model_name="profile", + name="affiliation", + field=models.PositiveSmallIntegerField( + choices=[ + (0, "Undergraduate"), + (1, "Masters"), + (2, "Professional"), + (3, "Doctoral"), + (4, "Staff"), + ], + default=0, + ), + ), + ] diff --git a/backend/clubs/models.py b/backend/clubs/models.py index 40da6ecab..24e7c52af 100644 --- a/backend/clubs/models.py +++ b/backend/clubs/models.py @@ -1415,9 +1415,26 @@ class Profile(models.Model): Additional information attached to a user account. """ + UNDERGRADUATE = 0 + MASTERS = 1 + PROFESSIONAL = 2 + PHD = 3 + STAFF = 4 + + AFFILIATION_CHOICES = ( + (UNDERGRADUATE, "Undergraduate"), + (MASTERS, "Masters"), + (PROFESSIONAL, "Professional"), + (PHD, "Doctoral"), + (STAFF, "Staff"), + ) + user = models.OneToOneField( get_user_model(), on_delete=models.CASCADE, primary_key=True ) + affiliation = models.PositiveSmallIntegerField( + choices=AFFILIATION_CHOICES, default=UNDERGRADUATE + ) image = models.ImageField(upload_to=get_user_file_name, null=True, blank=True) uuid_secret = models.UUIDField(default=uuid.uuid4) @@ -1428,6 +1445,36 @@ class Profile(models.Model): school = models.ManyToManyField(School, blank=True) major = models.ManyToManyField(Major, blank=True) + def detect_information(self): + """ + Try to detect appropriate values for profile fields based on what platform has + returned. Currently only supports detecting the affiliation. + This method is not very accurate and should only be used to provide the user + an initial guess. + + Overwrites existing information. + """ + + # detect the affilation + if self.user.groups.filter(name="platform_student").exists(): + # if the user has the student group from platform, they're probably student + domain = self.user.email.split("@", 1)[-1].lower() + if domain in { + "nursing.upenn.edu", + "sas.upenn.edu", + "seas.upenn.edu", + "upenn.edu", + "wharton.upenn.edu", + }: + # domains commonly associated with undergrad schools marked as undergrad + self.affiliation = Profile.UNDERGRADUATE + else: + self.affiliation = Profile.MASTERS + else: + self.affiliation = Profile.STAFF + + self.save(update_fields=["affiliation"]) + def __str__(self): return self.user.username diff --git a/backend/clubs/serializers.py b/backend/clubs/serializers.py index 17a84fdd5..6024e3204 100644 --- a/backend/clubs/serializers.py +++ b/backend/clubs/serializers.py @@ -1783,6 +1783,7 @@ class UserSerializer(serializers.ModelSerializer): graduation_year = serializers.IntegerField( source="profile.graduation_year", allow_null=True ) + affiliation = serializers.IntegerField(source="profile.affiliation") school = SchoolSerializer(many=True, source="profile.school") major = MajorSerializer(many=True, source="profile.major") @@ -1841,6 +1842,7 @@ def update(self, instance, validated_data): class Meta: model = get_user_model() fields = [ + "affiliation", "email", "graduation_year", "has_been_prompted", diff --git a/frontend/components/Settings/ProfileForm.tsx b/frontend/components/Settings/ProfileForm.tsx index a0adb6e42..6dff98848 100644 --- a/frontend/components/Settings/ProfileForm.tsx +++ b/frontend/components/Settings/ProfileForm.tsx @@ -80,6 +80,14 @@ const ProfileForm = ({ } } + const affiliations = [ + { value: 0, label: 'Undergraduate Student' }, + { value: 1, label: 'Masters Student' }, + { value: 2, label: 'Professional Student' }, + { value: 3, label: 'Doctoral Student' }, + { value: 4, label: 'Faculty or Staff Member' }, + ] + return ( <> + + affiliations.find((item) => item.value === val) + } + />