Coverage for application/main.py: 28%

53 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-23 02:22 +0000

1import traceback 

2from json import JSONDecodeError 

3 

4from flask import flash, render_template, request, session 

5 

6from application import app 

7from application.vars.annosaurus import * 

8 

9 

10@app.route('/favicon.ico') 

11def favicon(): 

12 return app.send_static_file('img/favicon.ico') 

13 

14 

15@app.route('/') 

16def index(): 

17 unread_comments = 0 

18 read_comments = 0 

19 total_comments = 0 

20 active_reviewers = [] 

21 try: 

22 # get list of reviewers from external review db 

23 with requests.get( 

24 url=f'{app.config.get("DARC_REVIEW_URL")}/reviewer/all', 

25 headers=app.config.get('DARC_REVIEW_HEADERS'), 

26 ) as reviewers_res: 

27 session['reviewers'] = reviewers_res.json() 

28 # get stats from external review db 

29 with requests.get( 

30 url=f'{app.config.get("DARC_REVIEW_URL")}/stats', 

31 headers=app.config.get('DARC_REVIEW_HEADERS'), 

32 ) as stats_res: 

33 stats_json = stats_res.json() 

34 unread_comments = stats_json['unread_comments'] 

35 read_comments = stats_json['read_comments'] 

36 total_comments = stats_json['total_comments'] 

37 active_reviewers = stats_json['active_reviewers'] 

38 except (JSONDecodeError, KeyError, requests.exceptions.ConnectionError): 

39 flash('Unable to connect to external review server', 'danger') 

40 print('\nERROR: unable to connect to external review server\n') 

41 session['reviewers'] = [] 

42 try: 

43 # get list of sequences from vars 

44 with requests.get(url=app.config.get('VARS_SEQUENCE_LIST_URL')) as sequences_res: 

45 session['vars_video_sequences'] = sequences_res.json() 

46 # get concept list from vars (for input validation) 

47 with requests.get(url=app.config.get('VARS_CONCEPT_LIST_URL')) as concept_res: 

48 session['vars_concepts'] = concept_res.json() 

49 except requests.exceptions.ConnectionError: 

50 print('\nERROR: unable to connect to VARS\n') 

51 flash('Unable to connect to VARS', 'danger') 

52 session['vars_video_sequences'] = [] 

53 session['vars_concepts'] = [] 

54 return render_template( 

55 'index.html', 

56 sequences=session['vars_video_sequences'], 

57 unread_comment_count=unread_comments, 

58 read_comment_count=read_comments, 

59 total_comment_count=total_comments, 

60 active_reviewers=active_reviewers, 

61 ) 

62 

63 

64# video player 

65@app.get('/video') 

66def video(): 

67 data = {'link': request.args.get('link'), 'time': request.args.get('time')} 

68 return render_template('video.html', data=data), 200 

69 

70 

71@app.errorhandler(404) 

72def page_not_found(e): 

73 return render_template('errors/404.html', err=''), 404 

74 

75 

76@app.errorhandler(Exception) 

77def server_error(e): 

78 error = f'{type(e).__name__}: {e}' 

79 print('\nApplication error 😔') 

80 print(error) 

81 print(traceback.format_exc()) 

82 requests.post( 

83 url=f'{app.config.get("DARC_REVIEW_URL")}/log-error', 

84 headers=app.config.get('DARC_REVIEW_HEADERS'), 

85 json={'error': traceback.format_exc()}, 

86 ) 

87 return render_template('errors/500.html', err=error), 500