Show More
@@ -1,68 +1,67 b'' | |||||
1 | from django.template.defaultfilters import filesizeformat |
|
1 | from django.template.defaultfilters import filesizeformat | |
2 | from django.contrib.staticfiles.templatetags.staticfiles import static |
|
2 | from django.contrib.staticfiles.templatetags.staticfiles import static | |
3 |
|
3 | |||
4 | FILE_STUB_IMAGE = 'images/file.png' |
|
4 | FILE_STUB_IMAGE = 'images/file.png' | |
5 |
|
5 | |||
6 | FILE_TYPE_WEBM = 'webm' |
|
6 | FILE_TYPE_WEBM = 'webm' | |
7 | FILE_TYPE_SVG = 'svg' |
|
7 | FILE_TYPE_SVG = 'svg' | |
8 | FILE_TYPES_AUDIO = ( |
|
8 | FILE_TYPES_AUDIO = ( | |
9 | 'ogg', |
|
9 | 'ogg', | |
10 | 'mp3', |
|
10 | 'mp3', | |
11 | ) |
|
11 | ) | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | def get_viewers(): |
|
14 | def get_viewers(): | |
15 | return AbstractViewer.__subclasses__() |
|
15 | return AbstractViewer.__subclasses__() | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | class AbstractViewer: |
|
18 | class AbstractViewer: | |
19 | def __init__(self, file, file_type): |
|
19 | def __init__(self, file, file_type): | |
20 | self.file = file |
|
20 | self.file = file | |
21 | self.file_type = file_type |
|
21 | self.file_type = file_type | |
22 |
|
22 | |||
23 | @staticmethod |
|
23 | @staticmethod | |
24 | def supports(file_type): |
|
24 | def supports(file_type): | |
25 | return True |
|
25 | return True | |
26 |
|
26 | |||
27 | def get_view(self): |
|
27 | def get_view(self): | |
28 |
return '<div class="image" |
|
28 | return '<div class="image">'\ | |
|
29 | '{}'\ | |||
|
30 | '<div class="image-metadata">{}, {}</div>'\ | |||
|
31 | '</div>'.format(self.get_format_view(), | |||
|
32 | self.file_type, filesizeformat(self.file.size)) | |||
|
33 | ||||
|
34 | def get_format_view(self): | |||
|
35 | return '<a href="{}">'\ | |||
29 | '<img src="{}" width="200" height="150"/>'\ |
|
36 | '<img src="{}" width="200" height="150"/>'\ | |
30 | '</a>'\ |
|
37 | '</a>'.format(self.file.url,static(FILE_STUB_IMAGE)) | |
31 | '<div class="image-metadata">{}, {}</div>'\ |
|
|||
32 | '</div>'.format(self.file.url, static(FILE_STUB_IMAGE), |
|
|||
33 | self.file_type, filesizeformat(self.file.size)) |
|
|||
34 |
|
38 | |||
35 |
|
39 | |||
36 | class WebmViewer(AbstractViewer): |
|
40 | class WebmViewer(AbstractViewer): | |
37 | @staticmethod |
|
41 | @staticmethod | |
38 | def supports(file_type): |
|
42 | def supports(file_type): | |
39 | return file_type == FILE_TYPE_WEBM |
|
43 | return file_type == FILE_TYPE_WEBM | |
40 |
|
44 | |||
41 | def get_view(self): |
|
45 | def get_format_view(self): | |
42 | return '<div class="image">'\ |
|
46 | return '<video width="200" height="150" controls src="{}"></video>'\ | |
43 | '<video width="200" height="150" controls src="{}"/>'\ |
|
47 | .format(self.file.url) | |
44 | '</div>'.format(self.file.url) |
|
|||
45 |
|
48 | |||
46 |
|
49 | |||
47 | class AudioViewer(AbstractViewer): |
|
50 | class AudioViewer(AbstractViewer): | |
48 | @staticmethod |
|
51 | @staticmethod | |
49 | def supports(file_type): |
|
52 | def supports(file_type): | |
50 | return file_type in FILE_TYPES_AUDIO |
|
53 | return file_type in FILE_TYPES_AUDIO | |
51 |
|
54 | |||
52 | def get_view(self): |
|
55 | def get_format_view(self): | |
53 | return '<div class="image">' \ |
|
56 | return '<audio controls src="{}"></audio>'.format(self.file.url) | |
54 | '<audio controls src="{}"/>' \ |
|
|||
55 | '</div>'.format(self.file.url) |
|
|||
56 |
|
57 | |||
57 |
|
58 | |||
58 | class SvgViewer(AbstractViewer): |
|
59 | class SvgViewer(AbstractViewer): | |
59 | @staticmethod |
|
60 | @staticmethod | |
60 | def supports(file_type): |
|
61 | def supports(file_type): | |
61 | return file_type == FILE_TYPE_SVG |
|
62 | return file_type == FILE_TYPE_SVG | |
62 |
|
63 | |||
63 | def get_view(self): |
|
64 | def get_format_view(self): | |
64 |
return '< |
|
65 | return '<a class="thumb" href="{}">'\ | |
65 | '<a class="thumb" href="{}">'\ |
|
66 | '<img class="post-image-preview" width="200" height="150" src="{}" />'\ | |
66 | '<img class="post-image-preview" width="200" height="150" src="{}"/>'\ |
|
67 | '</a>'.format(self.file.url, self.file.url) | |
67 | '</a>'\ |
|
|||
68 | '</div>'.format(self.file.url, self.file.url) |
|
General Comments 0
You need to be logged in to leave comments.
Login now