def _stow(func, *args, **kw): args_dict = get_args_dict(func, *args, **kw) key = key_pattern.format(**args_dict) if for_term: term_name = term_name_for_sis_id(args_dict.get('term_id')) key = f'term_{term_name}-{key}' stowed = JsonCache.query.filter_by(key=key).first() # Note that the query returns a DB row rather than the value of the JSON column. if stowed is not None: app.logger.debug(f'Returning stowed JSON for key {key}') return stowed.json else: app.logger.info(f'{key} not found in runtime DB') to_stow = func(*args, **kw) if to_stow is not None: app.logger.debug(f'Will stow JSON for key {key}') insert_row(key=key, json=to_stow) else: app.logger.info(f'{key} not generated and will not be stowed in DB') return to_stow
def _to_api_json(alert): term_id_match = re.search(r'^2[012]\d[0258]', alert.key[0:4]) active_until = alert.deleted_at or utc_now() return { 'sid': alert.sid, 'term': term_name_for_sis_id(term_id_match.string) if term_id_match else None, 'key': alert.key, 'type': alert.alert_type, 'is_active': not alert.deleted_at, 'active_duration_hours': round( (active_until - alert.created_at).total_seconds() / 3600), 'created_at': alert.created_at, 'deleted_at': alert.deleted_at, }
def test_term_name_for_sis_id(self): assert berkeley.term_name_for_sis_id('2178') == 'Fall 2017' assert berkeley.term_name_for_sis_id('1978') == 'Fall 1997'
def iter_csv(): def csv_line(_list): csv_output = io.StringIO() csv.writer(csv_output).writerow(_list) return csv_output.getvalue().encode('utf-8') csv_output.close() yield csv_line([ 'date_created', 'student_sid', 'student_name', 'author_uid', 'author_csid', 'author_name', 'subject', 'topics', 'attachments', 'body', 'is_private', 'late_change_request_action', 'late_change_request_status', 'late_change_request_term', 'late_change_request_course', ]) supplemental_calnet_advisor_feeds = get_calnet_users_for_csids( app, list( set([ note['author']['sid'] for note in notes if note['author']['sid'] and not note['author']['name'] ])), ) for note in notes: calnet_author = supplemental_calnet_advisor_feeds.get( note['author']['sid']) if calnet_author: calnet_author_name =\ calnet_author.get('name') or join_if_present(' ', [calnet_author.get('firstName'), calnet_author.get('lastName')]) calnet_author_uid = calnet_author.get('uid') else: calnet_author_name = None calnet_author_uid = None # strptime expects a timestamp without timezone; ancient date-only legacy notes get a bogus time appended. timestamp_created = f"{note['createdAt']}T12:00:00" if len( note['createdAt']) == 10 else note['createdAt'][:19] datetime_created = pytz.utc.localize( datetime.strptime(timestamp_created, '%Y-%m-%dT%H:%M:%S')) date_local = datetime_created.astimezone(app_timezone).strftime( '%Y-%m-%d') e_form = note.get('eForm') or {} omit_note_body = note.get( 'isPrivate') and not current_user.can_access_private_notes yield csv_line([ date_local, student['sid'], join_if_present(' ', [ student.get('first_name', ''), student.get('last_name', '') ]), (note['author']['uid'] or calnet_author_uid), note['author']['sid'], (note['author']['name'] or calnet_author_name), note['subject'], '; '.join([t for t in note['topics'] or []]), '' if omit_note_body else '; '.join( [a['displayName'] for a in note['attachments'] or []]), '' if omit_note_body else note['body'], note.get('isPrivate'), e_form.get('action'), e_form.get('status'), term_name_for_sis_id(e_form.get('term')), f"{e_form['sectionId']} {e_form['courseName']} - {e_form['courseTitle']} {e_form['section']}" if e_form.get('sectionId') else None, ])
def _term_option(term_id): term_name = term_name_for_sis_id(term_id) + ( ' (active)' if term_id == current_term else ' (future)') return {'name': term_name, 'value': term_id}
def entering_terms(): term_ids = [r['entering_term'] for r in data_loch.get_entering_terms()] return [{ 'name': ' '.join(term_name_for_sis_id(term_id).split()[::-1]), 'value': term_id } for term_id in term_ids]
def degree_terms(): term_ids = [r['term_id'] for r in data_loch.get_distinct_degree_term_ids()] return [{ 'name': ' '.join(term_name_for_sis_id(term_id).split()[::-1]), 'value': term_id } for term_id in term_ids]