##// END OF EJS Templates
Removed unneeded conditions in post template
Removed unneeded conditions in post template

File last commit:

r1673:8d6575ac default
r1675:0285c92a default
Show More
viewers.py
149 lines | 4.1 KiB | text/x-python | PythonLexer
neko259
Store images as regular attachments instead of separate model
r1590 from django.core.files.images import get_image_dimensions
neko259
Added support for different attachment types
r1273 from django.template.defaultfilters import filesizeformat
neko259
Updated file image
r1290 from django.contrib.staticfiles.templatetags.staticfiles import static
neko259
Added support for different attachment types
r1273
neko259
Support audio viewer
r1279 FILE_STUB_IMAGE = 'images/file.png'
neko259
Load URL if the file could not be loaded
r1660 FILE_STUB_URL = 'images/url.png'
neko259
Support audio viewer
r1279
neko259
Video formats are not limited to webm
r1307 FILE_TYPES_VIDEO = (
'webm',
'mp4',
neko259
Treat .mpeg files as video
r1342 'mpeg',
neko259
View ogv format as video
r1493 'ogv',
neko259
Video formats are not limited to webm
r1307 )
neko259
Added basic SVG support
r1284 FILE_TYPE_SVG = 'svg'
neko259
Support audio viewer
r1279 FILE_TYPES_AUDIO = (
'ogg',
'mp3',
neko259
Opus is an audio format too
r1339 'opus',
neko259
Support audio viewer
r1279 )
neko259
Store images as regular attachments instead of separate model
r1590 FILE_TYPES_IMAGE = (
'jpeg',
'jpg',
'png',
'bmp',
'gif',
)
neko259
Support audio viewer
r1279
neko259
Added images for different file formats
r1326 PLAIN_FILE_FORMATS = {
'pdf': 'pdf',
'djvu': 'djvu',
'txt': 'txt',
neko259
New file images
r1665 'tex': 'tex',
'xcf': 'xcf',
neko259
Added images for different file formats
r1326 }
neko259
Store images as regular attachments instead of separate model
r1590 CSS_CLASS_IMAGE = 'image'
CSS_CLASS_THUMB = 'thumb'
neko259
Added support for different attachment types
r1273
neko259
Autodetect attachment viewers by getting all abstract viewer subclasses
r1286 def get_viewers():
return AbstractViewer.__subclasses__()
neko259
Added support for different attachment types
r1273 class AbstractViewer:
neko259
Load URL if the file could not be loaded
r1660 def __init__(self, file, file_type, hash, url):
neko259
Added support for different attachment types
r1273 self.file = file
self.file_type = file_type
neko259
Fixed popup image preview
r1596 self.hash = hash
neko259
Load URL if the file could not be loaded
r1660 self.url = url
neko259
Added support for different attachment types
r1273
@staticmethod
def supports(file_type):
neko259
Support audio viewer
r1279 return True
neko259
Added support for different attachment types
r1273
def get_view(self):
neko259
Show file metadata for all attachments
r1300 return '<div class="image">'\
'{}'\
neko259
Removed obsolete code. Added download link to files
r1308 '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\
'</div>'.format(self.get_format_view(), self.file.url,
neko259
Show file metadata for all attachments
r1300 self.file_type, filesizeformat(self.file.size))
def get_format_view(self):
neko259
Added images for different file formats
r1326 if self.file_type in PLAIN_FILE_FORMATS:
image = 'images/fileformats/{}.png'.format(
PLAIN_FILE_FORMATS[self.file_type])
else:
image = FILE_STUB_IMAGE
neko259
Show file metadata for all attachments
r1300 return '<a href="{}">'\
neko259
Non-image file icons are inverted on hover
r1673 '<img class="url-image" src="{}" width="200" height="150"/>'\
neko259
Added images for different file formats
r1326 '</a>'.format(self.file.url, static(image))
neko259
Added support for different attachment types
r1273
neko259
Video formats are not limited to webm
r1307 class VideoViewer(AbstractViewer):
neko259
Added support for different attachment types
r1273 @staticmethod
def supports(file_type):
neko259
Video formats are not limited to webm
r1307 return file_type in FILE_TYPES_VIDEO
neko259
Added support for different attachment types
r1273
neko259
Show file metadata for all attachments
r1300 def get_format_view(self):
return '<video width="200" height="150" controls src="{}"></video>'\
.format(self.file.url)
neko259
Added support for different attachment types
r1273
neko259
Support audio viewer
r1279
class AudioViewer(AbstractViewer):
@staticmethod
def supports(file_type):
return file_type in FILE_TYPES_AUDIO
neko259
Show file metadata for all attachments
r1300 def get_format_view(self):
return '<audio controls src="{}"></audio>'.format(self.file.url)
neko259
Added basic SVG support
r1284
class SvgViewer(AbstractViewer):
@staticmethod
def supports(file_type):
return file_type == FILE_TYPE_SVG
neko259
Show file metadata for all attachments
r1300 def get_format_view(self):
return '<a class="thumb" href="{}">'\
'<img class="post-image-preview" width="200" height="150" src="{}" />'\
'</a>'.format(self.file.url, self.file.url)
neko259
Store images as regular attachments instead of separate model
r1590
class ImageViewer(AbstractViewer):
@staticmethod
def supports(file_type):
return file_type in FILE_TYPES_IMAGE
def get_format_view(self):
metadata = '{}, {}'.format(self.file.name.split('.')[-1],
filesizeformat(self.file.size))
width, height = get_image_dimensions(self.file.file)
preview_path = self.file.path.replace('.', '.200x150.')
pre_width, pre_height = get_image_dimensions(preview_path)
split = self.file.url.rsplit('.', 1)
w, h = 200, 150
thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1])
return '<a class="{}" href="{full}">' \
'<img class="post-image-preview"' \
' src="{}"' \
neko259
Fixed popup image preview
r1596 ' alt="{}"' \
neko259
Store images as regular attachments instead of separate model
r1590 ' width="{}"' \
' height="{}"' \
' data-width="{}"' \
' data-height="{}" />' \
'</a>' \
.format(CSS_CLASS_THUMB,
thumb_url,
neko259
Fixed popup image preview
r1596 self.hash,
neko259
Store images as regular attachments instead of separate model
r1590 str(pre_width),
str(pre_height), str(width), str(height),
full=self.file.url, image_meta=metadata)
neko259
Load URL if the file could not be loaded
r1660
class UrlViewer(AbstractViewer):
@staticmethod
def supports(file_type):
return file_type is None
def get_view(self):
return '<div class="image">' \
'{}' \
'</div>'.format(self.get_format_view())
def get_format_view(self):
return '<a href="{}">' \
neko259
Non-image file icons are inverted on hover
r1673 '<img class="url-image" src="{}" width="200" height="150"/>' \
neko259
Load URL if the file could not be loaded
r1660 '</a>'.format(self.url, static(FILE_STUB_URL))