From e51406e31afee3c285c9a378d977a2035aed2e5a Mon Sep 17 00:00:00 2001 From: jnfire Date: Fri, 29 Jul 2022 10:59:09 +0200 Subject: [PATCH 01/10] add env and clean old apps add env and clean old apps --- Caddyfile | 2 +- Dockerfiles/django/Dockerfile | 1 + app/website/admin.py | 3 -- app/website/apps.py | 6 --- app/website/migrations/__init__.py | 0 app/website/models.py | 3 -- app/website/templates/home.html | 1 - app/website/tests.py | 3 -- app/website/views.py | 5 -- ccstech/__init__.py | 0 ccstech/wsgi.py | 16 ------ {app/website => core}/__init__.py | 0 {ccstech => core}/asgi.py | 0 {ccstech => core}/settings.py | 4 +- {ccstech => core}/urls.py | 0 django-launcher.sh | 6 +-- docker-compose.dev.yaml | 82 ++++++++++++++++++++++++++++++ docker-compose.pro.yaml | 74 +++++++++++++++++++++++++++ docker-compose.yaml | 75 --------------------------- requirements.txt | 28 +++++----- 20 files changed, 177 insertions(+), 132 deletions(-) delete mode 100644 app/website/admin.py delete mode 100644 app/website/apps.py delete mode 100644 app/website/migrations/__init__.py delete mode 100644 app/website/models.py delete mode 100644 app/website/templates/home.html delete mode 100644 app/website/tests.py delete mode 100644 app/website/views.py delete mode 100644 ccstech/__init__.py delete mode 100644 ccstech/wsgi.py rename {app/website => core}/__init__.py (100%) rename {ccstech => core}/asgi.py (100%) rename {ccstech => core}/settings.py (98%) rename {ccstech => core}/urls.py (100%) create mode 100644 docker-compose.dev.yaml create mode 100644 docker-compose.pro.yaml delete mode 100644 docker-compose.yaml diff --git a/Caddyfile b/Caddyfile index 51e7b18..d883b6d 100644 --- a/Caddyfile +++ b/Caddyfile @@ -1,4 +1,4 @@ -http://ccstech.localhost +{$DOMAIN_URL} root * /usr/src/app/ diff --git a/Dockerfiles/django/Dockerfile b/Dockerfiles/django/Dockerfile index c19e5f2..1c298c5 100644 --- a/Dockerfiles/django/Dockerfile +++ b/Dockerfiles/django/Dockerfile @@ -19,6 +19,7 @@ RUN apt install -y build-essential python3-dev libpq-dev python3-pip gettext # install dependencies RUN pip3 install --upgrade pip +# install basic dependencies COPY ./requirements.txt . RUN pip3 install -r requirements.txt diff --git a/app/website/admin.py b/app/website/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/app/website/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/app/website/apps.py b/app/website/apps.py deleted file mode 100644 index b8fd484..0000000 --- a/app/website/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class WebsiteConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'app.website' diff --git a/app/website/migrations/__init__.py b/app/website/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/app/website/models.py b/app/website/models.py deleted file mode 100644 index 71a8362..0000000 --- a/app/website/models.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.db import models - -# Create your models here. diff --git a/app/website/templates/home.html b/app/website/templates/home.html deleted file mode 100644 index 15f747a..0000000 --- a/app/website/templates/home.html +++ /dev/null @@ -1 +0,0 @@ -

Hi

