Skip to content

Commit 855fd3a

Browse files
committed
add field to profile indicating user type
1 parent 4babe24 commit 855fd3a

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 3.1.6 on 2021-03-14 14:13
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("clubs", "0077_auto_20210219_2014"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="profile",
15+
name="affiliation",
16+
field=models.PositiveSmallIntegerField(
17+
choices=[(0, "Undergraduate"), (1, "Graduate"), (2, "Staff")], default=0
18+
),
19+
),
20+
]

backend/clubs/models.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

backend/clubs/serializers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,7 @@ class UserSerializer(serializers.ModelSerializer):
17731773
graduation_year = serializers.IntegerField(
17741774
source="profile.graduation_year", allow_null=True
17751775
)
1776+
affiliation = serializers.IntegerField(source="profile.affiliation")
17761777
school = SchoolSerializer(many=True, source="profile.school")
17771778
major = MajorSerializer(many=True, source="profile.major")
17781779

@@ -1831,6 +1832,7 @@ def update(self, instance, validated_data):
18311832
class Meta:
18321833
model = get_user_model()
18331834
fields = [
1835+
"affiliation",
18341836
"email",
18351837
"graduation_year",
18361838
"has_been_prompted",

frontend/components/Settings/ProfileForm.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ const ProfileForm = ({
8080
}
8181
}
8282

83+
const affiliations = [
84+
{ value: 0, label: 'Undergraduate Student' },
85+
{ value: 1, label: 'Graduate/Professional Student' },
86+
{ value: 2, label: 'Faculty or Staff Member' },
87+
]
88+
8389
return (
8490
<>
8591
<Formik
@@ -97,6 +103,14 @@ const ProfileForm = ({
97103
isImage
98104
/>
99105
<Field name="graduation_year" as={TextField} type="number" />
106+
<Field
107+
name="affiliation"
108+
as={SelectField}
109+
choices={affiliations}
110+
valueDeserialize={(val) =>
111+
affiliations.find((item) => item.value === val)
112+
}
113+
/>
100114
<Field name="school" as={SelectField} choices={schools} isMulti />
101115
<Field name="major" as={SelectField} choices={majors} isMulti />
102116
<Field

0 commit comments

Comments
 (0)