diff --git a/feedler/feedparser.py b/feedler/feedparser.py index b4d4f7b..8d7f9e0 100644 --- a/feedler/feedparser.py +++ b/feedler/feedparser.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from datetime import datetime +from guess_language import guess_language def parse(obj, raw, stream): """ @@ -47,6 +48,9 @@ def parse(obj, raw, stream): else: obj.content = '' + # Detect language + obj.lang = guess_language(obj.content) or '' + # Collect tags tags = [] if 'tags' in obj.raw: diff --git a/feedler/migrations/0003_entry_lang.py b/feedler/migrations/0003_entry_lang.py new file mode 100644 index 0000000..e27ee89 --- /dev/null +++ b/feedler/migrations/0003_entry_lang.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.4 on 2017-09-04 20:33 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('feedler', '0002_feedpage'), + ] + + operations = [ + migrations.AddField( + model_name='entry', + name='lang', + field=models.CharField(blank=True, default='', max_length=2), + ), + ] diff --git a/feedler/models/admin.py b/feedler/models/admin.py index 604b92f..c6b6587 100644 --- a/feedler/models/admin.py +++ b/feedler/models/admin.py @@ -61,5 +61,7 @@ def handle_save_settings(sender, instance, *args, **kwargs): except Entry.DoesNotExist: logger.info("Adding entry '%s'" % eid) entry = Entry() + # Parse the Feedly object entry = feedparser.parse(entry, raw_entry, stream) + # Persist resulting object entry.save() diff --git a/feedler/models/models.py b/feedler/models/models.py index 8777e58..83e9240 100644 --- a/feedler/models/models.py +++ b/feedler/models/models.py @@ -6,6 +6,8 @@ from wagtail.wagtailcore.models import Page, Orderable from wagtail.wagtailadmin.edit_handlers import FieldPanel from wagtail.wagtailcore.fields import RichTextField +from django.utils import translation + class Stream(models.Model): title = models.CharField(max_length=255) ident = models.CharField(max_length=255) @@ -13,6 +15,14 @@ class Stream(models.Model): def __str__(self): return self.title +LANGUAGE_CHOICES = ( + ('de', 'Deutsch'), + ('fr', 'Français'), + ('it', 'Italiano'), + ('en', 'English'), + ('', ' * * * '), +) + class Entry(models.Model): """Implementation of the Entry from the feedly API as generic Django model """ @@ -25,7 +35,7 @@ class Entry(models.Model): author = models.CharField(max_length=255, blank=True) link = models.URLField() visual = models.URLField(blank=True) - + lang = models.CharField(max_length=2, blank=True, default='', choices=LANGUAGE_CHOICES) content = models.TextField() tags = models.TextField(blank=True) @@ -53,6 +63,12 @@ class FeedPage(Page): entries = Entry.objects.filter(stream=self.stream) else: entries = Entry.objects.all() + # 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') # Order by most recent date first entries = entries.order_by('-published') return entries[:10] diff --git a/publichealth/home/migrations/0020_auto_20170904_2233.py b/publichealth/home/migrations/0020_auto_20170904_2233.py new file mode 100644 index 0000000..a96fbe9 --- /dev/null +++ b/publichealth/home/migrations/0020_auto_20170904_2233.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.4 on 2017-09-04 20:33 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('home', '0019_auto_20170703_1244'), + ] + + operations = [ + migrations.AlterField( + model_name='contactformfield', + name='field_type', + field=models.CharField(choices=[('singleline', 'Single line text'), ('multiline', 'Multi-line text'), ('email', 'Email'), ('number', 'Number'), ('url', 'URL'), ('checkbox', 'Checkbox'), ('checkboxes', 'Checkboxes'), ('dropdown', 'Drop down'), ('multiselect', 'Multiple select'), ('radio', 'Radio buttons'), ('date', 'Date'), ('datetime', 'Date/time')], max_length=16, verbose_name='field type'), + ), + ] diff --git a/publichealth/home/models/models.py b/publichealth/home/models/models.py index f1b569d..5354862 100644 --- a/publichealth/home/models/models.py +++ b/publichealth/home/models/models.py @@ -227,15 +227,26 @@ class HomePage(Page): posts = EntryPage.objects.live().descendant_of(parent[0]) # Order by most recent date first posts = posts.order_by('-date') - # Get news entries + return posts[:3] + + @property + def newsentries(self): + # Get the last few news entries entries = Entry.objects.all().order_by('-published') - return list(chain(entries[:3], posts[:3])) + # Filter out by current language + curlang = translation.get_language() + if curlang in ['de']: + entries = entries.exclude(lang='fr') + elif curlang in ['fr']: + entries = entries.exclude(lang='de') + return entries[:6] def get_context(self, request): # Update template context context = super(HomePage, self).get_context(request) context['featured'] = self.featured context['blogentries'] = self.blogentries + context['newsentries'] = self.newsentries return context parent_page_types = ['wagtailcore.Page'] diff --git a/publichealth/home/templates/news.html b/publichealth/home/templates/news.html index edf9738..e31f305 100644 --- a/publichealth/home/templates/news.html +++ b/publichealth/home/templates/news.html @@ -5,7 +5,6 @@