Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/node_modules
fortytwo/local_settings.py.example
frontend/dist
frontend/manifest.json
28 changes: 0 additions & 28 deletions .eslintrc.json

This file was deleted.

7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ static
*.dat
.env
.idea/
fortytwo_test_task/settings/local.py
/node_modules/
fortytwo/local_settings.py
frontend/node_modules/*
frontend/dist/*
frontend/manifest.json
.coverage
77 changes: 24 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,77 +1,48 @@
#
# constants
#
# Dev servers
CMD_PREFIX=./docker/compose.sh
MANAGE=$(CMD_PREFIX) run backend python manage.py
TEST_SETTINGS=fortytwo.test_settings
TEST_APP=apps/

SHELL=/bin/bash

PROJECT_NAME=fortytwo_test_task
BIND_TO=localhost
RUNSERVER_PORT=8000
SETTINGS=fortytwo_test_task.settings
TEST_SETTINGS=fortytwo_test_task.settings.test
TEST_APP?=apps
flake8=flake8

PYTHONPATH=$(CURDIR)

MANAGE_PREFIX= PYTHONPATH=$(PYTHONPATH) DJANGO_SETTINGS_MODULE=$(SETTINGS)
MANAGE_CMD=./manage.py
MANAGE= PYTHONPATH=$(PYTHONPATH) DJANGO_SETTINGS_MODULE=$(SETTINGS) $(MANAGE_CMD)

-include Makefile.def

#
# end of constants
#
run:
@echo Starting http://127.0.0.1:8000
$(CMD_PREFIX) up

#
# targets
#
build:
$(CMD_PREFIX) build

.PHONY: run syncdb initproject dumpdata shell flake8 djangotest collectstatic clean manage migrate only_migrate init_migrate
# Database
migrate:
$(MANAGE) migrate --noinput

run:
@echo Starting $(PROJECT_NAME)...
$(MANAGE) runserver $(BIND_TO):$(RUNSERVER_PORT)
migrations:
$(MANAGE) makemigrations

createcachetable:
@echo Creating cache table
$(MANAGE) createcachetable

initproject: migrate createcachetable

# Testing
shell:
@echo Starting shell...
$(MANAGE) shell

lint:
./bin/check_noqa.sh
./bin/check_layout.sh
jinjalint templates
$(flake8) apps
black apps
$(CMD_PREFIX) run backend sh -c "black apps && flake8 apps"

djangotest:
TESTING=1 PYTHONWARNINGS=ignore $(MANAGE_CMD) test --settings=$(TEST_SETTINGS) $(TEST_APP)
$(MANAGE) test --settings=$(TEST_SETTINGS) $(TEST_APP) --noinput

coverage:
coverage run manage.py test --settings=$(TEST_SETTINGS) $(TEST_APP) && coverage report -i

test: lint djangotest

collectstatic: clean
@echo Collecting static
collectstatic:
@echo Collecting static files
$(MANAGE) collectstatic --noinput
@echo Done

clean:
@echo Cleaning up...
find ./ -name '__pycache__' -print|xargs -I {} rm -r {}
find staticfiles/generated/ -type f | xargs -I {} rm {}
@echo Done

migrate:
$(MANAGE) migrate

migrations:
$(MANAGE) makemigrations

eslint:
node_modules/.bin/eslint .
$(CMD_PREFIX) run frontend sh -c "cd frontend && yarn lint src --fix"
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@

A Django 3+ project template

Use fortytwo_test_task.settings when deploying with getbarista.com

### Requirements
* apps in apps/ folder
* use migrations
* use local.py for different local settings
* templates live in templates/
* static lives in assets/
* SPA
* global: `yarn`, `node` (tested on v15), `python>=3.7` (tested on 3.9)
* management commands should be proxied to single word make commands, e.g make test
* run `make lint` often
* installed node and yarn. after cloning the project run `yarn`
* lint your js, run `make eslint`
3 changes: 0 additions & 3 deletions apps/hello/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.contrib import admin

# Register your models here.
File renamed without changes.
9 changes: 9 additions & 0 deletions apps/hello/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from apps.hello.api.views import PingView

app_name = "api_hello"

urlpatterns = [
path("ping/", PingView.as_view(), name="ping"),
]
7 changes: 7 additions & 0 deletions apps/hello/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework import views, status
from rest_framework.response import Response


class PingView(views.APIView):
def get(self, request, **kwargs):
return Response({"status": True}, status=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion apps/hello/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class HelloConfig(AppConfig):
name = 'apps.hello'
name = "apps.hello"
3 changes: 0 additions & 3 deletions apps/hello/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.db import models

# Create your models here.
11 changes: 0 additions & 11 deletions apps/hello/tests.py

This file was deleted.

Empty file added apps/hello/tests/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions apps/hello/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework.test import APITestCase
from django.shortcuts import reverse


class HelloAPITest(APITestCase):
def test_ping(self):
"""
endpoint is accessible and always return same status
"""
response = self.client.get(reverse("api_hello:ping"))
self.assertTrue(response.json()["status"])
3 changes: 0 additions & 3 deletions apps/hello/views.py

This file was deleted.

7 changes: 0 additions & 7 deletions bin/check_layout.sh

This file was deleted.

11 changes: 0 additions & 11 deletions bin/check_noqa.sh

This file was deleted.

25 changes: 25 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.7"

services:
backend:
build:
context: .
dockerfile: docker/backend.Dockerfile
container_name: backend
restart: always
# mount the volumes so we can change code and hot reload
volumes:
- .:/app
ports:
- 8000:8000

frontend:
build:
context: .
dockerfile: docker/frontend.Dockerfile
container_name: frontend
volumes:
- .:/app
ports:
- 3000:3000
user: ${CURRENT_UID}
3 changes: 3 additions & 0 deletions docker/backend-cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python manage.py migrate --noinput
python manage.py createcachetable
supervisord -c /etc/supervisord.conf
15 changes: 15 additions & 0 deletions docker/backend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3-alpine

WORKDIR /app
ADD . /app

# gcc, libc-dev are flake8 (typed-ast) deps
RUN apk update \
&& apk add make supervisor gcc libc-dev \
&& pip install gunicorn \
&& pip install -r requirements/base.txt \
&& pip install -r requirements/dev.txt \
&& echo "files = /app/docker/conf/supervisor.ini" >> /etc/supervisord.conf \
&& rm -rf /var/cache/apk/*

CMD docker/backend-cmd.sh
10 changes: 10 additions & 0 deletions docker/compose.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh
set -x
# If first argument 'exec' or 'run' adds --user flag, or sets CURRENT_UID flag so you can use it inside your scripts

G_U_ID=$(id -u):$(id -g)
FIRST_ARG=$1
shift
[ "$FIRST_ARG" = "exec" ] || [ "$FIRST_ARG" = "run" ] && FIRST_ARG="$FIRST_ARG --user $G_U_ID"
# shellcheck disable=SC2086
CURRENT_UID=$G_U_ID docker-compose $FIRST_ARG "$@"
13 changes: 13 additions & 0 deletions docker/conf/supervisor.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[supervisord]
nodaemon=true
pidfile=/tmp/fttt_docker_backend_supervisord.pid

[program:web]
command=gunicorn fortytwo.wsgi -b 0.0.0.0:8000 --reload
directory=/app
autostart=true
autorestart=true
startsecs=10
startretries=3
stdout_logfile=/var/log/webout.log
stderr_logfile=/var/log/weberr.log
3 changes: 3 additions & 0 deletions docker/frontend-cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd /app/frontend
yarn
yarn serve
6 changes: 6 additions & 0 deletions docker/frontend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:15-alpine

WORKDIR /app
ADD . /app

CMD docker/frontend-cmd.sh
Empty file added fortytwo/__init__.py
Empty file.
File renamed without changes.
Loading