Initial page model with translation support

This commit is contained in:
Oleg Lavrovsky 2016-12-09 06:54:14 +01:00
parent 90d3dc568d
commit cccdf0aa31
9 changed files with 81 additions and 17 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
*.pyc
.DS_Store
*.swp
*.sqlite3
/env/
/venv/
/static/

View file

@ -3,7 +3,43 @@ from __future__ import unicode_literals
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore import blocks
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.wagtailimages.blocks import ImageChooserBlock
from wagtail.wagtailsearch import index
from .util import TranslatedField
class HomePage(Page):
pass
title_fr = models.CharField(max_length=255, default="")
body_de = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
], null=True, blank=True)
body_fr = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
], null=True, blank=True)
translated_title = TranslatedField(
'title',
'title_fr',
)
body = TranslatedField(
'body_de',
'body_fr',
)
search_fields = Page.search_fields + [
index.SearchField('body_de'),
index.SearchField('body_fr'),
]
content_panels = Page.content_panels + [
StreamFieldPanel('title_fr'),
StreamFieldPanel('body_de'),
StreamFieldPanel('body_fr'),
]

12
publichealth/home/util.py Normal file
View file

@ -0,0 +1,12 @@
from django.utils import translation
class TranslatedField(object):
def __init__(self, de_field, fr_field):
self.de_field = de_field
self.fr_field = fr_field
def __get__(self, instance, owner):
if translation.get_language() == 'fr':
return getattr(instance, self.fr_field)
else:
return getattr(instance, self.de_field)

View file

@ -61,6 +61,8 @@ MIDDLEWARE_CLASSES = [
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware',
]
@ -121,9 +123,9 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-gb'
LANGUAGE_CODE = 'de-DE'
TIME_ZONE = 'Europe/London'
TIME_ZONE = 'Europe/Zurich'
USE_I18N = True

View file

@ -14,6 +14,12 @@ BASE_URL = 'http://localhost:8000'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'env/publichealth-dev.sqlite3',
}
}
try:
from .local import *

View file

@ -10,7 +10,6 @@ from .base import *
DEBUG = False
TEMPLATES[0]['OPTIONS']['debug'] = False
# Compress static files offline and minify CSS
# http://django-compressor.readthedocs.org/en/latest/settings/#django.conf.settings.COMPRESS_OFFLINE
COMPRESS_OFFLINE = True

View file

@ -1,6 +1,7 @@
from django.conf.urls import include, url
from django.conf import settings
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
@ -36,6 +37,8 @@ if settings.DEBUG:
]
urlpatterns += [
urlpatterns += i18n_patterns(
# These URLs will have /<language_code>/ appended to the beginning
url(r'', include(wagtail_urls)),
]
)

View file

@ -4,5 +4,10 @@ Public Health Schweiz
sudo apt-get install python3-venv python3-dev
pyvenv env
. env/bin/activate
pip install -U pip
pip install -r requirements.txt
./manage.py migrate
./manage.py createsuperuser
./manage.py runserver

View file

@ -1,17 +1,17 @@
Django==1.9.9
psycopg2==2.6.1
elasticsearch==1.2.0
django-redis==4.3.0
wagtail==1.5.3
django-libsass==0.6
libsass==0.8.3
Pillow==2.9.0
Django==1.10.4
psycopg2==2.6.2
elasticsearch==5.0.1
django-redis==4.6.0
wagtail==1.7
django-libsass==0.7
libsass==0.11.2
Pillow==3.4.2
# Development tools
stellar==0.4.3
# Production dependencies
dj-database-url==0.3.0
whitenoise==2.0.4
uwsgi==2.0.11.2
dj-database-url==0.4.1
whitenoise==3.2.2
uwsgi==2.0.14
ConcurrentLogHandler==0.9.1