From f4ca9a8835c92c146203ffb9ee10da4b94823765 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 30 May 2023 23:11:27 +0200 Subject: [PATCH] language switcher works --- gawa/gawa/settings.py | 14 ++++++- gawa/gawa/urls.py | 6 +-- gawa/start/middleware.py | 20 ++++++++++ gawa/start/migrations/0001_initial.py | 25 +++++++++++++ gawa/start/models.py | 15 +++++++- gawa/start/templates/start/base.html | 48 +++++++++++++++++------- gawa/start/templates/start/index.html | 38 +++++++++++++++++-- gawa/start/templatetags/helper_tags.py | 30 +++++++++++++++ gawa/start/urls.py | 9 +++-- gawa/start/views.py | 52 ++++++++++++++++++++++---- 10 files changed, 223 insertions(+), 34 deletions(-) create mode 100644 gawa/start/middleware.py create mode 100644 gawa/start/migrations/0001_initial.py create mode 100644 gawa/start/templatetags/helper_tags.py diff --git a/gawa/gawa/settings.py b/gawa/gawa/settings.py index 2f61154..26f03a9 100644 --- a/gawa/gawa/settings.py +++ b/gawa/gawa/settings.py @@ -31,6 +31,10 @@ DEBUG = True ALLOWED_HOSTS = [] +# Allow inclusion of stuff from these origins +CORS_ALLOWED_ORIGINS = [ + "https://static.cscherr.de", +] # Application definition @@ -54,6 +58,7 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.locale.LocaleMiddleware', + 'start.middleware.LangBasedOnUrlMiddleware', ] ROOT_URLCONF = 'gawa.urls' @@ -115,7 +120,14 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/3.2/topics/i18n/ -LANGUAGE_CODE = 'de-De' +from django.utils.translation import gettext_lazy as _ + +LANGUAGES = [ + ("de", _("German")), + ("en", _("English")), +] + +LANGUAGE_CODE = 'de' # treat this ^^^ as the default prefix_default_language = False diff --git a/gawa/gawa/urls.py b/gawa/gawa/urls.py index bdb7d74..c50c36b 100644 --- a/gawa/gawa/urls.py +++ b/gawa/gawa/urls.py @@ -14,14 +14,14 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf.urls.i18n import i18n_patterns +from django.conf.urls import url from django.contrib import admin from django.urls import include, path urlpatterns = [ - path("", include("start.urls")), - path("blog/", include("blog.urls")), - path('admin/', admin.site.urls), + url(r'^i18n/', include('django.conf.urls.i18n')), ] + urlpatterns += i18n_patterns( path("", include("start.urls")), path("blog/", include("blog.urls")), diff --git a/gawa/start/middleware.py b/gawa/start/middleware.py new file mode 100644 index 0000000..d39d4b1 --- /dev/null +++ b/gawa/start/middleware.py @@ -0,0 +1,20 @@ +from django.utils import translation +from django.conf import settings +from django.utils.deprecation import MiddlewareMixin + + +class LangBasedOnUrlMiddleware(MiddlewareMixin): + + @staticmethod + def process_request(request): + + if hasattr(request, 'session'): + active_session_lang = request.session.get(translation.LANGUAGE_SESSION_KEY) + + if active_session_lang == request.LANGUAGE_CODE: + return + + if any(request.LANGUAGE_CODE in language for language in settings.LANGUAGES): + translation.activate(request.LANGUAGE_CODE) + request.session[translation.LANGUAGE_SESSION_KEY] = request.LANGUAGE_CODE + diff --git a/gawa/start/migrations/0001_initial.py b/gawa/start/migrations/0001_initial.py new file mode 100644 index 0000000..f414316 --- /dev/null +++ b/gawa/start/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.19 on 2023-05-30 17:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='MainSearchEntry', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=50)), + ('subtitle', models.CharField(max_length=50)), + ('desc', models.CharField(max_length=250, unique=True)), + ('date', models.DateField(blank=True)), + ('link', models.URLField()), + ], + ), + ] diff --git a/gawa/start/models.py b/gawa/start/models.py index 71a8362..039c82e 100644 --- a/gawa/start/models.py +++ b/gawa/start/models.py @@ -1,3 +1,16 @@ from django.db import models -# Create your models here. +class MainSearchEntry(models.Model): + """ + This model will be searched for by the searchbox that is in every upper part of the website. + The view making use of it is MainSearchView. + + Any model object that I implement as searchable should eventually have an entry here. + """ + title = models.CharField(max_length=50) + subtitle = models.CharField(max_length=50) + desc = models.CharField(max_length=250, unique=True) + # may be empty/blank for some entries + date = models.DateField(blank=True) + # every searchable object should have some url associated with it. + link = models.URLField() diff --git a/gawa/start/templates/start/base.html b/gawa/start/templates/start/base.html index 30cdf8c..8febf0b 100644 --- a/gawa/start/templates/start/base.html +++ b/gawa/start/templates/start/base.html @@ -1,17 +1,18 @@ {% load i18n %} +{% load helper_tags %} +{% load static %} {% block title %}{% endblock title %} - Bootstrap demo - + -