|
1 | 1 | """ |
2 | 2 | Implementation of "credit" XBlock service |
3 | 3 | """ |
| 4 | +from datetime import datetime, timedelta |
4 | 5 | import logging |
5 | 6 |
|
6 | 7 | from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user |
@@ -100,11 +101,30 @@ def get_credit_state(self, user_id, course_key_or_id, return_course_info=False): |
100 | 101 | } |
101 | 102 |
|
102 | 103 | if return_course_info: |
103 | | - course_overview = CourseOverview.get_from_id(course_key) |
104 | | - result.update({ |
105 | | - 'course_name': course_overview.display_name, |
106 | | - 'course_end_date': course_overview.end, |
107 | | - }) |
| 104 | + try: |
| 105 | + course_overview = CourseOverview.get_from_id(course_key) |
| 106 | + result.update({ |
| 107 | + 'course_name': course_overview.display_name, |
| 108 | + 'course_end_date': course_overview.end, |
| 109 | + }) |
| 110 | + except CourseOverview.DoesNotExist: |
| 111 | + # NOTE: Since the caller requested "return_course_info=True" and we don't have course to get that info. |
| 112 | + # Also, The "get_credit_state" is called from several places directly or indirectly (Mostly from |
| 113 | + # exams/grades services) which relatively depend upon course end date. |
| 114 | + # As per the current structure of edX the exam attempts can exist without a course and they can request |
| 115 | + # credit state so, the safest options would be to send some date/time in the past (One hour in past to |
| 116 | + # be safe) so that those attempts can be marked or treated as expired/completed instead of any other |
| 117 | + # status that could be error prone. |
| 118 | + |
| 119 | + one_hour_past = datetime.now() - timedelta(hours=1) |
| 120 | + result.update({ |
| 121 | + 'course_end_date': one_hour_past, |
| 122 | + }) |
| 123 | + log.exception( |
| 124 | + "Could not get name and end_date for course %s, This happened because we were unable to " |
| 125 | + "get/create CourseOverview object for the course. It's possible that the Course has been deleted.", |
| 126 | + str(course_key), |
| 127 | + ) |
108 | 128 | return result |
109 | 129 |
|
110 | 130 | def set_credit_requirement_status(self, user_id, course_key_or_id, req_namespace, |
|
0 commit comments