commit c3667ec4b9353d74b99ce3fd452ad13105e5f731 Author: Jake Howard Date: Sun Jul 22 17:12:04 2018 +0100 Add wagtail template with docker config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee2b938 --- /dev/null +++ b/.gitignore @@ -0,0 +1,194 @@ + +# Created by https://www.gitignore.io/api/node,django,python + +### Django ### +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +db.sqlite3 +media + +# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ +# in your Git repository. Update and uncomment the following line accordingly. +# /staticfiles/ + +### Node ### +# Logs +logs +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless + +### Python ### +# Byte-compiled / optimized / DLL files +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo + +# Django stuff: + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +### Python Patch ### +.venv/ + + +# End of https://www.gitignore.io/api/node,django,python diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..477c533 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM theorangeone/docker-pyenv-nvm + +ENV LC_ALL C.UTF-8 +ENV LANG C.UTF-8 + +RUN mkdir /app + +RUN apt-get update && apt-get install python3-pip git -y && pip3 install pipenv + +WORKDIR /app + +COPY Pipfile Pipfile +COPY Pipfile.lock Pipfile.lock +RUN pipenv install --deploy --system + +COPY . /app + +RUN ./manage.py collectstatic --noinput + +EXPOSE 8000 diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..92fa1e8 --- /dev/null +++ b/Pipfile @@ -0,0 +1,22 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +wagtail = "*" +dj-database-url = "*" +django-cache-url = "*" +"psycopg2" = "*" +"psycopg2-binary" = "*" +elasticsearch = "<3.0.0,>=2.0.0" +django-redis = "*" +whitenoise = "*" +waitress = "*" + +[dev-packages] +mypy = "*" +"flake8" = "*" + +[requires] +python_version = "3.6" diff --git a/docker-compose.production.yml b/docker-compose.production.yml new file mode 100644 index 0000000..78908dd --- /dev/null +++ b/docker-compose.production.yml @@ -0,0 +1,24 @@ +version: '3' + +services: + redis: + restart: always + image: redis:3.0 + expose: + - "6379" + elasticsearch: + image: elasticsearch:2.3 + restart: always + expose: + - "9200" + app: + environment: + CACHE_URL: redis://redis + ELASTICSEARCH_URL: http://elasticsearch:9200 + command: "waitress-server --port 8000 project.wsgi:application" + links: + - redis:redis + - elasticsearch:elasticsearch + depends_on: + - redis + - elasticsearch diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3f15acb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3' + +services: + db: + environment: + POSTGRES_DB: app_db + POSTGRES_USER: app_user + POSTGRES_PASSWORD: changeme + restart: always + image: postgres:10-alpine + expose: + - "5432" + volumes: + - ./docker-data/postgres-data:/var/lib/postgresql + app: + environment: + DJANGO_SECRET_KEY: changeme + DATABASE_URL: postgres://app_user:changeme@db/app_db + build: + context: . + dockerfile: ./Dockerfile + stdin_open: true + tty: true + command: ./manage.py runserver 0.0.0.0:8000 + links: + - db:db + ports: + - "8000:8000" + volumes: + - ".:/app" + depends_on: + - db diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..71b3fa5 --- /dev/null +++ b/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/production.sh b/production.sh new file mode 100755 index 0000000..b7a13f6 --- /dev/null +++ b/production.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -e + +docker-compose -f docker-compose.yml -f docker-compose.production.yml $@ diff --git a/project/home/__init__.py b/project/home/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/home/migrations/0001_initial.py b/project/home/migrations/0001_initial.py new file mode 100644 index 0000000..ef46d12 --- /dev/null +++ b/project/home/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +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/project/home/migrations/0002_create_homepage.py b/project/home/migrations/0002_create_homepage.py new file mode 100644 index 0000000..039f0f5 --- /dev/null +++ b/project/home/migrations/0002_create_homepage.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +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): + + dependencies = [ + ('home', '0001_initial'), + ] + + operations = [ + migrations.RunPython(create_homepage, remove_homepage), + ] diff --git a/project/home/migrations/__init__.py b/project/home/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/home/models.py b/project/home/models.py new file mode 100644 index 0000000..af7b579 --- /dev/null +++ b/project/home/models.py @@ -0,0 +1,7 @@ +from django.db import models + +from wagtail.core.models import Page + + +class HomePage(Page): + pass diff --git a/project/home/templates/home/home_page.html b/project/home/templates/home/home_page.html new file mode 100644 index 0000000..8b7df4e --- /dev/null +++ b/project/home/templates/home/home_page.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block body_class %}template-homepage{% endblock %} + +{% block content %} +

Welcome to your new Wagtail site!

+ +

You can access the admin interface here (make sure you have run "./manage.py createsuperuser" in the console first).

+ +

If you haven't already given the documentation a read, head over to http://docs.wagtail.io to start building on Wagtail

