Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import mimetypes 

2import os 

3from typing import Any, List 

4from django.http import HttpResponse, FileResponse, Http404 

5from django.utils.translation import gettext as _ 

6from jutil.format import format_xml_file, format_xml_bytes, format_csv 

7 

8 

9class FileSystemFileResponse(FileResponse): 

10 """ 

11 File system download HTTP response. 

12 :param full_path: Full path to file 

13 :param filename: Filename (optional) passed to client. Defaults to basename of the full path. 

14 """ 

15 def __init__(self, full_path: str, filename: str = '', **kw): 

16 if not os.path.isfile(full_path): 16 ↛ 17line 16 didn't jump to line 17, because the condition on line 16 was never true

17 raise Http404(_("File {} not found").format(full_path)) 

18 if not filename: 18 ↛ 20line 18 didn't jump to line 20, because the condition on line 18 was never false

19 filename = os.path.basename(full_path) 

20 content_type = mimetypes.guess_type(filename)[0] 

21 super().__init__(open(full_path, 'rb'), **kw) 

22 if content_type: 22 ↛ 24line 22 didn't jump to line 24, because the condition on line 22 was never false

23 self['Content-Type'] = content_type 

24 self['Content-Length'] = os.path.getsize(full_path) 

25 self['Content-Disposition'] = "attachment; filename={}".format(filename) 

26 

27 

28class CsvResponse(HttpResponse): 

29 """ 

30 CSV download HTTP response. 

31 """ 

32 def __init__(self, rows: List[List[Any]], filename: str, dialect='excel', **kw): 

33 """ 

34 Returns CSV response. 

35 :param rows: List of column lists 

36 :param filename: Download response file name 

37 :param dialect: See csv.writer dialect 

38 :param kw: Parameters to be passed to HttpResponse __init__ 

39 """ 

40 buf = format_csv(rows, dialect=dialect).encode('utf-8') 

41 super().__init__(content=buf, content_type='text/csv', **kw) 

42 self['Content-Disposition'] = 'attachment;filename="{}"'.format(filename) 

43 

44 

45class FormattedXmlFileResponse(HttpResponse): 

46 def __init__(self, filename: str): 

47 content = format_xml_file(filename) 

48 super().__init__(content) 

49 self['Content-Type'] = 'application/xml' 

50 self['Content-Length'] = len(content) 

51 self['Content-Disposition'] = "attachment; filename={}".format(os.path.basename(filename)) 

52 

53 

54class XmlResponse(HttpResponse): 

55 def __init__(self, content: bytes, filename: str): 

56 super().__init__(content) 

57 self['Content-Type'] = 'application/xml' 

58 self['Content-Length'] = len(content) 

59 self['Content-Disposition'] = "attachment; filename={}".format(os.path.basename(filename)) 

60 

61 

62class FormattedXmlResponse(XmlResponse): 

63 def __init__(self, content: bytes, filename: str, encoding: str = 'UTF-8', exceptions: bool = True): 

64 super().__init__(format_xml_bytes(content, encoding=encoding, exceptions=exceptions), filename)