\ No newline at end of file diff --git a/app/website/tests.py b/app/website/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/app/website/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/app/website/views.py b/app/website/views.py deleted file mode 100644 index 35fb3dc..0000000 --- a/app/website/views.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.shortcuts import render - -# Create your views here. -def home(request): - return render(request, 'home.html') \ No newline at end of file diff --git a/ccstech/__init__.py b/ccstech/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/ccstech/wsgi.py b/ccstech/wsgi.py deleted file mode 100644 index 08b6afb..0000000 --- a/ccstech/wsgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -WSGI config for ccstech 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.0/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ccstech.settings') - -application = get_wsgi_application() diff --git a/app/website/__init__.py b/core/__init__.py similarity index 100% rename from app/website/__init__.py rename to core/__init__.py diff --git a/ccstech/asgi.py b/core/asgi.py similarity index 100% rename from ccstech/asgi.py rename to core/asgi.py diff --git a/ccstech/settings.py b/core/settings.py similarity index 98% rename from ccstech/settings.py rename to core/settings.py index aedd0ff..2e710aa 100644 --- a/ccstech/settings.py +++ b/core/settings.py @@ -42,7 +42,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'django_extensions', - 'app.website', + 'rest_framework', ] MIDDLEWARE = [ @@ -55,7 +55,7 @@ MIDDLEWARE = [ 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] -ROOT_URLCONF = 'ccstech.urls' +ROOT_URLCONF = 'core.urls' TEMPLATES = [ { diff --git a/ccstech/urls.py b/core/urls.py similarity index 100% rename from ccstech/urls.py rename to core/urls.py diff --git a/django-launcher.sh b/django-launcher.sh index 990988f..7eb6bec 100644 --- a/django-launcher.sh +++ b/django-launcher.sh @@ -12,6 +12,6 @@ python3 manage.py migrate # Start server echo "Starting server" ## With WebSockets -uvicorn --host 0.0.0.0 --port 8000 --reload ccstech.asgi:application -## without WebSockets -#gunicorn --workers=4 -b 0.0.0.0:8000 --reload ccstech.wsgi:application +python3 manage.py runserver 0.0.0.0:8000 +#echo "*****Start server with production mode*****" +#daphne -b 0.0.0.0 -p 8000 core.asgi:application diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml new file mode 100644 index 0000000..47b5b23 --- /dev/null +++ b/docker-compose.dev.yaml @@ -0,0 +1,82 @@ +version: '3.8' + +services: + + postgresql: + image: postgres + container_name: ${PROJECT_NAME}-postgresql + restart: "no" + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_NAME} + ports: + - ${DB_PORT}:5432 + + django: + build: + context: ./ + dockerfile: ./Dockerfiles/django/Dockerfile + container_name: ${PROJECT_NAME}-django + restart: "no" + entrypoint: /django-launcher.sh + volumes: + - .:/usr/src/app/ + environment: + DEBUG: "False" + ALLOWED_HOSTS: ${DOMAIN} + SECRET_KEY: ${DJANGO_SECRET_KEY} + DB_ENGINE: django.db.backends.postgresql + DB_NAME: ${DB_NAME} + DB_USER: ${DB_USER} + DB_PASSWORD: ${DB_PASSWORD} + DB_HOST: ${DB_HOST} + DB_PORT: ${DB_PORT} + DOMAIN: ${DOMAIN} + DOMAIN_URL: ${DOMAIN_URL} + STATIC_URL: /static/ + STATIC_ROOT: static + MEDIA_URL: /media/ + REDIS_HOST: ${REDIS_HOST} + REDIS_PORT: ${REDIS_PORT} + EMAIL_HOST: ${EMAIL_HOST} + EMAIL_USE_TLS: ${EMAIL_USE_TLS} + EMAIL_USE_SSL: ${EMAIL_USE_SSL} + EMAIL_PORT: ${EMAIL_PORT} + EMAIL_USER: ${EMAIL_USER} + EMAIL_PASSWORD: ${EMAIL_PASSWORD} + expose: + - 8000 + depends_on: + - postgresql + links: + - redis + + caddy: + image: caddy:alpine + container_name: ${PROJECT_NAME}-caddy + restart: "no" + ports: + - ${CADDY_PORT_ONE}:80 + - ${CADDY_PORT_TWO}:443 + volumes: + - ./Caddyfile:/etc/caddy/Caddyfile + - ./caddy_data:/data + - .:/usr/src/app/ + depends_on: + - django + + redis: + image: redis:alpine + container_name: ${PROJECT_NAME}-redis + restart: "no" + expose: + - ${REDIS_PORT} + + mailhog: + image: mailhog/mailhog:latest + restart: "no" + expose: + - 1025 + ports: + - ${MAILHOG_PORT}:8025 diff --git a/docker-compose.pro.yaml b/docker-compose.pro.yaml new file mode 100644 index 0000000..596024a --- /dev/null +++ b/docker-compose.pro.yaml @@ -0,0 +1,74 @@ +version: '3.8' + +services: + + postgresql: + image: postgres + container_name: ${PROJECT_NAME}-postgresql + restart: always + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_NAME} + ports: + - ${DB_PORT}:5432 + + django: + build: + context: ./ + dockerfile: ./Dockerfiles/django/Dockerfile + container_name: ${PROJECT_NAME}-django + restart: always + entrypoint: /django-launcher.sh + volumes: + - .:/usr/src/app/ + environment: + DEBUG: "False" + ALLOWED_HOSTS: ${DOMAIN} + SECRET_KEY: ${DJANGO_SECRET_KEY} + DB_ENGINE: django.db.backends.postgresql + DB_NAME: ${DB_NAME} + DB_USER: ${DB_USER} + DB_PASSWORD: ${DB_PASSWORD} + DB_HOST: ${DB_HOST} + DB_PORT: ${DB_PORT} + DOMAIN: ${DOMAIN} + DOMAIN_URL: ${DOMAIN_URL} + STATIC_URL: /static/ + STATIC_ROOT: static + MEDIA_URL: /media/ + REDIS_HOST: ${REDIS_HOST} + REDIS_PORT: ${REDIS_PORT} + EMAIL_HOST: ${EMAIL_HOST} + EMAIL_USE_TLS: ${EMAIL_USE_TLS} + EMAIL_USE_SSL: ${EMAIL_USE_SSL} + EMAIL_PORT: ${EMAIL_PORT} + EMAIL_USER: ${EMAIL_USER} + EMAIL_PASSWORD: ${EMAIL_PASSWORD} + expose: + - 8000 + depends_on: + - postgresql + links: + - redis + + caddy: + image: caddy:alpine + container_name: ${PROJECT_NAME}-caddy + restart: always + ports: + - ${CADDY_PORT_ONE}:80 + - ${CADDY_PORT_TWO}:443 + volumes: + - ./Caddyfile:/etc/caddy/Caddyfile + - ./caddy_data:/data + - .:/usr/src/app/ + depends_on: + - django + + redis: + image: redis:alpine + container_name: ${PROJECT_NAME}-redis + restart: always + expose: + - ${REDIS_PORT} diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 219cb24..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,75 +0,0 @@ -version: '3.8' - -services: - - postgresql: - image: postgres - restart: "no" - environment: - POSTGRES_USER: "postgres" - POSTGRES_PASSWORD: "postgres" - POSTGRES_DB: "ccstech" - ports: - - 5432:5432 - - django: - build: - context: ./ - dockerfile: ./Dockerfiles/django/Dockerfile - restart: "no" - entrypoint: /django-launcher.sh - volumes: - - .:/usr/src/app/ - environment: - DEBUG: "True" - ALLOWED_HOSTS: "ccstech.localhost" - SECRET_KEY: "misecreto" - DB_ENGINE: "django.db.backends.postgresql" - DB_NAME: "ccstech" - DB_USER: "postgres" - DB_PASSWORD: "postgres" - DB_HOST: "postgresql" - DB_PORT: "5432" - DOMAIN: "ccstech.localhost" - DOMAIN_URL: "http://ccstech.localhost" - STATIC_URL: "/static/" - STATIC_ROOT: "static" - MEDIA_URL: "/media/" - REDIS_HOST: "redis" - REDIS_PORT: "6379" - EMAIL_HOST: "mailhog" - EMAIL_USE_TLS: "False" - EMAIL_PORT: "1025" - EMAIL_USER: "" - EMAIL_PASSWORD: "" - expose: - - 8000 - depends_on: - - postgresql - - caddy: - image: caddy:alpine - restart: "no" - ports: - - 80:80 - - 443:443 - volumes: - - ./Caddyfile:/etc/caddy/Caddyfile - - ./caddy_data:/data - - .:/usr/src/app/ - depends_on: - - django - - redis: - image: redis:alpine - restart: "no" - expose: - - 6379 - - mailhog: - image: mailhog/mailhog:latest - restart: "no" - expose: - - 1025 - ports: - - 8025:8025 diff --git a/requirements.txt b/requirements.txt index 2548d31..406a540 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,18 @@ # Django -django===4.0.5 -django-extensions===3.1.5 +django===4.0.6 +django-extensions===3.2.0 # PostgreSQL driver psycopg2-binary===2.9.3 -# Servidor para Django sin Websockets -gunicorn===20.1.0 -# Servidor para Django con Websockets -uvicorn===0.18.1 -websockets===10.3 -# Channels -channels==3.0.5 +# Check connection +redis==4.3.4 +# Django Server +daphne===3.0.2 asgiref===3.5.2 -# Conector de Redis para Channels -channels_redis===3.4.0 -# Template -# Pillow -Pillow===9.1.1 \ No newline at end of file + +# Templates +## Image processing +Pillow===9.2.0 + +# Testing +pytest==7.1.2 +pytest-django==4.5.2 From 128271411d94b6b70414493608a2ad8b1cf14ffa Mon Sep 17 00:00:00 2001 From: jnfire Date: Fri, 29 Jul 2022 15:15:22 +0200 Subject: [PATCH 02/10] fix names fix names --- Makefile | 23 +++++++++--------- README.md | 54 +++++++++++++++++++++++++++++++++++------ core/asgi.py | 4 +-- core/settings.py | 10 ++------ core/urls.py | 4 +-- docker-compose.dev.yaml | 1 + manage.py | 2 +- requirements.txt | 13 ++++++++++ 8 files changed, 79 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index b02098a..6646f18 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,9 @@ format: ## Format style with black black --exclude="/(postgres|venv|migrations|\.git)/" . docker.recreate.django: ## Recreate Django image - docker-compose -f docker-compose.yaml build --no-cache --force-rm django - docker-compose -f docker-compose.yaml up --force-recreate --no-deps -d django - make run.loaddata + docker-compose -f docker-compose.dev.yaml build --no-cache --force-rm django + docker-compose -f docker-compose.dev.yaml up --force-recreate --no-deps -d django + ## make run.loaddata run.makemigrations: ## Makemigrations docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py makemigrations" @@ -21,18 +21,19 @@ run.migrate: ## Migrate run.loaddata: ## Load initial data # Remove database - rm -rf database.sqlite + docker-compose exec -T django bash -c "python3 manage.py flush --noinput" # Remove media - rm -rf media + sudo rm -rf media/categories/* # Migrate - docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py migrate" + docker-compose exec -T django bash -c "python3 manage.py migrate" run.loaddata.test: ## Load initial data test make run.loaddata # Add superuser: alias "admin" - password "admin" - docker-compose -f docker-compose.yaml exec -T django bash -c "cat data/create_superuser.py | python3 manage.py shell" - # Add more users: alias random - password "password" - docker-compose -f docker-compose.yaml exec -T django bash -c "cat data/create_users.py | python3 manage.py shell" + docker-compose -f docker-compose.dev.yaml exec -T django bash -c "cat data/create_superuser.py | python3 manage.py shell" -run.server: ## Run server - docker-compose -f docker-compose.yaml up +run.server.dev: ## Run server + docker-compose -f docker-compose.dev.yaml up + +run.server.pro: ## Run server + docker-compose -f docker-compose.pro.yaml up diff --git a/README.md b/README.md index 11f7637..df07dd6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ make run.server Now open: -`http://ccstech.localhost` +`http://template.localhost` ## Gulp @@ -68,15 +68,15 @@ make run.loaddata.test ## Other domains -- Caddy: `http://ccstech.localhost`. -- Gulp: `http://ccstech.localhost:3000`. -- Django: `http://ccstech.localhost:8000`. -- Mailhog: `http://ccstech.localhost:8025`. +- Caddy: `http://template.localhost`. +- Gulp: `http://template.localhost:3000`. +- Django: `http://template.localhost:8000`. +- Mailhog: `http://template.localhost:8025`. ### Bash Django ``` shell -docker exec -it ccstech_django_1 bash +docker exec -it template-django bash ``` # Run production @@ -85,4 +85,44 @@ docker exec -it ccstech_django_1 bash docker-compose -f docker-compose.pro.yaml up ``` -Open `https://ccstech.io`. +Open `https://template.io`. + +# Enviroment (.env) +```text +PROJECT_NAME=template + +# Domain +DOMAIN=template.localhost +DOMAIN_URL=http://template.localhost + +# Database +DB_NAME=template_db +DB_USER=postgres +DB_PASSWORD=postgres +DB_HOST=postgresql +DB_PORT=5432 + +# Django options +DJANGO_SECRET_KEY=mysecret + +# Redis +REDIS_HOST=redis +REDIS_PORT=6379 + +# Caddy +CADDY_PORT_ONE=80 +CADDY_PORT_TWO=443 + +# Email +DEFAULT_FROM_EMAIL=no-reply@template.localhost +EMAIL_CONTACT=info@template.localhost +EMAIL_HOST=mailhog +EMAIL_USER= +EMAIL_PASSWORD= +EMAIL_PORT=1025 +EMAIL_USE_TLS=False +EMAIL_USE_SSL=False + +# Mailhog +MAILHOG_PORT=8025 +``` \ No newline at end of file diff --git a/core/asgi.py b/core/asgi.py index a11ad89..04e6d00 100644 --- a/core/asgi.py +++ b/core/asgi.py @@ -1,5 +1,5 @@ """ -ASGI config for ccstech project. +ASGI config for core project. It exposes the ASGI callable as a module-level variable named ``application``. @@ -11,6 +11,6 @@ import os from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ccstech.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') application = get_asgi_application() diff --git a/core/settings.py b/core/settings.py index 2e710aa..a747e7a 100644 --- a/core/settings.py +++ b/core/settings.py @@ -10,6 +10,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.2/ref/settings/ """ import os +import dj_database_url from pathlib import Path from django.db.backends.signals import connection_created @@ -78,14 +79,7 @@ TEMPLATES = [ # https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { - "default": { - "ENGINE": os.environ.get("DB_ENGINE"), - "NAME": os.environ.get("DB_NAME"), - "USER": os.environ.get("DB_USER"), - "PASSWORD": os.environ.get("DB_PASSWORD"), - "HOST": os.environ.get("DB_HOST"), - "PORT": os.environ.get("DB_PORT"), - } + "default": dj_database_url.config(default=f"postgres://{os.environ.get('DB_USER')}:{os.environ.get('DB_PASSWORD')}@{os.environ.get('DB_HOST')}:{os.environ.get('DB_PORT')}/{os.environ.get('DB_NAME')}") } # Password validation diff --git a/core/urls.py b/core/urls.py index e594313..3141348 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,4 +1,4 @@ -"""ccstech URL Configuration +"""template URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/4.0/topics/http/urls/ @@ -15,9 +15,7 @@ Including another URLconf """ from django.contrib import admin from django.urls import path -from app.website import views as website_views urlpatterns = [ - path('', website_views.home, name='home'), path('admin/', admin.site.urls), ] diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index 47b5b23..d0dd138 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -75,6 +75,7 @@ services: mailhog: image: mailhog/mailhog:latest + container_name: ${PROJECT_NAME}-mailhog restart: "no" expose: - 1025 diff --git a/manage.py b/manage.py index 393e21e..f2a662c 100755 --- a/manage.py +++ b/manage.py @@ -6,7 +6,7 @@ import sys def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ccstech.settings') + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/requirements.txt b/requirements.txt index 406a540..c4bdc29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,18 @@ # Django django===4.0.6 django-extensions===3.2.0 +dj-database-url==0.5.0 + +# Django REST +djangorestframework==3.13.1 +django-cors-headers==3.10.0 + # PostgreSQL driver psycopg2-binary===2.9.3 + # Check connection redis==4.3.4 + # Django Server daphne===3.0.2 asgiref===3.5.2 @@ -16,3 +24,8 @@ Pillow===9.2.0 # Testing pytest==7.1.2 pytest-django==4.5.2 + +# Quality code +black==22.6.0 +flake8==4.0.1 +isort==5.10.1 \ No newline at end of file From 86ee2172d389cbf0ab219847bd2c78a60b686be7 Mon Sep 17 00:00:00 2001 From: jnfire Date: Mon, 1 Aug 2022 15:52:14 +0200 Subject: [PATCH 03/10] fix variables fix variables --- Caddyfile.dev | 18 ++++++++++++++++++ Caddyfile => Caddyfile.pro | 2 +- Makefile | 8 ++++---- README.md | 24 ++++++++++++------------ docker-compose.dev.yaml | 3 +-- docker-compose.pro.yaml | 3 +-- scripts/__ini__.py | 0 test/__ini__.py | 0 test/test_start.py | 4 ++++ 9 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 Caddyfile.dev rename Caddyfile => Caddyfile.pro (85%) create mode 100644 scripts/__ini__.py create mode 100644 test/__ini__.py create mode 100644 test/test_start.py diff --git a/Caddyfile.dev b/Caddyfile.dev new file mode 100644 index 0000000..8d9c458 --- /dev/null +++ b/Caddyfile.dev @@ -0,0 +1,18 @@ +http://here.project.localhost { + + root * /usr/src/app/ + encode gzip zstd + + @notStatic { + not path /static/* /media/* + } + + reverse_proxy @notStatic django:8000 + + file_server /static/* + file_server /media/* +} + +http://webmail.localhost { + reverse_proxy mailhog:8025 +} diff --git a/Caddyfile b/Caddyfile.pro similarity index 85% rename from Caddyfile rename to Caddyfile.pro index d883b6d..5122434 100644 --- a/Caddyfile +++ b/Caddyfile.pro @@ -1,4 +1,4 @@ -{$DOMAIN_URL} +https://here.project root * /usr/src/app/ diff --git a/Makefile b/Makefile index 6646f18..befa457 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,11 @@ help: @perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}' -lint: ## Check style with black - black --check --exclude="/(postgres|venv|migrations|\.git)/" . - format: ## Format style with black - black --exclude="/(postgres|venv|migrations|\.git)/" . + black --exclude="/(postgres_data|venv|migrations|\.git)/" core/ apps/ scripts/ tests/ + +test: ## Tests + docker-compose -f docker-compose.dev.yaml exec -T django bash -c "pytest" docker.recreate.django: ## Recreate Django image docker-compose -f docker-compose.dev.yaml build --no-cache --force-rm django diff --git a/README.md b/README.md index df07dd6..e40453a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ make run.server Now open: -`http://template.localhost` +`http://here.project.localhost` ## Gulp @@ -68,15 +68,15 @@ make run.loaddata.test ## Other domains -- Caddy: `http://template.localhost`. -- Gulp: `http://template.localhost:3000`. -- Django: `http://template.localhost:8000`. -- Mailhog: `http://template.localhost:8025`. +- Caddy: `http://here.project.localhost`. +- Gulp: `http://here.project.localhost:3000`. +- Django: `http://here.project.localhost:8000`. +- Mailhog: `http://here.project.localhost:8025`. ### Bash Django ``` shell -docker exec -it template-django bash +docker exec -it here.project-django bash ``` # Run production @@ -89,14 +89,14 @@ Open `https://template.io`. # Enviroment (.env) ```text -PROJECT_NAME=template +PROJECT_NAME=here.project # Domain -DOMAIN=template.localhost -DOMAIN_URL=http://template.localhost +DOMAIN=here.project.localhost +DOMAIN_URL=http://here.project.localhost # Database -DB_NAME=template_db +DB_NAME=project_db DB_USER=postgres DB_PASSWORD=postgres DB_HOST=postgresql @@ -114,8 +114,8 @@ CADDY_PORT_ONE=80 CADDY_PORT_TWO=443 # Email -DEFAULT_FROM_EMAIL=no-reply@template.localhost -EMAIL_CONTACT=info@template.localhost +DEFAULT_FROM_EMAIL=no-reply@here.project.localhost +EMAIL_CONTACT=info@here.project.localhost EMAIL_HOST=mailhog EMAIL_USER= EMAIL_PASSWORD= diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index d0dd138..66c4fe9 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -26,7 +26,6 @@ services: DEBUG: "False" ALLOWED_HOSTS: ${DOMAIN} SECRET_KEY: ${DJANGO_SECRET_KEY} - DB_ENGINE: django.db.backends.postgresql DB_NAME: ${DB_NAME} DB_USER: ${DB_USER} DB_PASSWORD: ${DB_PASSWORD} @@ -60,7 +59,7 @@ services: - ${CADDY_PORT_ONE}:80 - ${CADDY_PORT_TWO}:443 volumes: - - ./Caddyfile:/etc/caddy/Caddyfile + - ./Caddyfile.dev:/etc/caddy/Caddyfile - ./caddy_data:/data - .:/usr/src/app/ depends_on: diff --git a/docker-compose.pro.yaml b/docker-compose.pro.yaml index 596024a..0bfe700 100644 --- a/docker-compose.pro.yaml +++ b/docker-compose.pro.yaml @@ -26,7 +26,6 @@ services: DEBUG: "False" ALLOWED_HOSTS: ${DOMAIN} SECRET_KEY: ${DJANGO_SECRET_KEY} - DB_ENGINE: django.db.backends.postgresql DB_NAME: ${DB_NAME} DB_USER: ${DB_USER} DB_PASSWORD: ${DB_PASSWORD} @@ -60,7 +59,7 @@ services: - ${CADDY_PORT_ONE}:80 - ${CADDY_PORT_TWO}:443 volumes: - - ./Caddyfile:/etc/caddy/Caddyfile + - ./Caddyfile.pro:/etc/caddy/Caddyfile - ./caddy_data:/data - .:/usr/src/app/ depends_on: diff --git a/scripts/__ini__.py b/scripts/__ini__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/__ini__.py b/test/__ini__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_start.py b/test/test_start.py new file mode 100644 index 0000000..6c9f592 --- /dev/null +++ b/test/test_start.py @@ -0,0 +1,4 @@ + +def test_hello_world(): + assert "hello_world" == "hello_world" + assert "foo" != "bar" \ No newline at end of file From edef594e5952da9d0c34af61a01fb05f5bdab1b6 Mon Sep 17 00:00:00 2001 From: jnfire Date: Mon, 1 Aug 2022 16:58:51 +0200 Subject: [PATCH 04/10] fix test and dockerfile of django fix test and dockerfile of django --- Dockerfiles/django/Dockerfile | 2 +- pytest.ini | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 pytest.ini diff --git a/Dockerfiles/django/Dockerfile b/Dockerfiles/django/Dockerfile index 1c298c5..d79b72e 100644 --- a/Dockerfiles/django/Dockerfile +++ b/Dockerfiles/django/Dockerfile @@ -1,4 +1,4 @@ -FROM debian:11 +FROM python:3.10-slim # Prevents Python from writing pyc files to disc (equivalent to python -B option) ENV PYTHONDONTWRITEBYTECODE 1 diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..1bc85b2 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +DJANGO_SETTINGS_MODULE = core.settings + +# -- recommended but optional: +python_files = test_*.py \ No newline at end of file From fc650af1b73ea826bd618a89be54bae718459a9b Mon Sep 17 00:00:00 2001 From: jnfire Date: Mon, 1 Aug 2022 17:02:57 +0200 Subject: [PATCH 05/10] update gitignore update gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7fda18d..854f719 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env .idea/* static/admin/ -static/django_extensions/ \ No newline at end of file +static/django_extensions/ +static/rest_framework/ \ No newline at end of file From ae2758a44f5c2711398fdf866b13fe678c9622bd Mon Sep 17 00:00:00 2001 From: jnfire Date: Tue, 2 Aug 2022 08:56:21 +0200 Subject: [PATCH 06/10] update code and fix errors update code and fix errors --- Caddyfile.dev | 2 +- Caddyfile.pro | 2 +- Dockerfiles/django/Dockerfile | 1 - Makefile | 2 +- README.md | 25 ++++---- test/__ini__.py => apps/website/__init__.py | 0 apps/website/admin.py | 3 + apps/website/apps.py | 6 ++ apps/website/migrations/__init__.py | 0 apps/website/models.py | 3 + apps/website/templates/home.html | 1 + apps/website/views.py | 5 ++ core/asgi.py | 2 +- core/settings.py | 71 +++++++++++---------- core/urls.py | 3 +- docker-compose.dev.yaml | 2 - tests/__ini__.py | 0 {test => tests}/test_start.py | 3 +- 18 files changed, 72 insertions(+), 59 deletions(-) rename test/__ini__.py => apps/website/__init__.py (100%) create mode 100644 apps/website/admin.py create mode 100644 apps/website/apps.py create mode 100644 apps/website/migrations/__init__.py create mode 100644 apps/website/models.py create mode 100644 apps/website/templates/home.html create mode 100644 apps/website/views.py create mode 100644 tests/__ini__.py rename {test => tests}/test_start.py (71%) diff --git a/Caddyfile.dev b/Caddyfile.dev index 8d9c458..488aff7 100644 --- a/Caddyfile.dev +++ b/Caddyfile.dev @@ -1,4 +1,4 @@ -http://here.project.localhost { +http://project.localhost { root * /usr/src/app/ encode gzip zstd diff --git a/Caddyfile.pro b/Caddyfile.pro index 5122434..3ea662e 100644 --- a/Caddyfile.pro +++ b/Caddyfile.pro @@ -1,4 +1,4 @@ -https://here.project +https://project.com root * /usr/src/app/ diff --git a/Dockerfiles/django/Dockerfile b/Dockerfiles/django/Dockerfile index d79b72e..0a5ff5c 100644 --- a/Dockerfiles/django/Dockerfile +++ b/Dockerfiles/django/Dockerfile @@ -19,7 +19,6 @@ RUN apt install -y build-essential python3-dev libpq-dev python3-pip gettext # install dependencies RUN pip3 install --upgrade pip -# install basic dependencies COPY ./requirements.txt . RUN pip3 install -r requirements.txt diff --git a/Makefile b/Makefile index befa457..76b26f5 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ run.loaddata: ## Load initial data # Remove database docker-compose exec -T django bash -c "python3 manage.py flush --noinput" # Remove media - sudo rm -rf media/categories/* + rm -rf media # Migrate docker-compose exec -T django bash -c "python3 manage.py migrate" diff --git a/README.md b/README.md index e40453a..d6e599a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ make run.server Now open: -`http://here.project.localhost` +`http://project.localhost` ## Gulp @@ -68,15 +68,15 @@ make run.loaddata.test ## Other domains -- Caddy: `http://here.project.localhost`. -- Gulp: `http://here.project.localhost:3000`. -- Django: `http://here.project.localhost:8000`. -- Mailhog: `http://here.project.localhost:8025`. +- Caddy: `http://project.localhost`. +- Gulp: `http://project.localhost:3000`. +- Django: `http://project.localhost:8000`. +- Mailhog: `http://project.localhost:8025`. ### Bash Django ``` shell -docker exec -it here.project-django bash +docker exec -it project-django bash ``` # Run production @@ -85,15 +85,15 @@ docker exec -it here.project-django bash docker-compose -f docker-compose.pro.yaml up ``` -Open `https://template.io`. +Open `https://proyect.com`. # Enviroment (.env) ```text PROJECT_NAME=here.project # Domain -DOMAIN=here.project.localhost -DOMAIN_URL=http://here.project.localhost +DOMAIN=project.localhost +DOMAIN_URL=http://project.localhost # Database DB_NAME=project_db @@ -114,15 +114,12 @@ CADDY_PORT_ONE=80 CADDY_PORT_TWO=443 # Email -DEFAULT_FROM_EMAIL=no-reply@here.project.localhost -EMAIL_CONTACT=info@here.project.localhost +DEFAULT_FROM_EMAIL=no-reply@project.localhost +EMAIL_CONTACT=info@project.localhost EMAIL_HOST=mailhog EMAIL_USER= EMAIL_PASSWORD= EMAIL_PORT=1025 EMAIL_USE_TLS=False EMAIL_USE_SSL=False - -# Mailhog -MAILHOG_PORT=8025 ``` \ No newline at end of file diff --git a/test/__ini__.py b/apps/website/__init__.py similarity index 100% rename from test/__ini__.py rename to apps/website/__init__.py diff --git a/apps/website/admin.py b/apps/website/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/apps/website/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/apps/website/apps.py b/apps/website/apps.py new file mode 100644 index 0000000..f1d93d1 --- /dev/null +++ b/apps/website/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class WebsiteConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "app.website" diff --git a/apps/website/migrations/__init__.py b/apps/website/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/website/models.py b/apps/website/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/apps/website/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/apps/website/templates/home.html b/apps/website/templates/home.html new file mode 100644 index 0000000..15f747a --- /dev/null +++ b/apps/website/templates/home.html @@ -0,0 +1 @@ +

