From e98db55a16493a142600137befe5b2fa3576e811 Mon Sep 17 00:00:00 2001 From: Konstantin Kostov Date: Sat, 23 Nov 2024 14:26:14 +0100 Subject: [PATCH 1/3] feat: enable translations --- iamkonstantin_web/settings/base.py | 14 +++++++++++++- iamkonstantin_web/urls.py | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/iamkonstantin_web/settings/base.py b/iamkonstantin_web/settings/base.py index 44ac0f7..4a71085 100644 --- a/iamkonstantin_web/settings/base.py +++ b/iamkonstantin_web/settings/base.py @@ -43,6 +43,7 @@ INSTALLED_APPS = [ "wagtail.images", "wagtail.search", "wagtail.admin", + "wagtail.locales", "wagtail", "modelcluster", "taggit", @@ -68,6 +69,7 @@ MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "wagtail.contrib.redirects.middleware.RedirectMiddleware", "blog.middleware.BlogRedirectMiddleware", + "django.middleware.locale.LocaleMiddleware", "django_browser_reload.middleware.BrowserReloadMiddleware" ] @@ -128,7 +130,7 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/5.0/topics/i18n/ -LANGUAGE_CODE = "en-us" +LANGUAGE_CODE = "en" TIME_ZONE = "UTC" @@ -136,6 +138,16 @@ USE_I18N = True USE_TZ = True +WAGTAIL_I18N_ENABLED = True + +USE_L10N = True # allows dates to be shown in the user's locale + +WAGTAIL_CONTENT_LANGUAGES = LANGUAGES = [ + ('en', "English"), + ('fr', "French"), + ('es', "Spanish"), + ('nl', "Dutch"), +] # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ diff --git a/iamkonstantin_web/urls.py b/iamkonstantin_web/urls.py index d53a50e..2f409b7 100644 --- a/iamkonstantin_web/urls.py +++ b/iamkonstantin_web/urls.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.conf.urls.i18n import i18n_patterns from django.urls import include, path from django.contrib import admin from django.views.generic.base import TemplateView @@ -25,6 +26,13 @@ urlpatterns = [ path('newsletter/thanks', newsletter_views.thanks, name='thanks') ] +# Translatable URLs +# These will be available under a language code prefix. For example /en/search/ +urlpatterns += i18n_patterns( + path("", include(wagtail_urls)), + prefix_default_language=False, +) + if settings.DEBUG: from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns From 310fd5ed7a6fa5b6483223ce089bb22cf8993321 Mon Sep 17 00:00:00 2001 From: Konstantin Kostov Date: Sat, 23 Nov 2024 14:59:51 +0100 Subject: [PATCH 2/3] feat: enable translations and rest api --- blog/models.py | 11 +++++++++++ iamkonstantin_web/api.py | 15 +++++++++++++++ iamkonstantin_web/settings/base.py | 8 ++++++++ iamkonstantin_web/urls.py | 23 ++++++++++++++--------- 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 iamkonstantin_web/api.py diff --git a/blog/models.py b/blog/models.py index c0279c0..64ad0d5 100644 --- a/blog/models.py +++ b/blog/models.py @@ -5,6 +5,7 @@ from django.db import models from modelcluster.fields import ParentalKey, ParentalManyToManyField from modelcluster.contrib.taggit import ClusterTaggableManager from taggit.models import TaggedItemBase +from wagtail.api import APIField from wagtail.models import Page, Orderable from wagtail.fields import RichTextField, StreamField @@ -74,6 +75,13 @@ class BlogPage(SeoMixin, Page): else: return None + # Export fields over the API + api_fields = [ + APIField('gallery_images'), + APIField('body'), + APIField('intro'), + APIField('tags'), + ] search_fields = Page.search_fields + [ index.SearchField('intro'), index.SearchField('body'), @@ -122,6 +130,9 @@ class BlogPageGalleryImage(Orderable): FieldPanel('image'), FieldPanel('caption'), ] + api_fields = [ + APIField('image'), + ] @register_snippet diff --git a/iamkonstantin_web/api.py b/iamkonstantin_web/api.py new file mode 100644 index 0000000..9415198 --- /dev/null +++ b/iamkonstantin_web/api.py @@ -0,0 +1,15 @@ +from wagtail.api.v2.views import PagesAPIViewSet +from wagtail.api.v2.router import WagtailAPIRouter +from wagtail.images.api.v2.views import ImagesAPIViewSet +from wagtail.documents.api.v2.views import DocumentsAPIViewSet + +# Create the router. "wagtailapi" is the URL namespace +api_router = WagtailAPIRouter('wagtailapi') + +# Add the three endpoints using the "register_endpoint" method. +# The first parameter is the name of the endpoint (such as pages, images). This +# is used in the URL of the endpoint +# The second parameter is the endpoint class that handles the requests +api_router.register_endpoint('pages', PagesAPIViewSet) +api_router.register_endpoint('images', ImagesAPIViewSet) +api_router.register_endpoint('documents', DocumentsAPIViewSet) \ No newline at end of file diff --git a/iamkonstantin_web/settings/base.py b/iamkonstantin_web/settings/base.py index 4a71085..d3cff38 100644 --- a/iamkonstantin_web/settings/base.py +++ b/iamkonstantin_web/settings/base.py @@ -35,6 +35,7 @@ INSTALLED_APPS = [ "wagtail.contrib.settings", "wagtail.contrib.forms", "wagtail.contrib.redirects", + "wagtail.contrib.simple_translation", "wagtail.embeds", "wagtail.sites", "wagtail.users", @@ -44,6 +45,8 @@ INSTALLED_APPS = [ "wagtail.search", "wagtail.admin", "wagtail.locales", + "wagtail.api.v2", + "rest_framework", "wagtail", "modelcluster", "taggit", @@ -149,6 +152,8 @@ WAGTAIL_CONTENT_LANGUAGES = LANGUAGES = [ ('nl', "Dutch"), ] +WAGTAILSIMPLETRANSLATION_SYNC_PAGE_TREE = True + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ @@ -198,6 +203,9 @@ WAGTAILSEARCH_BACKENDS = { # Base URL to use when referring to full URLs within the Wagtail admin backend - # e.g. in notification emails. Don't include '/admin' or a trailing slash WAGTAILADMIN_BASE_URL = "https://iamkonstantin.eu" +WAGTAILAPI_BASE_URL = "https://iamkonstantin.eu" + +WAGTAILAPI_SEARCH_ENABLED = True WAGTAIL_CODE_BLOCK_LANGUAGES = ( ('bash', 'Bash/Shell'), diff --git a/iamkonstantin_web/urls.py b/iamkonstantin_web/urls.py index 2f409b7..4b29694 100644 --- a/iamkonstantin_web/urls.py +++ b/iamkonstantin_web/urls.py @@ -9,6 +9,7 @@ from wagtail import urls as wagtail_urls from wagtail.documents import urls as wagtaildocs_urls from blog.feeds import RssBlogFeed +from iamkonstantin_web.api import api_router from newsletter import views as newsletter_views from search import views as search_views from wagtail.contrib.sitemaps.views import sitemap @@ -26,6 +27,10 @@ urlpatterns = [ path('newsletter/thanks', newsletter_views.thanks, name='thanks') ] +urlpatterns += [ + path('api/v2/', api_router.urls), +] + # Translatable URLs # These will be available under a language code prefix. For example /en/search/ urlpatterns += i18n_patterns( @@ -42,12 +47,12 @@ if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += path("__reload__/", include("django_browser_reload.urls")), -urlpatterns = urlpatterns + [ - # For anything not caught by a more specific rule above, hand over to - # Wagtail's page serving mechanism. This should be the last pattern in - # the list: - path("", include(wagtail_urls)), - # Alternatively, if you want Wagtail pages to be served from a subpath - # of your site, rather than the site root: - # path("pages/", include(wagtail_urls)), -] +# urlpatterns = urlpatterns + [ +# # For anything not caught by a more specific rule above, hand over to +# # Wagtail's page serving mechanism. This should be the last pattern in +# # the list: +# path("", include(wagtail_urls)), +# # Alternatively, if you want Wagtail pages to be served from a subpath +# # of your site, rather than the site root: +# # path("pages/", include(wagtail_urls)), +# ] From 1b3a2dec72330cbc7feb513cd4baffd3e022fa2c Mon Sep 17 00:00:00 2001 From: Konstantin Kostov Date: Sat, 23 Nov 2024 15:00:00 +0100 Subject: [PATCH 3/3] feat: update wagtail to 6.3.1 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 69558ab..04fcbe1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ Django>=4.2,<5.2 -wagtail>=6.3,<6.4 +wagtail>=6.3.1,<6.4 whitenoise>=6.6,<7.0 wagtailcodeblock>=1.29.0.2,<2.0 django-tailwind>=3.6.0