| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
👀 Need help with your Django project? HackSoft is here for you. Reach out at consulting@hacksoft.io
Table of contents:
Few points to navigate yourself:
That's about it ✨
Hello 👋
This projects serves as the following:
If you want to learn more about the Django Styleguide, you can watch the videos below:
Radoslav Georgiev's Django structure for scale and longevity for the philosophy behind the styleguide:
Radoslav Georgiev & Ivaylo Bachvarov's discussion on HackCast, around the Django Styleguide:
The initial structure was inspired by cookiecutter-django.
The structure now is modified based on our work & production experience with Django.
Few important things:
The project is running django-cors-headers with the following general configuration:
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_ALL_ORIGINS = TrueFor production.py, we have the following:
CORS_ALLOW_ALL_ORIGINS = False
CORS_ORIGIN_WHITELIST = env.list('CORS_ORIGIN_WHITELIST', default=[])The project is using https://github.com/Styria-Digital/django-rest-framework-jwt for having authentication via JWT capabilities.
All JWT related settings are located in config/settings/jwt.py.
⚠️ We highly recommend reading the entire settings page from the project documentation - https://styria-digital.github.io/django-rest-framework-jwt/#additional-settings - to figure out your needs & the proper defaults for you!
The default settings also include the JWT token as a cookie.
The specific details about how the cookie is set, can be found here - https://github.com/Styria-Digital/django-rest-framework-jwt/blob/master/src/rest_framework_jwt/compat.py#L43
The JWT related APIs are:
The current implementation of the login API returns just the token:
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJhZG9yYWRvQGhhY2tzb2Z0LmlvIiwiaWF0IjoxNjQxMjIxMDMxLCJleHAiOjE2NDE4MjU4MzEsImp0aSI6ImIyNTEyNmY4LTM3ZDctNGI5NS04Y2M0LTkzZjI3MjE4ZGZkOSIsInVzZXJfaWQiOjJ9.TUoQQPSijO2O_3LN-Pny4wpQp-0rl4lpTs_ulkbxzO4"
}This can be changed from auth_jwt_response_payload_handler.
We follow this concept:
This project is using the already existing cookie-based session authentication in Django:
sessionid=5yic8rov868prmfoin2vhtg4vx35h71p; expires=Tue, 13 Apr 2021 11:17:58 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax
axios.get(url, { withCredentials: true });
axios.post(url, data, { withCredentials: true });For convenience, CSRF_USE_SESSIONS is set to True
Check config/settings/sessions.py for all configuration that's related to sessions.
Since the default implementation of SessionAuthentication enforces CSRF check, which is not the desired behavior for our APIs, we've done the following:
from rest_framework.authentication import SessionAuthentication
class CsrfExemptedSessionAuthentication(SessionAuthentication):
"""
DRF SessionAuthentication is enforcing CSRF, which may be problematic.
That's why we want to make sure we are exempting any kind of CSRF checks for APIs.
"""
def enforce_csrf(self, request):
returnWhich is then used to construct an ApiAuthMixin, which marks an API that requires authentication:
from rest_framework.permissions import IsAuthenticated
class ApiAuthMixin:
authentication_classes = (CsrfExemptedSessionAuthentication, )
permission_classes = (IsAuthenticated, )By default, all APIs are public, unless you add the ApiAuthMixin
We have the following general cases:
The current implementation of /api/auth/session/login does 2 things:
The second thing is required, because Safari is not respecting the SameSite = None option for cookies.
More on the issue here - https://www.chromium.org/updates/same-site/incompatible-clients
Since cookies can be somewhat elusive, check the following urls:
You can find the UserListApi in styleguide_example/users/apis.py
List API is located at:
http://localhost:8000/api/users/
The API can be filtered:
Example data structure:
{
"limit": 1,
"offset": 0,
"count": 4,
"next": "http://localhost:8000/api/users/?limit=1&offset=1",
"previous": null,
"results": [
{
"id": 1,
"email": "radorado@hacksoft.io",
"is_admin": false
}
]
}
Following this article - https://www.hacksoft.io/blog/direct-to-s3-file-upload-with-django - there's a rich file-upload implementation in the Django Styleguide Example.
Everything is located in the files app.
Configuration wise, everything is located in config/settings/files_and_storages.py
Additionally, you can check the available options in .env.example
Currently, the following is supported:
Feel free to use this as the basis of your file upload needs.
To create Postgres database:
sudo -u postgres createdb -O your_postgres_user_here database_name_here
If you want to recreate your database, you can use the bootstrap script:
./scripts/bootstrap.sh your_postgres_user_here
To start Celery:
celery -A styleguide_example.tasks worker -l info --without-gossip --without-mingle --without-heartbeat
To start Celery Beat:
celery -A styleguide_example.tasks beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
To build and run everything
docker compose up
To run migrations
docker compose run django python manage.py migrate
To shell
docker compose run django python manage.py shell
This project is ready to be deployed either on Heroku or AWS ECS.
Deploying a Python / Django application on Heroku is quite straighforward & this project is ready to be deployed.
To get an overview of how Heroku deployment works, we recommend reading this first - https://devcenter.heroku.com/articles/deploying-python
Files related to Heroku deployment:
Additionally, you need to specify at least the following settings:
On top of that, we've added gunicorn.conf.py with some example settings.
We recommend the following materials, to figure out gunicorn defaults and configuration:
Coming soon
In all our Django projects we use:
To make sure all of the above tools work in symbiosis, you'd need to add some configuration:
build:
runs-on: ubuntu-latest
steps:
- name: Run ruff
uses: chartboost/ruff-action@v1
- name: Run ruff run: ruff check .
In order to test if your local setup is up to date, you can either:
| Back | FazBrowse Home | New Git URL |