diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..95e6d0f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +venv +.git +db.sqlite3 +.env +.idea +.vscode diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml index 58caef8..61a0d1d 100644 --- a/.github/workflows/workflow.yaml +++ b/.github/workflows/workflow.yaml @@ -1,25 +1,51 @@ -name: Upload coverage reports to Codecov +name: Workflow on: push: branches: - main + - dev jobs: - build: + tests: runs-on: ubuntu-latest + steps: - - name: Checkout code - uses: actions/checkout@v2 + - name: Check out code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.11 - - name: Run tests with coverage + - name: Install dependencies run: | - pip install coverage - coverage run -m unittest discover - coverage report -m + python -m pip install --upgrade pip + pip install -r requirements_mini.txt - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4.0.1 + - name: Check with flake8 + run: python -m flake8 crm_yandex/ + + build_and_push_to_docker_hub: + name: Push Docker image to DockerHub + runs-on: ubuntu-latest + needs: tests + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Push to DockerHub + uses: docker/build-push-action@v4 with: - token: ${{ secrets.CODECOV_TOKEN }} - slug: CRM-for-Yandex-ambassadors/backend + push: true + tags: margoloko/ya_crm_backend:latest diff --git a/.gitignore b/.gitignore index 8c479b9..2ad4227 100644 --- a/.gitignore +++ b/.gitignore @@ -161,4 +161,3 @@ cython_debug/ .vscode/ migrations/ - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1cc3b80..8d993ae 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,12 +1,12 @@ repos: - repo: https://github.com/commitizen-tools/commitizen - rev: 3.5.3 + rev: 3.6.0 hooks: - id: commitizen stages: [commit-msg] - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -15,14 +15,8 @@ repos: - id: check-merge-conflict - id: check-json -- repo: https://github.com/commitizen-tools/commitizen - rev: 3.6.0 - hooks: - - id: commitizen - stages: [commit-msg] - - repo: https://github.com/pycqa/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort exclude: (migrations) @@ -32,7 +26,7 @@ repos: rev: 6.1.0 hooks: - id: flake8 - exclude: (apps|__init__|migrations|backend/demand_oracle/demand_oracle/settings/) + exclude: (apps|__init__|migrations|backend/crm_yandex/crm_yandex/settings/) additional_dependencies: - flake8-docstrings args: @@ -40,8 +34,8 @@ repos: - --ignore=D100, D105, D106 - repo: https://github.com/ambv/black - rev: 23.7.0 + rev: 24.2.0 hooks: - id: black - exclude: (migrations|backend/demand_oracle/demand_oracle/settings/) + exclude: (__init__|migrations|backend/crm_yandex/crm_yandex/settings/) args: [--line-length=88] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..766fb41 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.11 + +WORKDIR /backend + +RUN pip install gunicorn==20.1.0 + +COPY requirements.txt . + +RUN pip install -r requirements.txt --no-cache-dir + +RUN apt-get update && apt-get install -y nano + +COPY crm_yandex/ . + +CMD ["gunicorn", "crm_yandex.wsgi:application", "--bind", "0.0.0.0:8000", "--reload"] diff --git a/README.md b/README.md index 3d70e3c..2ba3e91 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # backend CRM +![example workflow](https://github.com/CRM-for-Yandex-ambassadors/backend/actions/workflows/workflow.yaml/badge.svg) +[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) + ![python version](https://img.shields.io/badge/Python-3.11-green) ![django version](https://img.shields.io/badge/Django-4.2-green) ![djangorestframework version](https://img.shields.io/badge/DRF-3.14-green) -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) -[![codecov](https://codecov.io/gh/CRM-for-Yandex-ambassadors/backend/graph/badge.svg?token=AWKLAD8MLB)](https://codecov.io/gh/CRM-for-Yandex-ambassadors/backend) ### Оглавление: - [backend CRM](#backend-crm) - - [Оглавление:](#оглавление) - - [О проекте:](#о-проекте) + - [О проекте](#о-проекте) - [Запуск приложения](#запуск-приложения) - [Запуск приложения на локальном сервере](#запуск-приложения-на-локальном-сервере) - [Запуск тестов](#запуск-тестов) @@ -150,4 +150,4 @@ cz c - [Балахонова Марина](https://github.com/margoloko) - [Виноградов Сергей](https://github.com/yan-gabala) - [Раскатов Андрей](https://github.com/Diavolution) -- [Смирнов Артем](https://github.com/BeardedDev1911) +- [Климов Артем](https://github.com/grinchomsk) diff --git a/crm_yandex/crm_yandex/settings.py b/crm_yandex/crm_yandex/settings.py index e6bafa7..ade5f7e 100644 --- a/crm_yandex/crm_yandex/settings.py +++ b/crm_yandex/crm_yandex/settings.py @@ -9,10 +9,13 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ - +import os from datetime import timedelta from pathlib import Path +from dotenv import load_dotenv + +load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -21,12 +24,12 @@ # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "django-insecure-ir8*tzr*o!cqe@h)a35pht8($4*-+rqn6qp$f0g$*o9+g(a79n" +SECRET_KEY = os.getenv("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["127.0.0.1", "localhost", "158.160.13.137", "crm-yandex.ddns.net"] # Application definition @@ -80,8 +83,12 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + "ENGINE": "django.db.backends.postgresql", + "NAME": os.getenv("POSTGRES_DB"), + "USER": os.getenv("POSTGRES_USER"), + "PASSWORD": os.getenv("POSTGRES_PASSWORD"), + "HOST": os.getenv("DB_HOST"), + "PORT": os.getenv("DB_PORT"), } } @@ -136,6 +143,7 @@ # https://docs.djangoproject.com/en/4.2/howto/static-files/ STATIC_URL = "static/" +STATIC_ROOT = os.path.join(BASE_DIR, "static/") # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/crm_yandex/crm_yandex/urls.py b/crm_yandex/crm_yandex/urls.py index 6ef0a67..2eb5865 100644 --- a/crm_yandex/crm_yandex/urls.py +++ b/crm_yandex/crm_yandex/urls.py @@ -19,6 +19,6 @@ urlpatterns = [ path("admin/", admin.site.urls), - path("auth/", include("djoser.urls")), - path("auth/", include("djoser.urls.jwt")), + path("authorize/", include("djoser.urls")), + path("authorize/", include("djoser.urls.jwt")), ] diff --git a/crm_yandex/db.sqlite3 b/crm_yandex/db.sqlite3 new file mode 100644 index 0000000..34f6ef0 Binary files /dev/null and b/crm_yandex/db.sqlite3 differ diff --git a/crm_yandex/db.sqlite3.bak b/crm_yandex/db.sqlite3.bak new file mode 100644 index 0000000..06c0b5b Binary files /dev/null and b/crm_yandex/db.sqlite3.bak differ diff --git a/infra/Dockerfile b/infra/Dockerfile new file mode 100644 index 0000000..f0808a1 --- /dev/null +++ b/infra/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx:1.22.1 +COPY nginx.conf /etc/nginx/templates/default.conf.template diff --git a/infra/docker-compose.yml b/infra/docker-compose.yml new file mode 100644 index 0000000..6f8ca79 --- /dev/null +++ b/infra/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3' + +volumes: + pg_data: + +services: + db: + image: postgres:13.10 + env_file: ../.env + volumes: + - pg_data:/var/lib/postgresql/data + + backend: + image: margoloko/ya_crm_backend:latest + restart: always + build: ../. + env_file: ../.env + depends_on: + - db + + #frontend: + # image: margoloko/ya_crm_frontend:latest + # volumes: + # - ... + # env_file: ../.env + # depends_on: + # - backend + + nginx: + build: . + # Ключ ports устанавливает + # перенаправление всех запросов с порта 8000 хоста + # на порт 80 контейнера. + ports: + - 8000:80 diff --git a/requirements.txt b/requirements.txt index e615b60..69d9b83 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,8 +16,8 @@ django-oauth-toolkit==2.3.0 django-rest-framework-social-oauth2==1.2.0 django-templated-mail==1.1.1 djangorestframework==3.14.0 -# djangorestframework-jwt==1.11.0 -djangorestframework-simplejwt==4.7.2 +djangorestframework-jwt +djangorestframework-simplejwt djoser==2.1.0 filelock==3.13.1 identify==2.5.35 @@ -33,14 +33,16 @@ packaging==23.2 pathspec==0.12.1 platformdirs==4.2.0 pre-commit==3.6.2 +psycopg2-binary==2.9.3 pycparser==2.21 -PyJWT==2.8.0 +python-dotenv==1.0.1 python3-openid==3.2.0 pytz==2024.1 PyYAML==6.0.1 requests==2.31.0 requests-oauthlib==1.3.1 six==1.16.0 +social-auth-core==4.5.3 sqlparse==0.4.4 tomli==2.0.1 typing_extensions==4.9.0 diff --git a/requirements_mini.txt b/requirements_mini.txt new file mode 100644 index 0000000..d982b31 --- /dev/null +++ b/requirements_mini.txt @@ -0,0 +1,19 @@ +asgiref==3.7.2 +black==24.2.0 +Django==4.2 +django-oauth-toolkit==2.3.0 +djangorestframework==3.14.0 +django-templated-mail==1.1.1 +djangorestframework-jwt +djangorestframework-simplejwt +djoser==2.1.0 +flake8==6.1.0 +flake8-isort==6.0.0 +psycopg2-binary==2.9.3 +python-dotenv==1.0.1 +pytz==2024.1 +sqlparse==0.4.4 +typing_extensions==4.9.0 +tzdata==2024.1 +urllib3==2.2.1 +virtualenv==20.25.1 diff --git a/setup.cfg b/setup.cfg index 2752c3b..b21832d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,7 +7,9 @@ exclude = tests/, */migrations/, venv/, - env/ + */venv/, + env/, + */env/ per-file-ignores = */settings.py:E501 max-complexity = 10