From e5fcd61105b6851b4833ab9a13210aa1fdf00140 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 13 Jan 2018 15:45:54 +0100 Subject: [PATCH] Add the static pages module --- khaganat/settings.py | 13 +++++++++++- khaganat/urls.py | 9 +++++++- pages/__init__.py | 0 pages/admin.py | 11 ++++++++++ pages/apps.py | 5 +++++ pages/migrations/0001_initial.py | 35 ++++++++++++++++++++++++++++++++ pages/migrations/__init__.py | 0 pages/models.py | 21 +++++++++++++++++++ pages/templates/pages/page.html | 8 ++++++++ pages/tests.py | 3 +++ pages/urls.py | 8 ++++++++ pages/views.py | 18 ++++++++++++++++ 12 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 pages/__init__.py create mode 100644 pages/admin.py create mode 100644 pages/apps.py create mode 100644 pages/migrations/0001_initial.py create mode 100644 pages/migrations/__init__.py create mode 100644 pages/models.py create mode 100644 pages/templates/pages/page.html create mode 100644 pages/tests.py create mode 100644 pages/urls.py create mode 100644 pages/views.py diff --git a/khaganat/settings.py b/khaganat/settings.py index 055c026..46f634d 100644 --- a/khaganat/settings.py +++ b/khaganat/settings.py @@ -10,6 +10,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.0/ref/settings/ """ +from django.utils.translation import gettext_lazy as _ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -37,11 +38,14 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'khaganat', + 'pages.apps.PagesConfig', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', @@ -103,7 +107,14 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/2.0/topics/i18n/ -LANGUAGE_CODE = os.getenv('KHAGANAT_LANGUAGE_CODE', default='fr-fr') +LANGUAGE_CODE = os.getenv('KHAGANAT_LANGUAGE_CODE', default='fr') + +# https://github.com/django/django/blob/master/django/conf/global_settings.py + +LANGUAGES = [ + ('en', _('English')), + ('fr', _('French')), +] TIME_ZONE = os.getenv('KHAGANAT_TIME_ZONE', default='Europe/Paris') diff --git a/khaganat/urls.py b/khaganat/urls.py index 4f5bb5f..c4fb89b 100644 --- a/khaganat/urls.py +++ b/khaganat/urls.py @@ -13,9 +13,16 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.views.generic.base import RedirectView +from django.conf.urls.i18n import i18n_patterns from django.contrib import admin -from django.urls import path +from django.urls import include,path urlpatterns = [ + path('', RedirectView.as_view(url='/page', permanent=False)), path('admin/', admin.site.urls), ] + +urlpatterns += i18n_patterns( + path('page/', include('pages.urls')), +) diff --git a/pages/__init__.py b/pages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pages/admin.py b/pages/admin.py new file mode 100644 index 0000000..e7b494a --- /dev/null +++ b/pages/admin.py @@ -0,0 +1,11 @@ +from django.contrib import admin +from .models import Page,PageContent + +class PageContentInline(admin.StackedInline): + model = PageContent + extra = 2 + +class PageAdmin(admin.ModelAdmin): + inlines = [PageContentInline] + +admin.site.register(Page, PageAdmin) diff --git a/pages/apps.py b/pages/apps.py new file mode 100644 index 0000000..acdb960 --- /dev/null +++ b/pages/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class PagesConfig(AppConfig): + name = 'pages' diff --git a/pages/migrations/0001_initial.py b/pages/migrations/0001_initial.py new file mode 100644 index 0000000..de0c51f --- /dev/null +++ b/pages/migrations/0001_initial.py @@ -0,0 +1,35 @@ +# Generated by Django 2.0.1 on 2018-01-13 14:07 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Page', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('slug', models.CharField(max_length=200, unique=True)), + ('default', models.BooleanField(default=False)), + ], + ), + migrations.CreateModel( + name='PageContent', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('language', models.CharField(choices=[('en', 'English'), ('fr', 'French')], max_length=10)), + ('created_on', models.DateTimeField(auto_now_add=True)), + ('edited_on', models.DateTimeField(auto_now=True)), + ('title', models.CharField(max_length=200)), + ('content', models.TextField()), + ('page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pages.Page')), + ], + ), + ] diff --git a/pages/migrations/__init__.py b/pages/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pages/models.py b/pages/models.py new file mode 100644 index 0000000..d4fbd4a --- /dev/null +++ b/pages/models.py @@ -0,0 +1,21 @@ +from django.conf import settings +from django.db import models + + +class Page(models.Model): + slug = models.CharField(max_length=200, unique=True) + default = models.BooleanField(default=False) + + def __str__(self): + return self.slug + +class PageContent(models.Model): + page = models.ForeignKey(Page, on_delete=models.CASCADE) + language = models.CharField(max_length=10, choices=settings.LANGUAGES) + created_on = models.DateTimeField(auto_now_add=True) + edited_on = models.DateTimeField(auto_now=True) + title = models.CharField(max_length=200) + content = models.TextField() + + def __str__(self): + return self.title diff --git a/pages/templates/pages/page.html b/pages/templates/pages/page.html new file mode 100644 index 0000000..514ae66 --- /dev/null +++ b/pages/templates/pages/page.html @@ -0,0 +1,8 @@ +{% extends "khaganat/base.html" %} + +{% block title %}{{ page.title }}{% endblock %} + +{% block content %} +

{{ page.title }}

+

{{ page.content }}

+{% endblock %} diff --git a/pages/tests.py b/pages/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/pages/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/pages/urls.py b/pages/urls.py new file mode 100644 index 0000000..84efb5e --- /dev/null +++ b/pages/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + + +urlpatterns = [ + path('/', views.PageView.as_view(), name='page'), + path('', views.index, name='index'), +] diff --git a/pages/views.py b/pages/views.py new file mode 100644 index 0000000..f6c17ef --- /dev/null +++ b/pages/views.py @@ -0,0 +1,18 @@ +from django.utils.translation import get_language +from django.http import HttpResponseRedirect +from django.views import generic +from django.urls import reverse +from .models import PageContent + + +def index(request): + default_page = Page.objects.filter(default=True).first() + return HttpResponseRedirect(reverse('page', args=(default_page.slug, ))) + +class PageView(generic.DetailView): + model = PageContent + context_object_name = 'page' + template_name = 'pages/page.html' + + def get_object(self): + return PageContent.objects.filter(language=get_language(), page__slug=self.kwargs['slug']).first()