commit 7c35d0f046ceb738a3a2565d2fdf1ccf0b5953f3 Author: Edgar P. Burkhart Date: Fri Jun 14 14:57:03 2024 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f1c35b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/env +__pycache__ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..9294a9f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,27 @@ +repos: + - repo: https://github.com/PyCQA/isort + rev: 5.12.0 + hooks: + - id: isort + args: ["--profile", "black"] + - repo: https://github.com/psf/black + rev: 23.3.0 + hooks: + - id: black + - repo: https://github.com/PyCQA/flake8 + rev: "6.0.0" + hooks: + - id: flake8 + args: ["--max-line-length=88", "--extend-ignore=E203"] + exclude: "blog/blog/settings/|migrations" + - repo: https://github.com/Riverside-Healthcare/djLint + rev: v1.23.3 + hooks: + - id: djlint-django + args: ["--reformat"] + - repo: https://github.com/pre-commit/mirrors-prettier + rev: "v3.0.0-alpha.6" + hooks: + - id: prettier + types_or: ["css", "javascript", "svg"] + exclude: "css" diff --git a/blog/.dockerignore b/blog/.dockerignore new file mode 100644 index 0000000..63ce98d --- /dev/null +++ b/blog/.dockerignore @@ -0,0 +1,39 @@ +# Django project +/media/ +/static/ +*.sqlite3 + +# Python and others +__pycache__ +*.pyc +.DS_Store +*.swp +/venv/ +/tmp/ +/.vagrant/ +/Vagrantfile.local +node_modules/ +/npm-debug.log +/.idea/ +.vscode +coverage +.python-version + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg diff --git a/blog/.gitignore b/blog/.gitignore new file mode 100644 index 0000000..f9115f9 --- /dev/null +++ b/blog/.gitignore @@ -0,0 +1,2 @@ +/db.sqlite3 +/media diff --git a/blog/Dockerfile b/blog/Dockerfile new file mode 100644 index 0000000..f82fa12 --- /dev/null +++ b/blog/Dockerfile @@ -0,0 +1,60 @@ +# Use an official Python runtime based on Debian 10 "buster" as a parent image. +FROM python:3.8.1-slim-buster + +# Add user that will be used in the container. +RUN useradd wagtail + +# Port used by this container to serve HTTP. +EXPOSE 8000 + +# Set environment variables. +# 1. Force Python stdout and stderr streams to be unbuffered. +# 2. Set PORT variable that is used by Gunicorn. This should match "EXPOSE" +# command. +ENV PYTHONUNBUFFERED=1 \ + PORT=8000 + +# Install system packages required by Wagtail and Django. +RUN apt-get update --yes --quiet && apt-get install --yes --quiet --no-install-recommends \ + build-essential \ + libpq-dev \ + libmariadbclient-dev \ + libjpeg62-turbo-dev \ + zlib1g-dev \ + libwebp-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install the application server. +RUN pip install "gunicorn==20.0.4" + +# Install the project requirements. +COPY requirements.txt / +RUN pip install -r /requirements.txt + +# Use /app folder as a directory where the source code is stored. +WORKDIR /app + +# Set this directory to be owned by the "wagtail" user. This Wagtail project +# uses SQLite, the folder needs to be owned by the user that +# will be writing to the database file. +RUN chown wagtail:wagtail /app + +# Copy the source code of the project into the container. +COPY --chown=wagtail:wagtail . . + +# Use user "wagtail" to run the build commands below and the server itself. +USER wagtail + +# Collect static files. +RUN python manage.py collectstatic --noinput --clear + +# Runtime command that executes when "docker run" is called, it does the +# following: +# 1. Migrate the database. +# 2. Start the application server. +# WARNING: +# Migrating database at the same time as starting the server IS NOT THE BEST +# PRACTICE. The database should be migrated manually or using the release +# phase facilities of your hosting platform. This is used only so the +# Wagtail instance can be started with a simple "docker run" command. +CMD set -xe; python manage.py migrate --noinput; gunicorn blog.wsgi:application diff --git a/blog/blog/__init__.py b/blog/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/blog/settings/__init__.py b/blog/blog/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/blog/settings/base.py b/blog/blog/settings/base.py new file mode 100644 index 0000000..fbb2250 --- /dev/null +++ b/blog/blog/settings/base.py @@ -0,0 +1,191 @@ +""" +Django settings for blog project. + +Generated by 'django-admin startproject' using Django 5.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.0/ref/settings/ +""" + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import os + +PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +BASE_DIR = os.path.dirname(PROJECT_DIR) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ + + +# Application definition + +INSTALLED_APPS = [ + "home", + "search", + "wagtail.contrib.forms", + "wagtail.contrib.redirects", + "wagtail.embeds", + "wagtail.sites", + "wagtail.users", + "wagtail.snippets", + "wagtail.documents", + "wagtail.images", + "wagtail.search", + "wagtail.admin", + "wagtail", + "modelcluster", + "taggit", + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", +] + +MIDDLEWARE = [ + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", + "django.middleware.security.SecurityMiddleware", + "wagtail.contrib.redirects.middleware.RedirectMiddleware", +] + +ROOT_URLCONF = "blog.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [ + os.path.join(PROJECT_DIR, "templates"), + ], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "blog.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/5.0/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.0/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.0/howto/static-files/ + +STATICFILES_FINDERS = [ + "django.contrib.staticfiles.finders.FileSystemFinder", + "django.contrib.staticfiles.finders.AppDirectoriesFinder", +] + +STATICFILES_DIRS = [ + os.path.join(PROJECT_DIR, "static"), +] + +STATIC_ROOT = os.path.join(BASE_DIR, "static") +STATIC_URL = "/static/" + +MEDIA_ROOT = os.path.join(BASE_DIR, "media") +MEDIA_URL = "/media/" + +# Default storage settings, with the staticfiles storage updated. +# See https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-STORAGES +STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + # ManifestStaticFilesStorage is recommended in production, to prevent + # outdated JavaScript / CSS assets being served from cache + # (e.g. after a Wagtail upgrade). + # See https://docs.djangoproject.com/en/5.0/ref/contrib/staticfiles/#manifeststaticfilesstorage + "staticfiles": { + "BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage", + }, +} + + +# Wagtail settings + +WAGTAIL_SITE_NAME = "blog" + +# Search +# https://docs.wagtail.org/en/stable/topics/search/backends.html +WAGTAILSEARCH_BACKENDS = { + "default": { + "BACKEND": "wagtail.search.backends.database", + } +} + +# 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 = "http://example.com" + +# Allowed file extensions for documents in the document library. +# This can be omitted to allow all files, but note that this may present a security risk +# if untrusted users are allowed to upload files - +# see https://docs.wagtail.org/en/stable/advanced_topics/deploying.html#user-uploaded-files +WAGTAILDOCS_EXTENSIONS = [ + "csv", + "docx", + "key", + "odt", + "pdf", + "pptx", + "rtf", + "txt", + "xlsx", + "zip", +] diff --git a/blog/blog/settings/dev.py b/blog/blog/settings/dev.py new file mode 100644 index 0000000..5814c4f --- /dev/null +++ b/blog/blog/settings/dev.py @@ -0,0 +1,18 @@ +from .base import * + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-))^+br5e3)maa$(1fxusy*)75@ovprip0_mq9ut=a!^-gteptq" + +# SECURITY WARNING: define the correct hosts in production! +ALLOWED_HOSTS = ["*"] + +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + + +try: + from .local import * +except ImportError: + pass diff --git a/blog/blog/settings/production.py b/blog/blog/settings/production.py new file mode 100644 index 0000000..9ca4ed7 --- /dev/null +++ b/blog/blog/settings/production.py @@ -0,0 +1,8 @@ +from .base import * + +DEBUG = False + +try: + from .local import * +except ImportError: + pass diff --git a/blog/blog/static/css/blog.css b/blog/blog/static/css/blog.css new file mode 100644 index 0000000..e69de29 diff --git a/blog/blog/static/js/blog.js b/blog/blog/static/js/blog.js new file mode 100644 index 0000000..e69de29 diff --git a/blog/blog/templates/404.html b/blog/blog/templates/404.html new file mode 100644 index 0000000..47ead9a --- /dev/null +++ b/blog/blog/templates/404.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block title %}Page not found{% endblock %} +{% block body_class %}template-404{% endblock %} +{% block content %} +

