Coverage for app/routes/main_page.py: 50%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-02 02:49 +0000

1from flask import render_template, Blueprint, send_file 

2from app.models.response import error_response 

3from app.models.status_codes import StatusCodes 

4from app.services.log_service import get_all_data_from_db 

5import tempfile 

6import json 

7 

8main_page_bp = Blueprint('main_page', __name__) 

9 

10@main_page_bp.route('/') 

11def main_page_route(): 

12 """ 

13 Displays test AI input page 

14 """ 

15 try: 

16 return render_template('index.html') 

17 except Exception as e: 

18 return error_response( 

19 f"Error loading page {e}", 

20 StatusCodes.SERVER_ERROR 

21 ) 

22 

23@main_page_bp.route('/download') 

24def download_route(): 

25 try: 

26 return send_file('files/clover-latest.vsix', as_attachment=True) 

27 except Exception as e: 

28 return error_response( 

29 f"Error loading page {e}", 

30 StatusCodes.SERVER_ERROR 

31 ) 

32 

33 

34@main_page_bp.route('/export', methods=['GET']) 

35def get_all_data(): 

36 """ 

37 Retrieve all data from the database. 

38 """ 

39 try: 

40 data = get_all_data_from_db() 

41 json_data = json.dumps(data, indent=4) 

42 

43 with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as temp_file: 

44 temp_file.write(json_data) 

45 temp_file_path = temp_file.name 

46 return send_file( 

47 temp_file_path, 

48 as_attachment=True, 

49 download_name='data.json', 

50 mimetype='application/json' 

51 ) 

52 

53 except Exception as e: 

54 return error_response( 

55 f"Error fetching all data: {e}", 

56 None, 

57 StatusCodes.SERVER_ERROR 

58 )