def get_current_semester(allow_not_found=False): """ This function retrieves the string value of the current semester, either from memory (if the value has been cached), or from the db (after which it will cache the value for future use). If the value retrieved from the db is None, an error is thrown indicating that the SEMESTER Option must be set for this API to work properly. You can prevent an error from being thrown (and cause the function to just return None in this case) by setting allow_not_found=True. The cache has a timeout of 25 hours, but is also invalidated whenever the SEMESTER Option is saved (which will occur whenever it is updated), using a post_save hook. See the invalidate_current_semester_cache function below to see how this works. """ cached_val = cache.get("SEMESTER", None) if cached_val is not None: return cached_val retrieved_val = get_value("SEMESTER", None) if not allow_not_found and retrieved_val is None: raise APIException( "The SEMESTER runtime option is not set. If you are in dev, you can set this " "option by running the command " "'python manage.py setoption SEMESTER 2020C', " "replacing 2020C with the current semester, in the backend directory (remember " "to run 'pipenv shell' before running this command, though).") cache.set("SEMESTER", retrieved_val, timeout=90000) # cache expires every 25 hours return retrieved_val
def generate_course_json(semester=None, use_cache=True): if semester is None: semester = get_value('SEMESTER') if use_cache: sections = r.get('sections') if sections is not None: return json.loads(sections) sections = [] for section in Section.objects.filter(course__semester=semester): # {'section_id': section_id, 'course_title': course_title, 'instructors': instructors, # 'meeting_days': meeting_days} # meetings = json.loads('{"meetings": "%s"}' % section.meeting_times)['meetings'] if section.meeting_times is not None and len(section.meeting_times) > 0: meetings = json.loads(section.meeting_times) else: meetings = [] sections.append({ 'section_id': section.normalized, 'course_title': section.course.title, 'instructors': list(map(lambda i: i.name, section.instructors.all())), 'meeting_days': meetings }) serialized_sections = json.dumps(sections) r.set('sections', serialized_sections) return sections
def prepare_alerts(semester=None): if semester is None: semester = get_value('SEMESTER') for section_code, registrations in collect_registrations(semester).items(): send_alerts_for.delay(section_code, registrations, semester) return {'task': 'pca.tasks.prepare_alerts', 'result': 'complete'}
def load_courses(query='', semester=None): if semester is None: semester = get_value('SEMESTER') logger.info('load in courses with prefix %s from %s' % (query, semester)) results = api.get_courses(query, semester) for course in results: upsert_course_from_opendata(course, semester) return {'result': 'succeeded', 'name': 'pca.tasks.load_courses'}
def send_alerts_from_status(semester=None): if semester is None: semester = get_value('SEMESTER') courses = api.get_all_course_availability(semester) for course in courses: course_id = course['course_section'] if course['status'] == 'O': try: for reg in get_active_registrations(course_id, semester): send_alert.delay(reg.id, 'SERV') except: pass
def send_course_alerts(course_code, semester=None, sent_by=''): if semester is None: semester = get_value('SEMESTER') for reg in get_active_registrations(course_code, semester): send_alert.delay(reg.id, sent_by)
def test_get_value_no_exists(self): self.assertIsNone(get_value("invalid"))
def test_get_value_exists(self): self.assertEqual(get_value(self.key), self.value)
def get_current_semester(): return get_value('SEMESTER', '2019A')