Hi

\ No newline at end of file diff --git a/apps/website/views.py b/apps/website/views.py new file mode 100644 index 0000000..7061bb6 --- /dev/null +++ b/apps/website/views.py @@ -0,0 +1,5 @@ +from django.shortcuts import render + +# Create your views here. +def home(request): + return render(request, "home.html") diff --git a/core/asgi.py b/core/asgi.py index 04e6d00..4222035 100644 --- a/core/asgi.py +++ b/core/asgi.py @@ -11,6 +11,6 @@ import os from django.core.asgi import get_asgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") application = get_asgi_application() diff --git a/core/settings.py b/core/settings.py index a747e7a..9e19490 100644 --- a/core/settings.py +++ b/core/settings.py @@ -36,39 +36,40 @@ if not os.environ.get("ALLOWED_HOSTS") == None: # Application definition INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'django_extensions', - 'rest_framework', + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "django_extensions", + "rest_framework", + "apps.website", ] 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.middleware.clickjacking.XFrameOptionsMiddleware', + "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.middleware.clickjacking.XFrameOptionsMiddleware", ] -ROOT_URLCONF = 'core.urls' +ROOT_URLCONF = "core.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(BASE_DIR, "app", "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', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [os.path.join(BASE_DIR, "app", "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", ], }, }, @@ -79,7 +80,9 @@ TEMPLATES = [ # https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { - "default": dj_database_url.config(default=f"postgres://{os.environ.get('DB_USER')}:{os.environ.get('DB_PASSWORD')}@{os.environ.get('DB_HOST')}:{os.environ.get('DB_PORT')}/{os.environ.get('DB_NAME')}") + "default": dj_database_url.config( + default=f"postgres://{os.environ.get('DB_USER')}:{os.environ.get('DB_PASSWORD')}@{os.environ.get('DB_HOST')}:{os.environ.get('DB_PORT')}/{os.environ.get('DB_NAME')}" + ) } # Password validation @@ -87,16 +90,16 @@ DATABASES = { AUTH_PASSWORD_VALIDATORS = [ { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", }, ] @@ -104,9 +107,9 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/3.2/topics/i18n/ -LANGUAGE_CODE = 'es-es' +LANGUAGE_CODE = "es-es" -TIME_ZONE = 'UTC' +TIME_ZONE = "UTC" USE_I18N = True @@ -146,12 +149,10 @@ CHANNEL_LAYERS = { } - - # Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" if DEBUG: CACHES = { diff --git a/core/urls.py b/core/urls.py index 3141348..38fd573 100644 --- a/core/urls.py +++ b/core/urls.py @@ -15,7 +15,8 @@ Including another URLconf """ from django.contrib import admin from django.urls import path +from app.website import views as website_views urlpatterns = [ - path('admin/', admin.site.urls), + path("admin/", admin.site.urls), ] diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index 66c4fe9..74d2dfe 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -78,5 +78,3 @@ services: restart: "no" expose: - 1025 - ports: - - ${MAILHOG_PORT}:8025 diff --git a/tests/__ini__.py b/tests/__ini__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_start.py b/tests/test_start.py similarity index 71% rename from test/test_start.py rename to tests/test_start.py index 6c9f592..2ea353d 100644 --- a/test/test_start.py +++ b/tests/test_start.py @@ -1,4 +1,3 @@ - def test_hello_world(): assert "hello_world" == "hello_world" - assert "foo" != "bar" \ No newline at end of file + assert "foo" != "bar" From 1d684069ad346dcbd8e59440af211947005929c4 Mon Sep 17 00:00:00 2001 From: jnfire Date: Tue, 2 Aug 2022 08:56:52 +0200 Subject: [PATCH 07/10] fix project name fix project name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d6e599a..a80be55 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Open `https://proyect.com`. # Enviroment (.env) ```text -PROJECT_NAME=here.project +PROJECT_NAME=project # Domain DOMAIN=project.localhost From 2a80349e500fb1777452a3f3c816a1a81d85ad8d Mon Sep 17 00:00:00 2001 From: jnfire Date: Tue, 2 Aug 2022 08:58:45 +0200 Subject: [PATCH 08/10] fix app route fix app route --- apps/website/apps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/website/apps.py b/apps/website/apps.py index f1d93d1..0d4a5d4 100644 --- a/apps/website/apps.py +++ b/apps/website/apps.py @@ -3,4 +3,4 @@ from django.apps import AppConfig class WebsiteConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" - name = "app.website" + name = "apps.website" From bb5423fcc4b901d88c901d74aa0e22371a06cf34 Mon Sep 17 00:00:00 2001 From: jnfire Date: Tue, 2 Aug 2022 09:04:23 +0200 Subject: [PATCH 09/10] add first url add first url --- apps/website/urls.py | 7 +++++++ core/urls.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 apps/website/urls.py diff --git a/apps/website/urls.py b/apps/website/urls.py new file mode 100644 index 0000000..69b66f1 --- /dev/null +++ b/apps/website/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from apps.web.views import home + +urlpatterns = [ + path("", home, name="home"), +] \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 38fd573..f3d2a8c 100644 --- a/core/urls.py +++ b/core/urls.py @@ -14,9 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path -from app.website import views as website_views +from django.urls import path, include urlpatterns = [ + path("", include("apps.web.urls")), path("admin/", admin.site.urls), ] From fe49b302aeead7165b5e9b3ccde29035e2e5d12d Mon Sep 17 00:00:00 2001 From: jnfire Date: Tue, 2 Aug 2022 09:06:45 +0200 Subject: [PATCH 10/10] fix website fix website --- apps/website/urls.py | 2 +- core/urls.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/website/urls.py b/apps/website/urls.py index 69b66f1..3fbd58a 100644 --- a/apps/website/urls.py +++ b/apps/website/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from apps.web.views import home +from apps.website.views import home urlpatterns = [ path("", home, name="home"), diff --git a/core/urls.py b/core/urls.py index f3d2a8c..576217f 100644 --- a/core/urls.py +++ b/core/urls.py @@ -17,6 +17,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path("", include("apps.web.urls")), + path("", include("apps.website.urls")), path("admin/", admin.site.urls), ]