From 4949b123d69083f8980b2d4f5a0e431776b6a621 Mon Sep 17 00:00:00 2001 From: Yu Long <82516689+Reimirno@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:38:16 -0700 Subject: [PATCH] fix: photo id (#66) --- server/services/c1c/__init__.py | 6 +++--- server/services/c1c/fake_data/__init__.py | 1 - server/services/c1c/fake_data/photos.json | 8 ++++---- server/views.py | 23 ++++++++++++----------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/server/services/c1c/__init__.py b/server/services/c1c/__init__.py index 54c9c6e..d6b16af 100644 --- a/server/services/c1c/__init__.py +++ b/server/services/c1c/__init__.py @@ -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: diff --git a/server/services/c1c/fake_data/__init__.py b/server/services/c1c/fake_data/__init__.py index 46db5b9..be616de 100644 --- a/server/services/c1c/fake_data/__init__.py +++ b/server/services/c1c/fake_data/__init__.py @@ -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: diff --git a/server/services/c1c/fake_data/photos.json b/server/services/c1c/fake_data/photos.json index a25b343..e4cab10 100644 --- a/server/services/c1c/fake_data/photos.json +++ b/server/services/c1c/fake_data/photos.json @@ -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" } \ No newline at end of file diff --git a/server/views.py b/server/views.py index 69fdf81..8c6813a 100644 --- a/server/views.py +++ b/server/views.py @@ -902,17 +902,18 @@ def student(exam_student): @app.route('//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