public-health-ch/feedler/models/models.py

108 lines
3.4 KiB
Python
Raw Normal View History

2017-07-03 11:36:01 +00:00
# -*- coding: utf-8 -*-
2022-05-03 10:02:34 +00:00
import datetime
2017-07-03 11:36:01 +00:00
from django.db import models
2017-09-05 08:54:43 +00:00
from django.utils import translation
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
2022-05-03 10:02:34 +00:00
from django.utils.translation import gettext_lazy as _
2019-05-13 15:11:16 +00:00
from wagtail.core.models import Page, Orderable
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.fields import RichTextField
2017-07-03 15:21:31 +00:00
2017-07-03 14:01:44 +00:00
class Stream(models.Model):
title = models.CharField(max_length=255)
2017-10-13 21:45:55 +00:00
ident = models.CharField(max_length=255,
help_text='Example: enterprise/myuser/tag/abcd...')
2017-07-03 14:01:44 +00:00
def __str__(self):
return self.title
2017-07-03 11:36:01 +00:00
2017-09-04 21:03:01 +00:00
LANGUAGE_CHOICES = (
('de', 'Deutsch'),
('fr', 'Français'),
('it', 'Italiano'),
('en', 'English'),
('', ' * * * '),
)
2017-07-03 11:36:01 +00:00
class Entry(models.Model):
"""Implementation of the Entry from the feedly API as generic Django model
"""
2017-07-03 14:01:44 +00:00
raw = models.TextField(blank=True, editable=False)
updated = models.DateTimeField(auto_now=True, editable=False)
published = models.DateTimeField(auto_now_add=True, editable=False)
2017-10-12 13:01:47 +00:00
entry_id = models.CharField(max_length=255, blank=True, editable=False)
2017-07-03 11:36:01 +00:00
title = models.CharField(max_length=255)
2017-07-03 14:01:44 +00:00
author = models.CharField(max_length=255, blank=True)
2017-11-17 16:00:04 +00:00
link = models.URLField(max_length=500)
visual = models.URLField(max_length=500, blank=True)
2017-09-04 21:03:01 +00:00
lang = models.CharField(max_length=2, blank=True, default='', choices=LANGUAGE_CHOICES)
2017-07-03 11:36:01 +00:00
content = models.TextField()
tags = models.TextField(blank=True)
2022-05-03 10:02:34 +00:00
expire_at = models.DateTimeField(
verbose_name=_("expiry date/time"),
blank=True,
null=True
)
2017-07-03 11:36:01 +00:00
2017-07-03 14:01:44 +00:00
stream = models.ForeignKey(Stream,
2017-09-14 07:56:38 +00:00
blank=False, on_delete=models.CASCADE,
2017-07-03 14:01:44 +00:00
verbose_name='Original stream')
2017-07-03 11:36:01 +00:00
2017-09-14 07:56:38 +00:00
@property
2017-09-14 16:25:10 +00:00
def with_image(self):
if not self.visual: return u''
return u''
2017-09-14 07:56:38 +00:00
2017-07-03 11:36:01 +00:00
class Meta:
verbose_name_plural = 'Entries'
2017-11-17 16:00:04 +00:00
def __str__(self):
return self.title
2017-07-03 11:36:01 +00:00
2017-07-03 15:21:31 +00:00
class FeedPage(Page):
intro = RichTextField(default='', blank=True)
stream = models.ForeignKey(Stream, on_delete=models.PROTECT,
null=True, blank=True, verbose_name='Filter to stream (optional)')
content_panels = [
FieldPanel('title'),
FieldPanel('intro'),
FieldPanel('stream'),
]
@property
def feedentries(self):
if self.stream:
2022-05-03 10:02:34 +00:00
entries = Entry.objects.filter(stream=self.stream).filter(
models.Q(expire_at__isnull=True) | models.Q(expire_at__gt=datetime.datetime.now())
)
2017-07-03 15:21:31 +00:00
else:
entries = Entry.objects.all()
2017-09-04 21:03:01 +00:00
# Filter out by chosen language
curlang = translation.get_language()
if curlang in ['de']:
entries = entries.exclude(lang='fr')
elif curlang in ['fr']:
entries = entries.exclude(lang='de')
2017-07-03 15:21:31 +00:00
# Order by most recent date first
2017-09-05 08:57:33 +00:00
return entries.order_by('-published')[:72]
2017-07-03 15:21:31 +00:00
def get_context(self, request):
# Update template context
context = super(FeedPage, self).get_context(request)
2017-09-05 08:54:43 +00:00
# Wrap with pagination
paginator = Paginator(self.feedentries, 9)
page = request.GET.get('page')
try:
feedentries = paginator.page(page)
except (PageNotAnInteger, EmptyPage):
feedentries = paginator.page(1)
context['feedentries'] = feedentries
2017-07-03 15:21:31 +00:00
return context
class Meta:
verbose_name = "Feeds"