From 9f38029f62ddf4029d4795b38503c64a1e7b3862 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sat, 6 Nov 2021 22:39:54 -0700 Subject: [PATCH] Fix up a few more things, add a Dockerfile and a local docker-compose deployment --- Dockerfile | 13 +++ docker-compose.local.yaml | 21 +++++ libertywiki/decorators.py | 6 +- libertywiki/templates/admin/base.html | 104 +++++++++++++++++++++ libertywiki/templates/admin/index.html | 13 +-- libertywiki/templates/admin/my_master.html | 4 +- libertywiki/templates/base.html | 27 +++++- libertywiki/templates/page.html | 4 +- libertywiki/utils.py | 9 +- libertywiki/wsgi.py | 4 + nginx.conf | 10 ++ 11 files changed, 190 insertions(+), 25 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.local.yaml create mode 100644 libertywiki/templates/admin/base.html create mode 100644 libertywiki/wsgi.py create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f73f84e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3 + +RUN useradd --system --gid root --uid 1001 uwsgi +RUN pip install uwsgi psycopg2_binary + +RUN mkdir -p /app +COPY . /app +WORKDIR /app +RUN pip install . + +EXPOSE 3031 +CMD ["uwsgi", "--master", "--workers", "4", "--socket", "0.0.0.0:3031", "--protocol", "uwsgi", \ + "--plugin", "python3", "--uid", "uwsgi", "--wsgi", "libertywiki.wsgi"] diff --git a/docker-compose.local.yaml b/docker-compose.local.yaml new file mode 100644 index 0000000..8b6db8f --- /dev/null +++ b/docker-compose.local.yaml @@ -0,0 +1,21 @@ +version: '3' +services: + wiki: + build: + context: . + environment: + - SQLALCHEMY_DATABASE_URI=postgresql://wiki:wiki@postgres/wiki + - SECRET_KEY=super-secret-key-replace-me + - SECURITY_PASSWORD_SALT=password-salt-replace-me + postgres: + image: postgres:14 + environment: + - POSTGRES_USER=wiki + - POSTGRES_PASSWORD=wiki + - POSTGRES_DB=wiki + nginx: + image: nginx + volumes: + - "./nginx.conf:/etc/nginx/conf.d/default.conf:ro" + ports: + - "8000:80" diff --git a/libertywiki/decorators.py b/libertywiki/decorators.py index 9311098..9cb391f 100644 --- a/libertywiki/decorators.py +++ b/libertywiki/decorators.py @@ -4,7 +4,7 @@ from flask import current_app, request from flask_login import current_user from flask_login.config import EXEMPT_METHODS -from libertywiki.utils import AccessType, WikiMode +from libertywiki.utils import WikiMode, needs_authentication def check_access(access_type): @@ -21,12 +21,10 @@ def check_access(access_type): if request.method in EXEMPT_METHODS or \ current_app.config.get('LOGIN_DISABLED'): pass - elif (wiki_mode == WikiMode.PRIVATE - or (wiki_mode == WikiMode.PUBLIC and access_type == AccessType.READ)) \ + elif needs_authentication(wiki_mode, access_type) \ and not current_user.is_authenticated: return current_app.login_manager.unauthorized() try: - # current_app.ensure_sync available in Flask >= 2.0 return current_app.ensure_sync(func)(*args, **kwargs) except AttributeError: return func(*args, **kwargs) diff --git a/libertywiki/templates/admin/base.html b/libertywiki/templates/admin/base.html new file mode 100644 index 0000000..d67565e --- /dev/null +++ b/libertywiki/templates/admin/base.html @@ -0,0 +1,104 @@ +{% import 'admin/layout.html' as layout with context -%} +{% import 'admin/static.html' as admin_static with context %} + + + + {% block title %}{% if admin_view.category %}{{ admin_view.category }} - {% endif %}{{ admin_view.name }} - {{ admin_view.admin.name }}{% endblock %} + {% block head_meta %} + + + + + + {% endblock %} + {% block head_css %} + + {% if config.get('FLASK_ADMIN_SWATCH', 'default') == 'default' %} + + {% endif %} + + + {% if admin_view.extra_css %} + {% for css_url in admin_view.extra_css %} + + {% endfor %} + {% endif %} + + {% endblock %} + {% block head %} + {% endblock %} + {% block head_tail %} + {% endblock %} + + +{% block page_body %} + + +
+ {% block messages %} + {{ layout.messages() }} + {% endblock %} + + {# store the jinja2 context for form_rules rendering logic #} + {% set render_ctx = h.resolve_ctx() %} + + {% block body %}{% endblock %} +
+{% endblock %} + +{% block tail_js %} + + + + + + + + + + {% if admin_view.extra_js %} + {% for js_url in admin_view.extra_js %} + + {% endfor %} + {% endif %} +{% endblock %} + + {% block tail %} + {% endblock %} + + + diff --git a/libertywiki/templates/admin/index.html b/libertywiki/templates/admin/index.html index a31f068..ee39e07 100644 --- a/libertywiki/templates/admin/index.html +++ b/libertywiki/templates/admin/index.html @@ -4,19 +4,8 @@
-

Flask-Admin example

-

- Authentication -

-

- This example shows how you can use Flask-Security for authentication. -

+

LibertyWiki Administration

{% if not current_user.is_authenticated %} -

You can register as a regular user, or log in as a superuser with the following credentials: -

    -
  • email: admin
  • -
  • password: admin
  • -

login register

diff --git a/libertywiki/templates/admin/my_master.html b/libertywiki/templates/admin/my_master.html index 6a7ba83..a444cb2 100644 --- a/libertywiki/templates/admin/my_master.html +++ b/libertywiki/templates/admin/my_master.html @@ -5,8 +5,8 @@