Page not found

+

Sorry, this page could not be found.

+{% endblock %} diff --git a/blog/blog/templates/500.html b/blog/blog/templates/500.html new file mode 100644 index 0000000..725c0df --- /dev/null +++ b/blog/blog/templates/500.html @@ -0,0 +1,12 @@ + + + + + Internal server error + + + +

Internal server error

+

Sorry, there seems to be an error. Please try again soon.

+ + diff --git a/blog/blog/templates/base.html b/blog/blog/templates/base.html new file mode 100644 index 0000000..f3744f5 --- /dev/null +++ b/blog/blog/templates/base.html @@ -0,0 +1,41 @@ +{% load static wagtailcore_tags wagtailuserbar %} + + + + + + {% block title %} + {% if page.seo_title %} + {{ page.seo_title }} + {% else %} + {{ page.title }} + {% endif %} + {% endblock %} + {% block title_suffix %} + {% wagtail_site as current_site %} + {% if current_site and current_site.site_name %}- {{ current_site.site_name }}{% endif %} + {% endblock %} + + {% if page.search_description %}{% endif %} + + {# Force all links in the live preview panel to be opened in a new tab #} + {% if request.in_preview_panel %}{% endif %} + {# Global stylesheets #} + + + + + + {% block extra_css %}{# Override this in templates to add extra stylesheets #}{% endblock %} + + + {% wagtailuserbar %} +
+ {% block content %}{% endblock %} +
+ {# Global javascript #} + + {% block extra_js %}{# Override this in templates to add extra javascript #}{% endblock %} + + diff --git a/blog/blog/urls.py b/blog/blog/urls.py new file mode 100644 index 0000000..9b09f89 --- /dev/null +++ b/blog/blog/urls.py @@ -0,0 +1,33 @@ +from django.conf import settings +from django.contrib import admin +from django.urls import include, path +from search import views as search_views +from wagtail import urls as wagtail_urls +from wagtail.admin import urls as wagtailadmin_urls +from wagtail.documents import urls as wagtaildocs_urls + +urlpatterns = [ + path("django-admin/", admin.site.urls), + path("admin/", include(wagtailadmin_urls)), + path("documents/", include(wagtaildocs_urls)), + path("search/", search_views.search, name="search"), +] + + +if settings.DEBUG: + from django.conf.urls.static import static + from django.contrib.staticfiles.urls import staticfiles_urlpatterns + + # Serve static and media files from development server + urlpatterns += staticfiles_urlpatterns() + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + +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)), +] diff --git a/blog/blog/wsgi.py b/blog/blog/wsgi.py new file mode 100644 index 0000000..e107762 --- /dev/null +++ b/blog/blog/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for blog project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings.dev") + +application = get_wsgi_application() diff --git a/blog/home/__init__.py b/blog/home/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/home/migrations/0001_initial.py b/blog/home/migrations/0001_initial.py new file mode 100644 index 0000000..27146c6 --- /dev/null +++ b/blog/home/migrations/0001_initial.py @@ -0,0 +1,30 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("wagtailcore", "0040_page_draft_title"), + ] + + operations = [ + migrations.CreateModel( + name="HomePage", + fields=[ + ( + "page_ptr", + models.OneToOneField( + on_delete=models.CASCADE, + parent_link=True, + auto_created=True, + primary_key=True, + serialize=False, + to="wagtailcore.Page", + ), + ), + ], + options={ + "abstract": False, + }, + bases=("wagtailcore.page",), + ), + ] diff --git a/blog/home/migrations/0002_create_homepage.py b/blog/home/migrations/0002_create_homepage.py new file mode 100644 index 0000000..a8586fb --- /dev/null +++ b/blog/home/migrations/0002_create_homepage.py @@ -0,0 +1,60 @@ +from django.db import migrations + + +def create_homepage(apps, schema_editor): + # Get models + ContentType = apps.get_model("contenttypes.ContentType") + Page = apps.get_model("wagtailcore.Page") + Site = apps.get_model("wagtailcore.Site") + HomePage = apps.get_model("home.HomePage") + + # Delete the default homepage + # If migration is run multiple times, it may have already been deleted + Page.objects.filter(id=2).delete() + + # Create content type for homepage model + homepage_content_type, __ = ContentType.objects.get_or_create( + model="homepage", app_label="home" + ) + + # Create a new homepage + homepage = HomePage.objects.create( + title="Home", + draft_title="Home", + slug="home", + content_type=homepage_content_type, + path="00010001", + depth=2, + numchild=0, + url_path="/home/", + ) + + # Create a site with the new homepage set as the root + Site.objects.create(hostname="localhost", root_page=homepage, is_default_site=True) + + +def remove_homepage(apps, schema_editor): + # Get models + ContentType = apps.get_model("contenttypes.ContentType") + HomePage = apps.get_model("home.HomePage") + + # Delete the default homepage + # Page and Site objects CASCADE + HomePage.objects.filter(slug="home", depth=2).delete() + + # Delete content type for homepage model + ContentType.objects.filter(model="homepage", app_label="home").delete() + + +class Migration(migrations.Migration): + run_before = [ + ("wagtailcore", "0053_locale_model"), + ] + + dependencies = [ + ("home", "0001_initial"), + ] + + operations = [ + migrations.RunPython(create_homepage, remove_homepage), + ] diff --git a/blog/home/migrations/0003_homepage_body_homepagelink.py b/blog/home/migrations/0003_homepage_body_homepagelink.py new file mode 100644 index 0000000..ea42ca7 --- /dev/null +++ b/blog/home/migrations/0003_homepage_body_homepagelink.py @@ -0,0 +1,52 @@ +# Generated by Django 5.0.6 on 2024-06-14 12:48 + +import django.db.models.deletion +import modelcluster.fields +import wagtail.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("home", "0002_create_homepage"), + ] + + operations = [ + migrations.AddField( + model_name="homepage", + name="body", + field=wagtail.fields.RichTextField(blank=True), + ), + migrations.CreateModel( + name="HomePageLink", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "sort_order", + models.IntegerField(blank=True, editable=False, null=True), + ), + ("title", models.CharField(max_length=256)), + ("link", models.URLField()), + ( + "page", + modelcluster.fields.ParentalKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="links", + to="home.homepage", + ), + ), + ], + options={ + "ordering": ["sort_order"], + "abstract": False, + }, + ), + ] diff --git a/blog/home/migrations/__init__.py b/blog/home/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/home/models.py b/blog/home/models.py new file mode 100644 index 0000000..7ee5de8 --- /dev/null +++ b/blog/home/models.py @@ -0,0 +1,22 @@ +from django.db import models +from modelcluster.fields import ParentalKey +from wagtail.admin.panels import FieldPanel, InlinePanel +from wagtail.fields import RichTextField +from wagtail.models import Orderable, Page + + +class HomePage(Page): + body = RichTextField(blank=True) + + content_panels = Page.content_panels + [ + FieldPanel("body"), + InlinePanel("links"), + ] + + +class HomePageLink(Orderable): + page = ParentalKey(HomePage, on_delete=models.CASCADE, related_name="links") + title = models.CharField(max_length=256) + link = models.URLField() + + panels = [FieldPanel("title"), FieldPanel("link")] diff --git a/blog/home/templates/home/home_page.html b/blog/home/templates/home/home_page.html new file mode 100644 index 0000000..389865b --- /dev/null +++ b/blog/home/templates/home/home_page.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block content %} +

{{ page.title }}

+
+ {% for link in page.links.all %}
{{ link.title }}
{% endfor %} +
+{% endblock content %} diff --git a/blog/manage.py b/blog/manage.py new file mode 100644 index 0000000..4ac5c1f --- /dev/null +++ b/blog/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings.dev") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/blog/requirements.txt b/blog/requirements.txt new file mode 100644 index 0000000..54c1cbd --- /dev/null +++ b/blog/requirements.txt @@ -0,0 +1,2 @@ +Django>=4.2,<5.1 +wagtail>=6.1,<6.2 diff --git a/blog/search/__init__.py b/blog/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/search/templates/search/search.html b/blog/search/templates/search/search.html new file mode 100644 index 0000000..dc05d92 --- /dev/null +++ b/blog/search/templates/search/search.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} +{% load static wagtailcore_tags %} +{% block body_class %}template-searchresults{% endblock %} +{% block title %}Search{% endblock %} +{% block content %} +

Search

+
+ + +
+ {% if search_results %} + + {% if search_results.has_previous %} + Previous + {% endif %} + {% if search_results.has_next %} + Next + {% endif %} + {% elif search_query %} + No results found + {% endif %} +{% endblock %} diff --git a/blog/search/views.py b/blog/search/views.py new file mode 100644 index 0000000..c8a81d3 --- /dev/null +++ b/blog/search/views.py @@ -0,0 +1,45 @@ +from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator +from django.template.response import TemplateResponse +from wagtail.models import Page + +# To enable logging of search queries for use with the "Promoted search results" module +# +# uncomment the following line and the lines indicated in the search function +# (after adding wagtail.contrib.search_promotions to INSTALLED_APPS): + +# from wagtail.contrib.search_promotions.models import Query + + +def search(request): + search_query = request.GET.get("query", None) + page = request.GET.get("page", 1) + + # Search + if search_query: + search_results = Page.objects.live().search(search_query) + + # To log this query for use with the "Promoted search results" module: + + # query = Query.get(search_query) + # query.add_hit() + + else: + search_results = Page.objects.none() + + # Pagination + paginator = Paginator(search_results, 10) + try: + search_results = paginator.page(page) + except PageNotAnInteger: + search_results = paginator.page(1) + except EmptyPage: + search_results = paginator.page(paginator.num_pages) + + return TemplateResponse( + request, + "search/search.html", + { + "search_query": search_query, + "search_results": search_results, + }, + )