Skip to content

Commit

Permalink
Merge pull request #376 from uw-it-aca/task/appuser-query
Browse files Browse the repository at this point in the history
update get_appuser_by_uwnetid method to return a minimal set of attri…
  • Loading branch information
jlaney committed Feb 2, 2024
2 parents a00ef3d + e1cd2f0 commit 8d51142
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
13 changes: 12 additions & 1 deletion compass/clients/mock_person_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ def get_adviser_caseload(self, uwnetid):
return self.get_persons_by_adviser_netid(uwnetid)

def get_appuser_by_uwnetid(self, uwnetid):
return self.get_person_by_uwnetid(uwnetid)
return self.get_person_by_uwnetid(
uwnetid,
include_employee=False,
include_student=False,
include_student_transcripts=False,
include_student_transfers=False,
include_student_sports=False,
include_student_advisers=False,
include_student_majors=False,
include_student_pending_majors=False,
include_student_holds=False,
include_student_degrees=False)
28 changes: 24 additions & 4 deletions compass/clients/person_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,34 @@ def get_adviser_caseload(self, uwnetid):
return sorted(persons.values(), key=lambda p: p.surname)

def get_appuser_by_uwnetid(self, uwnetid):
"""
Returns a cached "thin" person model for use with AppUser.
"""
cache_key = f'appuser_{uwnetid}'

person_data = cache.get(cache_key)
if person_data is not None:
return Person().from_dict(person_data)

expires = getattr(settings, 'APPUSER_PERSON_EXPIRES', 60 * 60 * 24)
person = self.get_person_by_uwnetid(
uwnetid, include_employee=False, include_student=False)
cache.set(cache_key, person.to_dict(), timeout=expires)
sqla_person = self.DB.session.query(self.DB.Person).filter(
or_(self.DB.Person.uwnetid == uwnetid,
self.DB.Person.prior_uwnetids.any(uwnetid))
).one_or_none()

if sqla_person is None:
raise PersonNotFoundException()

person = Person()
person.uwnetid = sqla_person.uwnetid
person.uwregid = sqla_person.uwregid
person.prior_uwnetids = sqla_person.prior_uwnetids
person.prior_uwregids = sqla_person.prior_uwregids
person.system_key = sqla_person.system_key
person.display_name = sqla_person.display_name

cache.set(
cache_key,
person.to_dict(),
timeout=getattr(settings, 'APPUSER_PERSON_EXPIRES', 60 * 60 * 24)
)
return person
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"prior_uwregids": [],
"pronouns": "He/him/his",
"surname": "Adviser",
"system_key": null,
"uwnetid": "jadviser",
"uwregid": "5136CCB9F66711D5BE060004AC494FF0",
"whitepages_publish": false
Expand Down Expand Up @@ -379,6 +380,7 @@
"visa_type": "F1"
},
"surname": "Average",
"system_key": "532353230",
"uwnetid": "javerage",
"uwregid": "9136CCB8F66711D5BE060004AC494FFE",
"whitepages_publish": true
Expand Down
3 changes: 1 addition & 2 deletions compass/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def upsert_appuser(self, uwnetid):
needed.
"""
# request the current person object for the user
person = CompassPersonClient().get_person_by_uwnetid(
uwnetid, include_employee=False, include_student=False)
person = CompassPersonClient().get_appuser_by_uwnetid(uwnetid)
# check the AppUser table to see if they have an existing entry
user = None
for netid in person.prior_uwnetids + [person.uwnetid]:
Expand Down
2 changes: 1 addition & 1 deletion compass/tests/views/api/test_visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_valid_student(self):

@patch('compass.views.api.visit.Visit')
@patch('compass.views.api.visit.VisitType')
@patch('compass.views.api.visit.UWPersonClient')
@patch('compass.views.api.visit.CompassPersonClient')
@patch('compass.views.api.visit.Student')
@patch('compass.views.api.visit.AccessGroup')
def test_post(self, mock_access_group_cls,
Expand Down
14 changes: 4 additions & 10 deletions compass/views/api/visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from compass.views.api import BaseAPIView, TokenAPIView
from compass.models import Visit, Student, AccessGroup, VisitType
from compass.serializers import VisitReadSerializer, VisitTypeSerializer
from uw_person_client import UWPersonClient
from compass.clients import CompassPersonClient
from rest_framework.response import Response
from rest_framework import status
from dateutil import parser
Expand Down Expand Up @@ -51,18 +51,12 @@ def _valid_student(self, netid):
raise ValueError('Missing Student NetID')

try:
person = UWPersonClient().get_person_by_uwnetid(
netid, include_employee=False, include_student=True,
include_student_transcripts=False,
include_student_transfers=False, include_student_sports=False,
include_student_advisers=False, include_student_majors=False,
include_student_pending_majors=False,
include_student_holds=False, include_student_degrees=False)
person = CompassPersonClient().get_appuser_by_uwnetid(netid)
except Exception as ex:
raise ValueError(f'IC Visit Error: {netid}: {ex}')

syskey = person.student.system_key
student, created = Student.objects.get_or_create(system_key=syskey)
student, created = Student.objects.get_or_create(
system_key=person.system_key)
return student

def _valid_visit_type(self, name, access_group):
Expand Down

0 comments on commit 8d51142

Please sign in to comment.