diff --git a/backend/clubs/migrations/0078_profile_affiliation.py b/backend/clubs/migrations/0078_profile_affiliation.py new file mode 100644 index 000000000..3d9e0bb3b --- /dev/null +++ b/backend/clubs/migrations/0078_profile_affiliation.py @@ -0,0 +1,20 @@ +# Generated by Django 3.1.6 on 2021-03-14 14:13 + +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, "Graduate"), (2, "Staff")], default=0 + ), + ), + ] diff --git a/backend/clubs/models.py b/backend/clubs/models.py index 40da6ecab..975f0d92a 100644 --- a/backend/clubs/models.py +++ b/backend/clubs/models.py @@ -1415,9 +1415,22 @@ class Profile(models.Model): Additional information attached to a user account. """ + UNDERGRADUATE = 0 + GRADUATE = 1 + STAFF = 2 + + AFFILIATION_CHOICES = ( + (UNDERGRADUATE, "Undergraduate"), + (GRADUATE, "Graduate"), + (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 +1441,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"): + # 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.GRADUATE + 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..abfd21168 100644 --- a/frontend/components/Settings/ProfileForm.tsx +++ b/frontend/components/Settings/ProfileForm.tsx @@ -80,6 +80,12 @@ const ProfileForm = ({ } } + const affiliations = [ + { value: 0, label: 'Undergraduate Student' }, + { value: 1, label: 'Graduate/Professional Student' }, + { value: 2, label: 'Faculty or Staff Member' }, + ] + return ( <> + + affiliations.find((item) => item.value === val) + } + />