+{% endblock %} diff --git a/project/search/__init__.py b/project/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/search/templates/search/search.html b/project/search/templates/search/search.html new file mode 100644 index 0000000..5f222e5 --- /dev/null +++ b/project/search/templates/search/search.html @@ -0,0 +1,38 @@ +{% 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/project/search/views.py b/project/search/views.py new file mode 100644 index 0000000..eeace7c --- /dev/null +++ b/project/search/views.py @@ -0,0 +1,34 @@ +from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator +from django.shortcuts import render + +from wagtail.core.models import Page +from wagtail.search.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) + query = Query.get(search_query) + + # Record hit + 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 render(request, 'search/search.html', { + 'search_query': search_query, + 'search_results': search_results, + }) diff --git a/project/settings.py b/project/settings.py new file mode 100644 index 0000000..dd1ade2 --- /dev/null +++ b/project/settings.py @@ -0,0 +1,187 @@ +""" +Django settings for project project. + +Generated by 'django-admin startproject' using Django 2.0.7. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.0/ref/settings/ +""" + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import os +import dj_database_url +import django_cache_url + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + + +# Application definition + +INSTALLED_APPS = [ + 'project.home', + 'project.search', + + 'wagtail.contrib.forms', + 'wagtail.contrib.redirects', + 'wagtail.embeds', + 'wagtail.sites', + 'wagtail.users', + 'wagtail.snippets', + 'wagtail.documents', + 'wagtail.images', + 'wagtail.search', + 'wagtail.admin', + 'wagtail.core', + + 'modelcluster', + 'taggit', + + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'whitenoise.runserver_nostatic', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.gzip.GZipMiddleware', + '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', + 'whitenoise.middleware.WhiteNoiseMiddleware', + + 'wagtail.core.middleware.SiteMiddleware', + 'wagtail.contrib.redirects.middleware.RedirectMiddleware', +] + +ROOT_URLCONF = 'project.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + os.path.join(BASE_DIR, 'templates'), + ], + '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', + ], + 'loaders': [ + ('django.template.loaders.cached.Loader', [ + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', + ]), + ], + }, + }, +] + +WSGI_APPLICATION = 'project.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.0/ref/settings/#databases + +DATABASES = { + 'default': dj_database_url.config(conn_max_age=500) +} + + +# Password validation +# https://docs.djangoproject.com/en/2.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/2.0/topics/i18n/ + +LANGUAGE_CODE = 'en-gb' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.0/howto/static-files/ + + +STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' + + +STATICFILES_DIRS = ( + os.path.join(BASE_DIR, "static", "build"), +) + +STATIC_ROOT = os.path.join(BASE_DIR, 'collected-static') +STATIC_URL = '/static/' + +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') +MEDIA_URL = '/media/' + + +# Wagtail settings + +WAGTAIL_SITE_NAME = "project" + +# 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 +BASE_URL = 'http://example.com' + +DEBUG = True + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '@%p6v$-d_+)v!=5ow9-*!lw@2n+&9!f83^+p)#jegc%7h122xe' + +# SECURITY WARNING: define the correct hosts in production! +ALLOWED_HOSTS = ['*'] + +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' + +if 'CACHE_URL' in os.environ: + CACHES = {'default': django_cache_url.config()} + +if 'ELASTICSEARCH_URL' in os.environ: + WAGTAILSEARCH_BACKENDS = { + 'default': { + 'BACKEND': 'wagtail.search.backends.elasticsearch2', + 'URLS': [os.environ['ELASTICSEARCH_URL']], + 'INDEX': 'wagtail', + 'TIMEOUT': 5, + 'OPTIONS': {}, + 'INDEX_SETTINGS': {}, + } + } diff --git a/project/urls.py b/project/urls.py new file mode 100644 index 0000000..1682530 --- /dev/null +++ b/project/urls.py @@ -0,0 +1,33 @@ +from django.conf import settings +from django.conf.urls import include, url + +from wagtail.admin import urls as wagtailadmin_urls +from wagtail.core import urls as wagtail_urls +from wagtail.documents import urls as wagtaildocs_urls + +from project.search import views as search_views + +urlpatterns = [ + url(r'^admin/', include(wagtailadmin_urls)), + url(r'^documents/', include(wagtaildocs_urls)), + + url(r'^search/$', search_views.search, name='search'), + + # 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: + url(r'', include(wagtail_urls)), + + # Alternatively, if you want Wagtail pages to be served from a subpath + # of your site, rather than the site root: + # url(r'^pages/', include(wagtail_urls)), +] + + +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) diff --git a/project/wsgi.py b/project/wsgi.py new file mode 100644 index 0000000..21f07f3 --- /dev/null +++ b/project/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for project 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/2.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + +application = get_wsgi_application() diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..3a5500e --- /dev/null +++ b/templates/404.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block body_class %}template-404{% endblock %} + +{% block content %} +

Page not found

+ +

Sorry, this page could not be found.

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

Internal server error

+ +

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

+ + diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..ecd4aee --- /dev/null +++ b/templates/base.html @@ -0,0 +1,40 @@ +{% load static wagtailuserbar %} + + + + + + + {% block title %} + {% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %} + {% endblock %} + {% block title_suffix %} + {% with self.get_site.site_name as site_name %} + {% if site_name %}- {{ site_name }}{% endif %} + {% endwith %} + {% endblock %} + + + + + {# 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 %} + +