Skip to content

Commit

Permalink
add field to profile indicating user type
Browse files Browse the repository at this point in the history
  • Loading branch information
ezwang committed Mar 14, 2021
1 parent 4babe24 commit 855fd3a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/clubs/migrations/0078_profile_affiliation.py
Original file line number Diff line number Diff line change
@@ -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
),
),
]
43 changes: 43 additions & 0 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions backend/clubs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,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")

Expand Down Expand Up @@ -1831,6 +1832,7 @@ def update(self, instance, validated_data):
class Meta:
model = get_user_model()
fields = [
"affiliation",
"email",
"graduation_year",
"has_been_prompted",
Expand Down
14 changes: 14 additions & 0 deletions frontend/components/Settings/ProfileForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<Formik
Expand All @@ -97,6 +103,14 @@ const ProfileForm = ({
isImage
/>
<Field name="graduation_year" as={TextField} type="number" />
<Field
name="affiliation"
as={SelectField}
choices={affiliations}
valueDeserialize={(val) =>
affiliations.find((item) => item.value === val)
}
/>
<Field name="school" as={SelectField} choices={schools} isMulti />
<Field name="major" as={SelectField} choices={majors} isMulti />
<Field
Expand Down

0 comments on commit 855fd3a

Please sign in to comment.