/django-school-api-drf-nested-router

Sample usage of Django, Django Rest Framework, drf-nested-routers

Primary LanguageJavaScript

Development note

Please see note for detailed explainations:

https://cheerful-olivine-ef5.notion.site/Django-School-Management-System-DSMS-cf3cdecb4362416f91cbc2aa3ea4a3e4

django-school-api-drf-nested-router

Sample usage of Django, Django Rest Framework, drf-nested-routers DEUBG=True is intentionally left on, to disable change environmental variable ENV to anything other than staging This is intended to be backend, all non matched routes are 404 by default

Milestones

  • Create Django App
  • Use Postgres
  • Use Pipenv
  • Stores sensitive information in .env (and not committing them!)
  • models: student, first_name, last_name, student identification id as UUID4() but only 20 chars
  • models: school, name, max_student
  • Use DRF
  • Enable DRF API views
  • use ModelViewSet
  • use ModelSerializer
  • endpoint /students/ GET, POST
  • endpoint /schools/ GET, POST
  • endpoint /students/:id PUT, PATCH, DELETE
  • endpoint /schools/:id PUT, PATCH, DELETE
  • Automatic UUID for student upon creation
  • Disallow student to overflow school
  • Use Django Nested Routers
  • /schools/:id/students GET
  • /schools/:id/students POST
  • /schools/:id/students/:id GET, POST, PATCH, PUT, DELETE
  • /students/ still works

Deployments

Heroku

https://django-school-api.herokuapp.com/api/v1/students/

AWS ECS Fargate

http://18.140.53.106:8000/api/v1/students/

docker-compose

Docker compose with quick tunnel to the internet for collaboration cloudflare argo tunnels your applications to the internet through Cloludflare without exposing any ports


docker compose --env-file ./src/app/app/.env up --build --force-recreate

Look for a url similar to https://savage-co**********cloudflare.com/api/v1/students/ in logs

pipenv with postgres in docker

cd postgres
docker compose up --build --force-recreate
cd src/app
conda activate <env>
pipenv run python manage.py runserver

Proceed to 127.0.0.1:8000/api/v1/students/

Notice

AWS and Heroku deployments will be taken down by the end of June. 💡 Paid services are already taken down.

Testings

usage

Make sure to change BASE_URL according to which deployment you want to test

conda create --name pytest python=3.9.7
conda activate pytest
pip install -r requirements.txt
cd tests
pytest -vv test_*

Bonus

  1. Notion development documentation
  2. Use deploy Postgres via docker with data persistence
  3. admin view for debugging/ quick edits
  4. Test driven design with pytest (with conda plus pip install -r requirements.txt)
  5. AWS Fargate serverless docker hosting
  6. Postgres deployed in AWS RDS
  7. USED AWS ECS Registry
  8. Docker compose local deploy with automatic exposing to the internet through cloudflare Argi (suitable for short client demo, team demo)
  9. Heroku database setup (does this count as a bonus ?)
  10. gunicorn was used in AWS deployment
  11. Serve static file without CDN/Bucket with Whitenoise

Time logging

  • Preparing, reading some documentations, and thinking how to tackle this problem: about 1-2 days on, and off while driving, showering (best methods!)
  • Setting up the development environment: 15 minutes, I do this pretty much everyday
  • Writing Django models up to working api routes 30 minutes, I do this everyday, too, but with Django Ninja instead of DRF
  • Django Nested Routers: about 2 hours of trying to understand the sparse documentations and how data is passed, I have never used this before. I think of this as a challenge to see how I learn new techs.
  • Writing helper functions: less than 15 minutes
  • Pytest normal cases and requirements, catching bugs: about 1.5 - 2 hours
  • Pytest edge cases: 30 minutes
  • Heroku deplotment: 30 minutes
  • AWS deployment: 2 hours, built image on a Mac M1, forgot to specify amd64 architechture since AWS ECS Fargate is amd64 and M1 is arm64 :')
  • Docker local deployment with tunnels to the internet: 30 minutes

Caviets

Some config files are duplicated to root to make Heroku happy.