This commit is contained in:
godd0t
2023-05-10 09:02:59 +02:00
commit 8ff2ae26e7
38 changed files with 975 additions and 0 deletions

22
src/manage.py Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.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()

View File

@@ -0,0 +1,5 @@
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)

16
src/project_name/asgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
ASGI config for project_name 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/4.2/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
application = get_asgi_application()

View File

@@ -0,0 +1,22 @@
import os
from celery import Celery
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.dev')
app = Celery('project_name')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django apps.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')

View File

View File

@@ -0,0 +1,144 @@
from os import getenv as os_getenv, path as os_path # noqa
from pathlib import Path
from django.core.management.utils import get_random_secret_key
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os_getenv("APP_SECRET_KEY", get_random_secret_key()) # If SECRET_KEY is not set, generate a random one
APP_ENV = os_getenv("APP_ENV", "dev")
DEBUG = os_getenv("DEBUG", "true").lower() in ["True", "true", "1", "yes", "y"]
ALLOWED_HOSTS = os_getenv("ALLOWED_HOSTS", "localhost").split(",")
CSRF_TRUSTED_ORIGINS = os_getenv("CSRF_TRUSTED_ORIGINS", "http://localhost").split(",")
if DEBUG:
CORS_ORIGIN_ALLOW_ALL = True
else:
CORS_ALLOWED_ORIGINS = os_getenv("CORS_ALLOWED_ORIGINS", "http://localhost").split(",")
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"corsheaders",
"test_app",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
"corsheaders.middleware.CorsMiddleware", # CorsMiddleware should be placed as high as possible,
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project_name.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'project_name.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
"ENGINE": "django.db.backends.postgresql",
'NAME': os_getenv("POSTGRES_DB", "postgres"),
"USER": os_getenv("POSTGRES_USER", "postgres"),
"PASSWORD": os_getenv("POSTGRES_PASSWORD", "postgres"),
"HOST": os_getenv("POSTGRES_HOST", "db"),
"PORT": os_getenv("POSTGRES_PORT", "5432"),
}
}
# Password validation
# https://docs.djangoproject.com/en/4.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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.2/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/4.2/howto/static-files/
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Redis
REDIS_DB_KEYS = {
"dev": 0,
"test": 1,
"prod": 2,
}
# Redis settings
REDIS_HOST = os_getenv("REDIS_HOST", "redis")
REDIS_PORT = os_getenv("REDIS_PORT", 6379)
REDIS_DB = REDIS_DB_KEYS.get(APP_ENV, 0)
REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}"
# Celery settings
CELERY_BROKER_URL = REDIS_URL
CELERY_RESULT_BACKEND = REDIS_URL

View File

@@ -0,0 +1 @@
from .base import * # noqa

View File

@@ -0,0 +1 @@
from .base import * # noqa

View File

@@ -0,0 +1 @@
from .base import * # noqa

14
src/project_name/urls.py Normal file
View File

@@ -0,0 +1,14 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("test_app/", include("test_app.urls")),
]
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

16
src/project_name/wsgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
WSGI config for project_name 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/4.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.dev')
application = get_wsgi_application()

21
src/requirements.txt Normal file
View File

@@ -0,0 +1,21 @@
amqp==5.1.1
asgiref==3.6.0
billiard==3.6.4.0
celery==5.2.7
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
Django==4.2.1
django-cors-headers==3.14.0
gunicorn==20.1.0
kombu==5.2.4
prompt-toolkit==3.0.38
psycopg==3.1.9
pytz==2023.3
six==1.16.0
sqlparse==0.4.4
typing_extensions==4.5.0
vine==5.0.0
wcwidth==0.2.6
redis==4.5.5

0
src/test_app/__init__.py Normal file
View File

3
src/test_app/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin # noqa
# Register your models here.

6
src/test_app/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class TestAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'test_app'

View File

3
src/test_app/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models # noqa
# Create your models here.

3
src/test_app/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase # noqa
# Create your tests here.

5
src/test_app/urls.py Normal file
View File

@@ -0,0 +1,5 @@
from django.urls import path # noqa
urlpatterns = [
# ... other urls
]

3
src/test_app/views.py Normal file
View File

@@ -0,0 +1,3 @@
from django.shortcuts import render # noqa
# Create your views here.

0
src/tests/__init__.py Normal file
View File

19
src/tests/conftest.py Normal file
View File

@@ -0,0 +1,19 @@
import pytest
from django.contrib.auth.models import User
@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
"""
This fixture enables database access for all tests.
"""
pass
@pytest.fixture
def test_user():
return User.objects.create_user(
username='test_user',
email="test_user@test.com",
password="test_password"
)

View File

View File

@@ -0,0 +1,5 @@
def test_example(test_user):
assert test_user.username == 'test_user'
assert test_user.email is not None