Skip to content

Commit

Permalink
Merge branch 'main' into upgrade-poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
theskumar authored Dec 20, 2023
2 parents 4432046 + 8dbac34 commit a190b89
Show file tree
Hide file tree
Showing 53 changed files with 1,676 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ Session.vim

# Pycharm project modules
.idea/


### VSCode
.vscode/
71 changes: 66 additions & 5 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,74 @@ History
----------------------
<small>Note: This file is autogenerated with [generate-history.sh](generate-history.sh)</small>

### 2022-01-05
### 2023-12-20
- feat(fly-io): Added fly.io template files (#475) ([John Taylor])

- Integrate poetry and update makefile commands. ([Sanyam Khurana])
- Move isort/black config from .editorconfig to .pyproject.toml. ([Sanyam Khurana])
### 2023-11-27
- feat(async): add uvicorn, asgi, upgrade django=4.1 (#473) ([Suneet Choudhary])

### 2022-01-11
- Fix the use of letsencrypt tasks to run only on hosts configured with `use_letsencrypt`. ([Sanyam Khurana])
### 2023-04-18
- fix(docker): Fixed an issue with the Postgres Docker image name (#474) ([John Taylor])

### 2023-03-28
- feat: add GraphQL API implementation using django-graphene (#449) ([Suneet Choudhary])

### 2023-03-03
- fix(docker-start): Redirect access/error logs to std-out (#470) ([Sanyam Khurana])

### 2023-02-27
- fix(Makefile): Update poetry export command to use --with option (#468) ([Sanyam Khurana])

### 2023-02-06
- fix(compose): Use postgis protocol for connection (#467) ([Sanyam Khurana])

### 2023-01-06
- feat(docker): Use GDAL/postgis when postgis is enabled (#464) ([Sanyam Khurana])

### 2022-12-15
- Configure Renovate (#459) ([renovate[bot]])

### 2022-12-07
- fix(users/api): Make code conformant to PEP8 ([Sanyam Khurana])
- chore: Add docker-compose to run all services through docker (#440) ([Sanyam Khurana])

### 2022-10-27
- fix(pyproject.toml): Add dependencies for mkdocs (#458) ([Sanyam Khurana])

### 2022-10-25
- docs(README): Update docs to drop legacy poetry command (#457) ([Sanyam Khurana])
- fix(github-actions): Use poetry to install requirements & run tests (#456) ([Sanyam Khurana])

### 2022-09-27
- docs(coding_rules): correct class names to be PascalCase (#455) ([Sanyam Khurana])

### 2022-09-16
- ci(github-actions): ensure poetry is installed lint action (#453) ([Sanyam Khurana])

### 2022-09-14
- chore: make the relative and absolute imports consistent (#450) ([Sahith Chandan Mekala])
- upgrade packages (#451) ([Sahith Chandan Mekala])

### 2022-06-18
- upgrade(requirements): black 21.12b0 => 22.3.0 (#447) ([Akash Mishra])

### 2022-02-16
- feat(setup): Add dependency management with poetry (#444) ([Sanyam Khurana])

### 2022-01-17
- chore: update packages - celery, ansible (#445) ([Suneet Choudhary])

### 2022-01-13
- fix(nginx/tasks): Run letsencrypt only for hosts with use_letsencrypt config (#446) ([Sanyam Khurana])

### 2022-01-04
- Fix(API-docs): deprecated rest_framework_swagger (#441) ([Suneet Choudhary])

### 2021-12-17
- feat: update python dependencies (#443) ([Saurabh Kumar])

### 2021-12-14
- chore(CI): add python cache with pip ([Saurabh Kumar])

### 2021-12-07
- chore: Update django-sites to 0.11 for Django 3.x (#442) ([Sanyam Khurana])
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

## Features

- Django 3.2.x
- Django 4.1.x
- Python 3.9.x
- [Poetry][poetry] Support
- Support for [black](https://pypi.org/project/black/)!
Expand All @@ -29,13 +29,15 @@

### Optional
- Heroku Setup
- Fly Setup
- Ubuntu 20 LTS via [Ansible]
- Celery with flower integration.
- AWS S3 media storage
- [Letsencrypt](https://letsencrypt.org/) Support via [certbot](https://certbot.eff.org).
- Postgis Setup
- Newrelic
- Sentry
- [GraphQL](https://graphql.org/) support via [Graphene-Django](https://docs.graphene-python.org/projects/django/en/latest/) (Optional)
- pre-commit hooks


Expand Down
5 changes: 5 additions & 0 deletions cookiecutter-test-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default_context:
enable_whitenoise: "y"
add_celery: "y"
add_graphql: "y"
add_asgi: "y"
3 changes: 3 additions & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
, "add_newrelic" : "y"
, "add_postgis": "n"
, "add_heroku": "n"
, "add_fly": "n"
, "enable_whitenoise": "n"
, "add_ansible": "y"
, "letsencrypt": "y"
Expand All @@ -18,6 +19,8 @@
, "add_sentry": "y"
, "add_django_auth_wall": "y"
, "add_celery": "n"
, "add_graphql": "n"
, "add_asgi": "n"
, "add_pre_commit": "y"
, "add_docker": "y"
, "pagination": ["LimitOffsetPagination", "CursorPagination"]
Expand Down
17 changes: 17 additions & 0 deletions hooks/post_gen_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ if echo "{{ cookiecutter.add_heroku }}" | grep -iq "^n"; then
rm -rf uwsgi.ini Procfile bin/post_compile
fi

if echo "{{ cookiecutter.add_fly }}" | grep -iq "^n"; then
rm .github/workflows/fly.yml
rm -rf compose/fly
fi

if echo "{{ cookiecutter.add_ansible }}" | grep -iq "^n"; then
rm -rf provisioner ansible.cfg
fi
Expand All @@ -45,6 +50,18 @@ if echo "{{ cookiecutter.add_docker }}" | grep -iq "^n"; then
rm -rf .envs compose local.yml dev.yml docs/backend/docker_setup.md
fi

if echo "{{ cookiecutter.add_graphql }}" | grep -iq "^n"; then
rm -rf {{ cookiecutter.main_module }}/graphql
rm -rm {{ cookiecutter.main_module }}/docs/graphql
rm -rf tests/graphql
fi

if echo "{{ cookiecutter.add_asgi }}" | grep -iq "^n"; then
rm -rf asgi.py
else
rm -rf wsgi.py
fi

if echo "$yn" | grep -iq "^y"; then
echo "==> Checking system dependencies. You may need to enter your sudo password."

Expand Down
2 changes: 1 addition & 1 deletion run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi
rm -rf hello-world-backend/;

# Generate new code, (it also creates db, migrate and install dependencies)
yes 'y' | cookiecutter . --no-input
yes 'y' | cookiecutter . --no-input --config-file cookiecutter-test-config.yaml

# Run the tests present inside generate project
cd hello-world-backend;
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.github_repository}}/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# DJANGO_AWS_S3_HOST=''
# DJANGO_AWS_S3_REGION_NAME=''

# Django Rest Framework
# APIs
# ==============================
# API_DEBUG=False

Expand Down
15 changes: 15 additions & 0 deletions {{cookiecutter.github_repository}}/.github/workflows/fly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Fly Deploy
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --dockerfile ./compose/fly/django/Dockerfile
env:
FLY_API_TOKEN: ${{ "{{" }} secrets.FLY_API_TOKEN {{ "}}" }}
35 changes: 30 additions & 5 deletions {{cookiecutter.github_repository}}/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{{ cookiecutter.project_name }}
==============================
# {{ cookiecutter.project_name }}

__Version:__ {{ cookiecutter.version }}
**Version:** {{ cookiecutter.version }}

{{ cookiecutter.project_description }}

## Getting up and running

{% if cookiecutter.add_docker == 'y' %}
!!! note
For setting up locally using `Docker`, check [here](docs/backend/docker_setup.md)
For setting up locally using `Docker`, check [here](docs/backend/docker_setup.md)
{% endif %}

Minimum requirements: **pip, python3.9, poetry, redis & [PostgreSQL 11][install-postgres]{% if cookiecutter.add_postgis.lower() == "y" %} with postgis-2.4{% endif %}**, setup is tested on Mac OSX only.
Expand Down Expand Up @@ -43,7 +42,7 @@ Running `poetry lock` generates `poetry.lock` which has all versions pinned.

You can install Poetry by using `pip install --pre poetry` or by following the official installation guide [here](https://github.com/python-poetry/poetry#installation).

*Tip:* We recommend that you use this workflow and keep `pyproject.toml` as well as `poetry.lock` under version control to make sure all computers and environments run exactly the same code.
_Tip:_ We recommend that you use this workflow and keep `pyproject.toml` as well as `poetry.lock` under version control to make sure all computers and environments run exactly the same code.


## Deploying Project
Expand All @@ -52,6 +51,32 @@ The deployment are managed via travis, but for the first time you'll need to set

Check out detailed server setup instruction [here](docs/backend/server_config.md).

{% if cookiecutter.add_fly == 'y' %}

### Develop on Fly.io

Create a [fly.io](https://fly.io) account.

Install `flyctl` and run the following commands to set up the Fly.io application, it will ask a series of questions regarding deployment configuration.

```
brew install flyctl
fly lauch
```

When ready to deploy, simply run the command using the Fly Dockerfile:

```
flyctl deploy --dockerfile ./compose/fly/django/Dockerfile
```

There is also a Github Action provided `.github/workflows/fly.yml` to deploy the application on the `master` branch. In order to deploy from CI:

1. Create Fly Access Token [here](https://fly.io/user/personal_access_tokens).
2. Add the `FLY_API_TOKEN` to the Github repo secrets [here](https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.github_repository}}/settings/secrets/actions)

{% endif %}

## How to release {{ cookiecutter.project_name }}

Execute the following commands:
Expand Down
15 changes: 15 additions & 0 deletions {{cookiecutter.github_repository}}/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Standard Library
import os

# Third Party Stuff
from django.core.asgi import get_asgi_application
from dotenv import load_dotenv

# Read .env file and set key/value inside it as environment variables
# see: http://github.com/theskumar/python-dotenv
load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))

# We defer to a DJANGO_SETTINGS_MODULE already in the environment.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.production")

application = get_asgi_application()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG PYTHON_VERSION=3.9-slim-buster
# define an alias for the specfic python version used in this file.
FROM python:${PYTHON_VERSION} as python

ENV POETRY_VERSION=1.3.1
ENV POETRY_VERSION=1.3.2

ARG BUILD_ENVIRONMENT=dev
ARG APP_HOME=/app
Expand All @@ -28,6 +28,9 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
# Versatile image field & pillow \
libmagic1 \
libmagic-dev \
{% if cookiecutter.add_postgis.lower() == "y" %}# GDAL postgres requirements
gdal-bin \
libgdal-dev \{% endif %}
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if [ -z "${POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
export DATABASE_URL="{% if cookiecutter.add_postgis.lower() == 'y' %}postgis{% else %}postgres{% endif %}://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

postgres_ready() {
python << END
Expand Down
7 changes: 5 additions & 2 deletions {{cookiecutter.github_repository}}/compose/dev/django/start
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ set -o nounset


python /app/manage.py collectstatic --noinput
# /usr/local/bin/gunicorn asgi --bind 0.0.0.0:5000 --chdir=/app -k uvicorn.workers.UvicornWorker
/usr/local/bin/gunicorn wsgi --bind 0.0.0.0:5000 --chdir=/app
{%- if cookiecutter.add_asgi.lower() == "y" %}
gunicorn asgi --bind 0.0.0.0:8000 --chdir=/app -k uvicorn.workers.UvicornWorker
{%- else %}
gunicorn wsgi --bind 0.0.0.0:8000 --chdir=/app --access-logfile - --error-logfile -
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM postgres:13
{% if cookiecutter.add_postgis.lower() == "y" %}FROM postgis/postgis:13-3.3{% else %}FROM postgres:13{% endif %}

COPY ./compose/dev/postgres/maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
Expand Down
64 changes: 64 additions & 0 deletions {{cookiecutter.github_repository}}/compose/fly/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
ARG PYTHON_VERSION=3.9-slim-buster

# define an alias for the specfic python version used in this file.
FROM python:${PYTHON_VERSION} as python

ENV POETRY_VERSION=1.3.2

ARG BUILD_ENVIRONMENT=dev
ARG APP_HOME=/app

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

WORKDIR ${APP_HOME}

RUN addgroup --system django \
&& adduser --system --ingroup django django

# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential \
# psycopg2 dependencies
libpq-dev \
# Translations dependencies
gettext \
# Versatile image field & pillow \
libmagic1 \
libmagic-dev \

# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN pip install --no-cache-dir poetry==${POETRY_VERSION}

COPY poetry.lock pyproject.toml ${APP_HOME}/

# Project initialization:
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi

COPY --chown=django:django ./compose/dev/django/celery/worker/start /start-celeryworker
RUN chmod +x /start-celeryworker


COPY --chown=django:django ./compose/dev/django/celery/beat/start /start-celerybeat
RUN chmod +x /start-celerybeat


COPY ./compose/dev/django/celery/flower/start /start-flower
RUN chmod +x /start-flower

COPY --chown=django:django . ${APP_HOME}

# make django owner of the WORKDIR directory as well.
RUN chown django:django ${APP_HOME}

RUN python manage.py collectstatic --noinput

EXPOSE 8000

CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "wsgi:application"]
Loading

0 comments on commit a190b89

Please sign in to comment.