Coverage for coverage_report/views.py: 14%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-12 19:27 +0000

1import os 

2from django.conf import settings 

3from django.http import HttpResponse, Http404 

4 

5# Create your views here. 

6 

7def serve_coverage(request, path): 

8 base_dir = os.path.join(settings.BASE_DIR, 'htmlcov') 

9 file_path = os.path.join(base_dir, path) 

10 

11 if not (os.path.exists(file_path) and os.path.isfile(file_path)): 

12 raise Http404("File does not exist") 

13 

14 # Determine the content type based on the file extension 

15 _, ext = os.path.splitext(path) 

16 if ext == '.html': 

17 content_type = 'text/html' 

18 elif ext in ['.css']: 

19 content_type = 'text/css' 

20 elif ext in ['.js']: 

21 content_type = 'application/javascript' 

22 else: 

23 raise Http404("Unsupported file type") 

24 

25 with open(file_path, 'rb') as f: 

26 return HttpResponse(f.read(), content_type=content_type) 

27 

28 

29def serve_sphinx_docs(request, path): 

30 # Define the base directory for Sphinx documentation 

31 sphinx_base_dir = os.path.join(settings.BASE_DIR, 'docs', 'build', 'html') 

32 

33 # Construct the full file path 

34 file_path = os.path.join(sphinx_base_dir, path) 

35 

36 # Debugging: Print paths to check them 

37 print(f"Base Directory: {sphinx_base_dir}") 

38 print(f"File Path: {file_path}") 

39 

40 if not (os.path.exists(file_path) and os.path.isfile(file_path)): 

41 raise Http404("File does not exist") 

42 

43 # Determine the content type based on the file extension 

44 _, ext = os.path.splitext(path) 

45 if ext == '.html': 

46 content_type = 'text/html' 

47 elif ext in ['.css']: 

48 content_type = 'text/css' 

49 elif ext in ['.js']: 

50 content_type = 'application/javascript' 

51 else: 

52 raise Http404("Unsupported file type") 

53 

54 with open(file_path, 'rb') as f: 

55 return HttpResponse(f.read(), content_type=content_type) 

56