event-timeline/app/event/models.py

81 lines
2.9 KiB
Python
Raw Normal View History

2022-04-12 13:09:48 +00:00
from datetime import datetime
2021-11-12 16:49:35 +00:00
from django.db import models
2021-11-12 17:17:59 +00:00
from django.conf import settings
2021-11-12 16:49:35 +00:00
import ox
class Event(models.Model):
class Meta:
2021-11-19 14:26:58 +00:00
ordering = ('position', 'date')
2021-11-12 16:49:35 +00:00
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
slug = models.SlugField(blank=True, unique=True)
position = models.IntegerField(default=0)
title = models.TextField(blank=True)
type = models.CharField(blank=True, default='', max_length=1024)
date = models.CharField(blank=True, null=True, max_length=1024)
body = models.TextField(blank=True, null=True)
2021-11-12 16:58:31 +00:00
media = models.TextField(blank=True, null=True)
2021-11-19 14:22:59 +00:00
media_caption = models.TextField(blank=True, null=True, default='')
2021-11-12 16:49:35 +00:00
data = models.JSONField(default=dict, blank=True, editable=False)
def __str__(self):
return '%s (%s)' % (ox.strip_tags(self.title), self.slug)
def get_absolute_url(self):
2021-11-12 17:17:59 +00:00
return '/' + settings.URL_PREFIX + '#' + self.slug
2021-11-19 14:31:23 +00:00
2022-04-12 13:12:25 +00:00
@property
2022-04-12 13:06:47 +00:00
def display_date(self):
2022-04-12 13:26:58 +00:00
if self.date:
date = self.date.split(' ')
start = date[0].split('-')
if len(date) > 1:
end = date[1].split('-')
else:
end = start
end_only_month = False
start_only_month = False
while len(end) < 3:
end.append(1)
end_only_month = True
while len(start) < 3:
start.append(1)
start_only_month = True
start = [int(p) for p in start]
end = [int(p) for p in end]
2022-04-12 13:06:47 +00:00
2022-04-12 13:26:58 +00:00
display_date = datetime(*start).strftime('%e %B, %Y')
if start_only_month:
display_date = ' '.join(display_date.split(' ')[1:])
if end != start:
end_date = datetime(*end).strftime('%e %B, %Y')
if end_only_month:
end_date = ' '.join(end_date.split(' ')[1:])
display_date += '' + end_date
else:
display_date = ''
2022-04-12 13:06:47 +00:00
return display_date
2021-11-19 14:31:23 +00:00
def media_html(self):
html = ''
if self.media and self.media.split('.')[-1] in ('jpg', 'png', 'gif'):
html += '<img src="%s">' % self.media
2021-11-19 14:32:23 +00:00
elif '<' in self.media:
html = self.media
2021-11-19 14:31:23 +00:00
else:
2021-11-19 14:35:56 +00:00
url = self.media
if 'youtube.com/watch' in url:
2021-11-19 14:41:08 +00:00
url = url.replace('/watch?v=', '/embed/')
2022-03-18 14:48:39 +00:00
elif 'twitter.com/' in url:
2022-03-18 14:48:59 +00:00
url = url.replace('twitter.com', 'nitter.net')
2021-11-19 14:35:56 +00:00
html += '<iframe src="%s" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>' % url
2021-11-19 14:31:23 +00:00
if self.media_caption:
html = '<figure>%s<figcaption>%s</figcaption></figure>' % (html, self.media_caption)
return html