Debian Codegraph @

demo/demoproject/path/views.py

raw
1 import os
2
3 from django_downloadview import PathDownloadView
4
5 # Let's initialize some fixtures.
6 app_dir = os.path.dirname(os.path.abspath(__file__))
7 project_dir = os.path.dirname(app_dir)
8 fixtures_dir = os.path.join(project_dir, "fixtures")
9 #: Path to a text file that says 'Hello world!'.
10 hello_world_path = os.path.join(fixtures_dir, "hello-world.txt")
11
12 #: Serve ``fixtures/hello-world.txt`` file.
13 static_path = PathDownloadView.as_view(path=hello_world_path)
14
15
16 class DynamicPathDownloadView(PathDownloadView):
17 """Serve file in ``settings.MEDIA_ROOT``.
18
19 .. warning::
20
21 Make sure to prevent "../" in path via URL patterns.
22
23 .. note::
24
25 This particular setup would be easier to perform with
26 :class:`StorageDownloadView`
27
28 """
29
30 def get_path(self):
31 """Return path inside fixtures directory."""
32 # Get path from URL resolvers or as_view kwarg.
33 relative_path = super().get_path()
34 # Make it absolute.
35 absolute_path = os.path.join(fixtures_dir, relative_path)
36 return absolute_path
37
38
39 dynamic_path = DynamicPathDownloadView.as_view()