From 386013b13714b8deb864ba7c8197efe57411038f Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 29 Feb 2020 06:12:46 +0100 Subject: [PATCH] Initial commit --- .gitignore | 47 +++++++++ Dockerfile | 18 ++++ apps/tbde/__init__.py | 0 apps/tbde/apps.py | 0 apps/tbde/models.py | 0 apps/tbde/urls.py | 0 entrypoint.sh | 12 +++ manage.py | 21 ++++ requirements.txt | 27 +++++ tombola/__init__.py | 0 tombola/asgi.py | 16 +++ tombola/middlewares.py | 38 +++++++ tombola/settings/__init__.py | 46 +++++++++ tombola/settings/base.py | 176 ++++++++++++++++++++++++++++++++ tombola/settings/development.py | 50 +++++++++ tombola/settings/production.py | 49 +++++++++ tombola/urls.py | 21 ++++ tombola/wsgi.py | 16 +++ 18 files changed, 537 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 apps/tbde/__init__.py create mode 100644 apps/tbde/apps.py create mode 100644 apps/tbde/models.py create mode 100644 apps/tbde/urls.py create mode 100755 entrypoint.sh create mode 100755 manage.py create mode 100644 requirements.txt create mode 100644 tombola/__init__.py create mode 100644 tombola/asgi.py create mode 100644 tombola/middlewares.py create mode 100644 tombola/settings/__init__.py create mode 100644 tombola/settings/base.py create mode 100644 tombola/settings/development.py create mode 100644 tombola/settings/production.py create mode 100644 tombola/urls.py create mode 100644 tombola/wsgi.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b57ed74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,47 @@ +# Server config files +nginx_note.conf + +# Byte-compiled / optimized / DLL files +dist +build +__pycache__ +*.py[cod] +*$py.class +*.swp +*.egg-info +_build +.tox +.coverage +coverage + +# Translations +*.mo +*.pot + +# Jupyter Notebook +.ipynb_checkpoints + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# PyCharm project settings +.idea + +# VSCode project settings +.vscode + +# Local data +secrets.py +*.log + +# Virtualenv +env/ +venv/ +db.sqlite3 + +# Ignore migrations during first phase dev +migrations/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2c84082 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3-buster + +ENV PYTHONUNBUFFERED 1 + +RUN mkdir /code +WORKDIR /code + +RUN apt update && \ + apt install -y gettext nginx uwsgi uwsgi-plugin-python3 && \ + rm -rf /var/lib/apt/lists/* + +COPY requirements.txt /code/ +RUN pip install -r requirements.txt + +COPY . /code/ + +ENTRYPOINT ["/code/entrypoint.sh"] +EXPOSE 8000 diff --git a/apps/tbde/__init__.py b/apps/tbde/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/tbde/apps.py b/apps/tbde/apps.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/tbde/models.py b/apps/tbde/models.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/tbde/urls.py b/apps/tbde/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..8485ba9 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Copyright (C) 2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +python manage.py compilemessages +python manage.py makemigrations + +# Wait for database +sleep 2 +python manage.py migrate + +python manage.py runserver 0.0.0.0:8000 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..a33b4b1 --- /dev/null +++ b/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tombola.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..21c2480 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,27 @@ +certifi==2019.6.16 +chardet==3.0.4 +defusedxml==0.6.0 +Django~=2.2 +django-allauth==0.39.1 +django-autocomplete-light==3.5.1 +django-crispy-forms==1.7.2 +django-extensions==2.1.9 +django-filter==2.2.0 +django-guardian==2.1.0 +django-polymorphic==2.0.3 +djangorestframework==3.9.0 +django-rest-polymorphic==0.1.8 +django-reversion==3.0.3 +django-tables2==2.1.0 +docutils==0.14 +psycopg2==2.8.4 +idna==2.8 +oauthlib==3.1.0 +Pillow==6.1.0 +python3-openid==3.1.0 +pytz==2019.1 +requests==2.22.0 +requests-oauthlib==1.2.0 +six==1.12.0 +sqlparse==0.3.0 +urllib3==1.25.3 diff --git a/tombola/__init__.py b/tombola/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tombola/asgi.py b/tombola/asgi.py new file mode 100644 index 0000000..75e442e --- /dev/null +++ b/tombola/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for tombola project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tombola.settings') + +application = get_asgi_application() diff --git a/tombola/middlewares.py b/tombola/middlewares.py new file mode 100644 index 0000000..73b87e3 --- /dev/null +++ b/tombola/middlewares.py @@ -0,0 +1,38 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from django.http import HttpResponseRedirect + +from urllib.parse import urlencode, parse_qs, urlsplit, urlunsplit + + +class TurbolinksMiddleware(object): + """ + Send the `Turbolinks-Location` header in response to a visit that was redirected, + and Turbolinks will replace the browser's topmost history entry. + """ + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + response = self.get_response(request) + + is_turbolinks = request.META.get('HTTP_TURBOLINKS_REFERRER') + is_response_redirect = response.has_header('Location') + + if is_turbolinks: + if is_response_redirect: + location = response['Location'] + prev_location = request.session.pop('_turbolinks_redirect_to', None) + if prev_location is not None: + # relative subsequent redirect + if location.startswith('.'): + location = prev_location.split('?')[0] + location + request.session['_turbolinks_redirect_to'] = location + else: + if request.session.get('_turbolinks_redirect_to'): + location = request.session.pop('_turbolinks_redirect_to') + response['Turbolinks-Location'] = location + return response + diff --git a/tombola/settings/__init__.py b/tombola/settings/__init__.py new file mode 100644 index 0000000..68a40b8 --- /dev/null +++ b/tombola/settings/__init__.py @@ -0,0 +1,46 @@ +import os +import re + +from .base import * + +def read_env(): + """Pulled from Honcho code with minor updates, reads local default + environment variables from a .env file located in the project root + directory. + """ + try: + with open('.env') as f: + content = f.read() + except IOError: + content = '' + for line in content.splitlines(): + m1 = re.match(r'\A([A-Za-z_0-9]+)=(.*)\Z', line) + if m1: + key, val = m1.group(1), m1.group(2) + m2 = re.match(r"\A'(.*)'\Z", val) + if m2: + val = m2.group(1) + m3 = re.match(r'\A"(.*)"\Z', val) + if m3: + val = re.sub(r'\\(.)', r'\1', m3.group(1)) + os.environ.setdefault(key, val) + +read_env() + +app_stage = os.environ.get('DJANGO_APP_STAGE', 'dev') +if app_stage == 'prod': + from .production import * + DATABASES["default"]["PASSWORD"] = os.environ.get('DJANGO_DB_PASSWORD','CHANGE_ME_IN_ENV_SETTINGS') + SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY','CHANGE_ME_IN_ENV_SETTINGS') + ALLOWED_HOSTS.append(os.environ.get('ALLOWED_HOSTS','localhost')) +else: + from .development import * + +try: + from .secrets import * +except ImportError: + pass + +# env variables set at the of in /env/bin/activate +# don't forget to unset in deactivate ! + diff --git a/tombola/settings/base.py b/tombola/settings/base.py new file mode 100644 index 0000000..c7ab272 --- /dev/null +++ b/tombola/settings/base.py @@ -0,0 +1,176 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +import os +import sys + +from django.utils.translation import gettext_lazy as _ + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +APPS_DIR = os.path.realpath(os.path.join(BASE_DIR, "apps")) +sys.path.append(APPS_DIR) + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'CHANGE_ME_IN_LOCAL_SETTINGS!' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ADMINS = ( + # ('Admin', 'webmaster@example.com'), +) + +SITE_ID = 1 + +ALLOWED_HOSTS = [] + +# Application definition + +INSTALLED_APPS = [ + # Theme overrides Django Admin templates + # 'theme', + + # External apps + 'polymorphic', + 'reversion', + 'crispy_forms', + 'django_tables2', + # Django contrib + 'django.contrib.admin', + 'django.contrib.admindocs', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.sites', + 'django.contrib.messages', + 'django.contrib.staticfiles', + # API + 'rest_framework', + 'rest_framework.authtoken', + # Autocomplete + 'dal', + 'dal_select2', + + # Tombola apps + 'tbde', +] +LOGIN_REDIRECT_URL = '/' + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.contrib.admindocs.middleware.XViewMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.locale.LocaleMiddleware', + 'django.contrib.sites.middleware.CurrentSiteMiddleware', + 'tombola.middlewares.TurbolinksMiddleware', +] + +ROOT_URLCONF = 'tombola.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_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', + 'django.template.context_processors.request', + ], + }, + }, +] + +# Password validation +# https://docs.djangoproject.com/en/2.2/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', + }, +] + +# Django Guardian object permissions + +AUTHENTICATION_BACKENDS = ( + 'django.contrib.auth.backends.ModelBackend', # this is default + 'guardian.backends.ObjectPermissionBackend', +) + +REST_FRAMEWORK = { + # Use Django's standard `django.contrib.auth` permissions, + # or allow read-only access for unauthenticated users. + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ], + 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework.authentication.TokenAuthentication', + ] +} + +ANONYMOUS_USER_NAME = None # Disable guardian anonymous user + +GUARDIAN_GET_CONTENT_TYPE = 'polymorphic.contrib.guardian.get_polymorphic_base_content_type' + +# Internationalization +# https://docs.djangoproject.com/en/2.2/topics/i18n/ + +LANGUAGE_CODE = 'en' + +LANGUAGES = [ + ('de', _('German')), + ('en', _('English')), + ('fr', _('French')), +] + +TIME_ZONE = 'Europe/Paris' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +LOCALE_PATHS = [os.path.join(BASE_DIR, "locale")] + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.2/howto/static-files/ + +# Absolute path to the directory static files should be collected to. +# Don't put anything in this directory yourself; store your static files +# in apps' "static/" subdirectories and in STATICFILES_DIRS. +# Example: "/var/www/example.com/static/" +STATIC_ROOT = os.path.realpath(__file__) +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, 'static')] + +CRISPY_TEMPLATE_PACK = 'bootstrap4' +DJANGO_TABLES2_TEMPLATE = 'django_tables2/bootstrap4.html' +# URL prefix for static files. +# Example: "http://example.com/static/", "http://static.example.com/" +STATIC_URL = '/static/' + +ALIAS_VALIDATOR_REGEX = r'' + diff --git a/tombola/settings/development.py b/tombola/settings/development.py new file mode 100644 index 0000000..60055ee --- /dev/null +++ b/tombola/settings/development.py @@ -0,0 +1,50 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +######################## +# Development Settings # +######################## +# For local dev on your machine: +# - Enabled by default +# - use sqlite as a db engine , Debug is True. +# - standalone mail server +# - and more ... + + +# Database +# https://docs.djangoproject.com/en/2.2/ref/settings/#databases +from . import * +import os + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + +# Break it, fix it! +DEBUG = True + +# Mandatory ! +ALLOWED_HOSTS = ['*'] + +# Emails +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' +# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +# EMAIL_USE_SSL = False +# EMAIL_HOST = 'smtp.example.org' +# EMAIL_PORT = 25 +# EMAIL_HOST_USER = 'change_me' +# EMAIL_HOST_PASSWORD = 'change_me' + +SERVER_EMAIL = 'no-reply@example.org' + +# Security settings +SECURE_CONTENT_TYPE_NOSNIFF = False +SECURE_BROWSER_XSS_FILTER = False +SESSION_COOKIE_SECURE = False +CSRF_COOKIE_SECURE = False +CSRF_COOKIE_HTTPONLY = False +X_FRAME_OPTIONS = 'DENY' +SESSION_COOKIE_AGE = 60 * 60 * 3 diff --git a/tombola/settings/production.py b/tombola/settings/production.py new file mode 100644 index 0000000..296c17a --- /dev/null +++ b/tombola/settings/production.py @@ -0,0 +1,49 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +######################## +# Production Settings # +######################## +# For local dev on your machine: +# - Enabled by setting env variable DJANGO_APP_STAGE = 'prod' +# - use Postgresql as db engine +# - Debug should be false. +# - should have a dedicated mail server +# - and more ... + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'note_db', + 'USER': 'note', + 'PASSWORD': 'update_in_env_variable', + 'HOST': '127.0.0.1', + 'PORT': '', + } +} + +# Break it, fix it! +DEBUG = True + +# Mandatory ! +ALLOWED_HOSTS = ['127.0.0.1','note.comby.xyz'] + +# Emails +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' +# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +# EMAIL_USE_SSL = False +# EMAIL_HOST = 'smtp.example.org' +# EMAIL_PORT = 25 +# EMAIL_HOST_USER = 'change_me' +# EMAIL_HOST_PASSWORD = 'change_me' + +SERVER_EMAIL = 'no-reply@example.org' + +# Security settings +SECURE_CONTENT_TYPE_NOSNIFF = False +SECURE_BROWSER_XSS_FILTER = False +SESSION_COOKIE_SECURE = False +CSRF_COOKIE_SECURE = False +CSRF_COOKIE_HTTPONLY = False +X_FRAME_OPTIONS = 'DENY' +SESSION_COOKIE_AGE = 60 * 60 * 3 diff --git a/tombola/urls.py b/tombola/urls.py new file mode 100644 index 0000000..fd43e52 --- /dev/null +++ b/tombola/urls.py @@ -0,0 +1,21 @@ +"""tombola URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/tombola/wsgi.py b/tombola/wsgi.py new file mode 100644 index 0000000..3fc7cb9 --- /dev/null +++ b/tombola/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for tombola 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/3.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tombola.settings') + +application = get_wsgi_application()