Coverage for application/image_review/tator/routes.py: 18%

40 statements  

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

1""" 

2Tator-specific image review endpoint. 

3 

4/image-review/tator [GET] 

5""" 

6 

7import tator 

8import requests 

9from flask import current_app, flash, render_template, redirect, request, session 

10 

11from . import tator_image_review_bp 

12from application.image_review.tator.tator_localization_processor import TatorLocalizationProcessor 

13 

14 

15# view all Tator annotations (localizations) in a specified project & section 

16@tator_image_review_bp.get('') 

17def tator_image_review(): 

18 if 'tator_token' not in session.keys(): 

19 return redirect('/') 

20 if not request.args.get('project') or not request.args.get('section') or not request.args.get('deployment'): 

21 flash('Please select a project, section, and deployment', 'info') 

22 return redirect('/') 

23 project_id = int(request.args.get('project')) 

24 section_id = int(request.args.get('section')) 

25 try: 

26 api = tator.get_api( 

27 host=current_app.config.get('TATOR_URL'), 

28 token=session['tator_token'], 

29 ) 

30 localization_processor = TatorLocalizationProcessor( 

31 project_id=project_id, 

32 section_id=section_id, 

33 api=api, 

34 deployment_list=request.args.getlist('deployment'), 

35 tator_url=current_app.config.get('TATOR_URL'), 

36 ) 

37 localization_processor.fetch_localizations() 

38 localization_processor.load_phylogeny() 

39 localization_processor.process_records() 

40 except tator.openapi.tator_openapi.exceptions.ApiException: 

41 flash('Please log in to Tator', 'info') 

42 return redirect('/') 

43 comments = {} 

44 image_refs = {} 

45 deployments = request.args.getlist('deployment') 

46 # get comments and image ref list from external review db 

47 try: 

48 for deployment in deployments: 

49 comment_res = requests.get( 

50 url=f'{current_app.config.get("DARC_REVIEW_URL")}/comment/sequence/{deployment.replace("-", "_")}', 

51 headers=current_app.config.get('DARC_REVIEW_HEADERS'), 

52 ) 

53 if comment_res.status_code != 200: 

54 raise requests.exceptions.ConnectionError 

55 comments |= comment_res.json() # merge dicts 

56 image_ref_res = requests.get(f'{current_app.config.get("DARC_REVIEW_URL")}/image-reference/quick') 

57 if image_ref_res.status_code != 200: 

58 raise requests.exceptions.ConnectionError 

59 image_refs = image_ref_res.json() 

60 except requests.exceptions.ConnectionError: 

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

62 data = { 

63 'annotations': localization_processor.final_records, 

64 'title': localization_processor.section_name, 

65 'tab_title': deployments[0] if len(deployments) == 1 else f'{deployments[0]} - {deployments[-1].split("_")[-1]}', 

66 'concepts': session.get('vars_concepts', []), 

67 'reviewers': session.get('reviewers', []), 

68 'comments': comments, 

69 'image_refs': image_refs, 

70 } 

71 return render_template('image_review/image-review.html', data=data)