Skip to content

Commit

Permalink
fix: photo id (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reimirno authored Apr 17, 2024
1 parent 91f2861 commit 4949b12
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions server/services/c1c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def _make_request(self, path, method='GET'):
else:
return requests.request(method, url, auth=(self.username, self.password))

def get_student_photo(self, student_canvas_id):
def get_student_photo(self, sid):
if is_mock_c1c():
return fake_data.get_fake_photo(student_canvas_id)
return fake_data.get_fake_photo(sid)
try:
r = self._make_request(f'/c1c-api/v1/photo/{student_canvas_id}')
r = self._make_request(f'/c1c-api/v1/photo/{sid}')
if r.status_code == 200:
return r.content
else:
Expand Down
1 change: 0 additions & 1 deletion server/services/c1c/fake_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


def get_fake_photo(student_canvas_id):
print(FAKE_PHOTO_DICT)
try:
with open(f"server/services/c1c/fake_data/photos/{FAKE_PHOTO_DICT[student_canvas_id]}",
'rb') as f:
Expand Down
8 changes: 4 additions & 4 deletions server/services/c1c/fake_data/photos.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"123456": "sample1.jpg",
"234567": "sample1.jpg",
"345678": "sample1.jpg",
"456789": "sample1.jpg"
"3033033333": "sample1.jpg",
"3033033334": "sample1.jpg",
"3033033335": "sample1.jpg",
"3033033336": "sample1.jpg"
}
23 changes: 12 additions & 11 deletions server/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,17 +902,18 @@ def student(exam_student):
@app.route('/<exam_student:exam_student>/photo/', methods=['GET'])
def student_photo(exam_student):
_, student = exam_student
student_canvas_id = student.canvas_id
from server.cache import cache_store, cache_key_photo, cache_life_photo
import io
photo = cache_store.get(cache_key_photo(student_canvas_id))
if photo is not None:
return send_file(io.BytesIO(photo), mimetype='image/jpeg')
from server.services.c1c import c1c_client
photo = c1c_client.get_student_photo(student_canvas_id)
if photo is not None:
cache_store.set(cache_key_photo(student_canvas_id), photo, timeout=cache_life_photo)
return send_file(io.BytesIO(photo), mimetype='image/jpeg')
sid = student.sid
if sid:
from server.cache import cache_store, cache_key_photo, cache_life_photo
import io
photo = cache_store.get(cache_key_photo(sid))
if photo is not None:
return send_file(io.BytesIO(photo), mimetype='image/jpeg')
from server.services.c1c import c1c_client
photo = c1c_client.get_student_photo(sid)
if photo is not None:
cache_store.set(cache_key_photo(sid), photo, timeout=cache_life_photo)
return send_file(io.BytesIO(photo), mimetype='image/jpeg')
return send_file('static/img/photo-placeholder.png', mimetype='image/png')

# endregion
Expand Down

0 comments on commit 4949b12

Please sign in to comment.