Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/Platform/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,6 @@
# Media Upload Settings
MEDIA_ROOT = os.path.join(BASE_DIR, "accounts", "mediafiles")
MEDIA_URL = "/media/"

PENN_GROUPS_USER = os.environ.get("PENN_GROUPS_USER", "")
PENN_GROUPS_PWD = os.environ.get("PENN_GROUPS_PWD", "")
1 change: 1 addition & 0 deletions backend/Platform/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
path("options/", include("options.urls", namespace="options")),
path("identity/", include("identity.urls", namespace="identity")),
path("healthcheck/", include("health.urls", namespace="healthcheck")),
path("penngroups/", include("penngroups.urls", namespace="penngroups")),
path("s/", include("shortener.urls", namespace="shortener")),
path(
"openapi/",
Expand Down
26 changes: 16 additions & 10 deletions backend/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ def ensure_student_object(sender, instance, created, **kwargs):
This post_save hook triggers automatically when a User object is saved, and if no Student
object exists for that User, it will create one
"""
Student.objects.get_or_create(user=instance)
# Only create Student object if the User instance has been saved to the database
# (has a primary key) to avoid "Model instances passed to related filters must be saved" error
if instance.pk:
Student.objects.get_or_create(user=instance)


class Email(models.Model):
Expand Down Expand Up @@ -180,12 +183,15 @@ def load_privacy_settings(sender, instance, created, **kwargs):
privacy settings for the User
"""

# In most cases, first checking if settings exists should reduce the number of queries
# to the database
if not instance.privacy_setting.exists():
resources = PrivacyResource.objects.all()
settings = [
PrivacySetting(user=instance, resource=resource, enabled=True)
for resource in resources
]
PrivacySetting.objects.bulk_create(settings, ignore_conflicts=True)
# Only access relationships if the User instance has been saved to the database
# (has a primary key) to avoid "User instance needs to have a primary key value" error
if instance.pk:
# In most cases, first checking if settings exists should reduce the number of queries
# to the database
if not instance.privacy_setting.exists():
resources = PrivacyResource.objects.all()
settings = [
PrivacySetting(user=instance, resource=resource, enabled=True)
for resource in resources
]
PrivacySetting.objects.bulk_create(settings, ignore_conflicts=True)
Empty file added backend/penngroups/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions backend/penngroups/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class PennGroupsConfig(AppConfig):
name = "penngroups"
9 changes: 9 additions & 0 deletions backend/penngroups/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from penngroups.views import PennGroupsGetGroupsView


app_name = "penngroups"

urlpatterns = [
path("groups/", PennGroupsGetGroupsView.as_view(), name="agh"),
]
18 changes: 18 additions & 0 deletions backend/penngroups/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import requests
from http import HTTPStatus
from django.http import JsonResponse
from django.views.generic import View

from Platform.settings.base import PENN_GROUPS_PWD, PENN_GROUPS_USER


class PennGroupsGetGroupsView(View):
def get(self, request):
if not request.user.is_authenticated:
return JsonResponse({"message": "Unauthorized"}, status=HTTPStatus.UNAUTHORIZED)
if request.user.pennid is None:
return JsonResponse({"message": "Unknown user"}, status=HTTPStatus.NOT_FOUND)

url = f"https://grouperWs.apps.upenn.edu/grouperWs/servicesRest/4.9.3/subjects/{request.user.pennid}/groups"
response = requests.get(url, auth=(PENN_GROUPS_USER, PENN_GROUPS_PWD))
return JsonResponse(response.json(), status=HTTPStatus.OK)
Loading