event-timeline/app/media/models.py

27 lines
809 B
Python
Raw Normal View History

2022-04-22 17:23:21 +00:00
from django.conf import settings
2022-04-22 17:20:42 +00:00
from django.db import models
from django.urls import reverse
class Image(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
file = models.ImageField(upload_to='image', null=True, blank=True)
def get_absolute_url(self):
2022-04-22 17:23:21 +00:00
return '/' + settings.URL_PREFIX + self.file.url[1:]
2022-04-22 17:20:42 +00:00
get_absolute_url.short_description = 'URL'
def image_tag(self):
from django.utils.html import mark_safe
return mark_safe('<img src="%s" style="max-width:256px" />' % self.get_absolute_url())
image_tag.short_description = 'Preview'
def __str__(self):
if self.file:
name = self.file.name.split('/')[-1]
else:
name = 'None'
return name