Multilingual entries
This commit is contained in:
parent
23705a072e
commit
9dbee27a16
7 changed files with 56 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from guess_language import guess_language
|
||||||
|
|
||||||
def parse(obj, raw, stream):
|
def parse(obj, raw, stream):
|
||||||
"""
|
"""
|
||||||
|
@ -47,6 +48,9 @@ def parse(obj, raw, stream):
|
||||||
else:
|
else:
|
||||||
obj.content = ''
|
obj.content = ''
|
||||||
|
|
||||||
|
# Detect language
|
||||||
|
obj.lang = guess_language(obj.content) or ''
|
||||||
|
|
||||||
# Collect tags
|
# Collect tags
|
||||||
tags = []
|
tags = []
|
||||||
if 'tags' in obj.raw:
|
if 'tags' in obj.raw:
|
||||||
|
|
20
feedler/migrations/0003_entry_lang.py
Normal file
20
feedler/migrations/0003_entry_lang.py
Normal file
|
@ -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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -61,5 +61,7 @@ def handle_save_settings(sender, instance, *args, **kwargs):
|
||||||
except Entry.DoesNotExist:
|
except Entry.DoesNotExist:
|
||||||
logger.info("Adding entry '%s'" % eid)
|
logger.info("Adding entry '%s'" % eid)
|
||||||
entry = Entry()
|
entry = Entry()
|
||||||
|
# Parse the Feedly object
|
||||||
entry = feedparser.parse(entry, raw_entry, stream)
|
entry = feedparser.parse(entry, raw_entry, stream)
|
||||||
|
# Persist resulting object
|
||||||
entry.save()
|
entry.save()
|
||||||
|
|
|
@ -6,6 +6,8 @@ from wagtail.wagtailcore.models import Page, Orderable
|
||||||
from wagtail.wagtailadmin.edit_handlers import FieldPanel
|
from wagtail.wagtailadmin.edit_handlers import FieldPanel
|
||||||
from wagtail.wagtailcore.fields import RichTextField
|
from wagtail.wagtailcore.fields import RichTextField
|
||||||
|
|
||||||
|
from django.utils import translation
|
||||||
|
|
||||||
class Stream(models.Model):
|
class Stream(models.Model):
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
ident = models.CharField(max_length=255)
|
ident = models.CharField(max_length=255)
|
||||||
|
@ -13,6 +15,14 @@ class Stream(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
LANGUAGE_CHOICES = (
|
||||||
|
('de', 'Deutsch'),
|
||||||
|
('fr', 'Français'),
|
||||||
|
('it', 'Italiano'),
|
||||||
|
('en', 'English'),
|
||||||
|
('', ' * * * '),
|
||||||
|
)
|
||||||
|
|
||||||
class Entry(models.Model):
|
class Entry(models.Model):
|
||||||
"""Implementation of the Entry from the feedly API as generic Django 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)
|
author = models.CharField(max_length=255, blank=True)
|
||||||
link = models.URLField()
|
link = models.URLField()
|
||||||
visual = models.URLField(blank=True)
|
visual = models.URLField(blank=True)
|
||||||
|
lang = models.CharField(max_length=2, blank=True, default='', choices=LANGUAGE_CHOICES)
|
||||||
content = models.TextField()
|
content = models.TextField()
|
||||||
tags = models.TextField(blank=True)
|
tags = models.TextField(blank=True)
|
||||||
|
|
||||||
|
@ -53,6 +63,12 @@ class FeedPage(Page):
|
||||||
entries = Entry.objects.filter(stream=self.stream)
|
entries = Entry.objects.filter(stream=self.stream)
|
||||||
else:
|
else:
|
||||||
entries = Entry.objects.all()
|
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
|
# Order by most recent date first
|
||||||
entries = entries.order_by('-published')
|
entries = entries.order_by('-published')
|
||||||
return entries[:10]
|
return entries[:10]
|
||||||
|
|
|
@ -231,8 +231,14 @@ class HomePage(Page):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def newsentries(self):
|
def newsentries(self):
|
||||||
# Get news entries
|
# Get the last few news entries
|
||||||
entries = Entry.objects.all().order_by('-published')
|
entries = Entry.objects.all().order_by('-published')
|
||||||
|
# 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]
|
return entries[:6]
|
||||||
|
|
||||||
def get_context(self, request):
|
def get_context(self, request):
|
||||||
|
|
|
@ -132,6 +132,10 @@ PASSWORD_REQUIRED_TEMPLATE = 'password.html'
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/1.8/topics/i18n/
|
# https://docs.djangoproject.com/en/1.8/topics/i18n/
|
||||||
|
|
||||||
|
LANGUAGES = (
|
||||||
|
('de', u'Deutsch'),
|
||||||
|
('fr', u'Français'),
|
||||||
|
)
|
||||||
LANGUAGE_CODE = 'de' # default language
|
LANGUAGE_CODE = 'de' # default language
|
||||||
|
|
||||||
TIME_ZONE = 'Europe/Zurich'
|
TIME_ZONE = 'Europe/Zurich'
|
||||||
|
|
|
@ -8,8 +8,9 @@ Django==1.11.4
|
||||||
psycopg2==2.7.3.1
|
psycopg2==2.7.3.1
|
||||||
dj-database-url==0.4.2
|
dj-database-url==0.4.2
|
||||||
|
|
||||||
# Addons
|
# Content
|
||||||
puput==0.9
|
puput==0.9
|
||||||
|
guess-language==0.2
|
||||||
|
|
||||||
# Search
|
# Search
|
||||||
elasticsearch>=2.0.0,<3.0.0
|
elasticsearch>=2.0.0,<3.0.0
|
||||||
|
|
Loading…
Reference in a new issue