Coverage for coverage_report/views.py: 15%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-11 19:33 +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 if not (os.path.exists(file_path) and os.path.isfile(file_path)): 

37 raise Http404("File does not exist") 

38 

39 # Determine the content type based on the file extension 

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

41 if ext == '.html': 

42 content_type = 'text/html' 

43 elif ext in ['.css']: 

44 content_type = 'text/css' 

45 elif ext in ['.js']: 

46 content_type = 'application/javascript' 

47 else: 

48 raise Http404("Unsupported file type") 

49 

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

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

52