Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a7e4028
Fixed most flake8 warnings
akhavr Mar 20, 2015
f21614b
Added `42cc_pystyle` to requirements and invocation of `flake8`
akhavr Mar 20, 2015
1fe78ab
Replaces `42cc_pystyle` for `flake8` until packaging problems would b…
akhavr Mar 23, 2015
1ead3cd
Returned 42cc style checks, excluding checks on migrations
akhavr Mar 23, 2015
8582c62
noqa check added
AlexandrDidyk Jun 15, 2016
f7ff5e0
Merge pull request #46 from AlexandrDidyk/master
42 Jun 17, 2016
254ad04
added --max-complexity=6 to flake8
alexander-vielimchanitsia Sep 9, 2016
5a7ef84
Merge pull request #48 from alexander-vielimchanitsia/master
akhavr Sep 10, 2016
63e9400
bump django
Jul 1, 2020
894d79c
flake ini
Jul 2, 2020
9592ed8
exclude settings/test.py
Jul 2, 2020
cfa0e79
Merge pull request #60 from Elbtalkessel/master
jardev Jul 2, 2020
81d957f
remove check_noqa, add eslint
Oct 3, 2020
1c166ef
linters
Oct 3, 2020
512f4a4
remove extra new line
Oct 3, 2020
cc225cc
run jinjalint on templates dir
Oct 3, 2020
c946565
Merge pull request #61 from Elbtalkessel/more_linters
jardev Oct 5, 2020
982d196
refactor
Feb 18, 2021
692eaf7
fixes
Feb 18, 2021
dbdd8e4
finish frontend setup
Feb 18, 2021
e7c864d
fixes
Feb 18, 2021
1a48cf2
simple docker containers from back and front
Feb 18, 2021
a4cf32b
add reload flag
Feb 18, 2021
766c69b
fixes
Feb 18, 2021
336b23c
more fixes
Feb 18, 2021
99f830b
add mockups
Feb 18, 2021
c84fa7b
small tweaks and cleanup
Feb 19, 2021
ae8f249
Merge pull request #63 from Elbtalkessel/microservices
jardev Apr 23, 2021
3707d79
Merge remote-tracking branch 'kava/master'
Apr 23, 2021
c3fca13
stage container, ability to run app without docker
Apr 23, 2021
4998a52
server build and run
Apr 23, 2021
22f3480
remove supervisor
Apr 23, 2021
f584d46
update comment
Apr 23, 2021
5b71a1f
Merge pull request #64 from Elbtalkessel/deployable-container
jardev Apr 23, 2021
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
29 changes: 29 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
max_line_length = 120
trim_trailing_whitespace = true
indent_style = space
quote_type = single
curly_bracket_next_line = true
spaces_around_operators = true
1indent_brace_style = TBS

[*.html]
indent_size = 4
quote_type = double

[*.py]
indent_size = 4

[Makefile]
indent_style = tab

[*.{js, jsx, vue}]
indent_size = 2

[{package.json, .travis.yml}]
indent_size = 2
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Remove the variable and all commands in Makefile will use docker
USE_LOCAL=1
# If you want deploy app on heroku makefile have "h-deploy" goal, set app name here
HEROKU_APP_NAME=
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 120
exclude = *migrations*,fortytwo_test_task/settings/__init__.py,fortytwo_test_task/settings/test.py
max-complexity = 6
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ build
media
static
*.dat
.env
.idea/
fortytwo/local_settings.py
frontend/node_modules/*
frontend/dist/*
frontend/manifest.json
.coverage
staticroot
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A single container to publish/run your app on a server

FROM node:15-alpine AS frontend
WORKDIR /app
ADD ./frontend /app
RUN yarn install && yarn build

FROM python:3-alpine
WORKDIR /app
ADD . /app
RUN apk update \
&& pip install gunicorn \
&& pip install -r requirements/base.txt \
&& rm -rf /var/cache/apk/*
COPY --from=frontend /app ./frontend
RUN python manage.py collectstatic --noinput

CMD docker/backend-cmd.sh
98 changes: 87 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,94 @@
MANAGE=django-admin.py
SETTINGS=fortytwo_test_task.settings
ifneq (,$(wildcard ./.env))
include .env
export
endif

test:
PYTHONPATH=`pwd` DJANGO_SETTINGS_MODULE=$(SETTINGS) $(MANAGE) test
DOCKER_CMD_PREFIX=./docker/compose.sh
ifeq (,$(USE_LOCAL))
MANAGE=$(DOCKER_CMD_PREFIX) run --rm backend python manage.py
else
MANAGE=python manage.py
endif
TEST_SETTINGS=fortytwo.test_settings
TEST_APP=apps/

# Dev servers
run:
PYTHONPATH=`pwd` DJANGO_SETTINGS_MODULE=$(SETTINGS) $(MANAGE) runserver

syncdb:
PYTHONPATH=`pwd` DJANGO_SETTINGS_MODULE=$(SETTINGS) $(MANAGE) syncdb --noinput
@echo Starting http://127.0.0.1:8000
ifeq (,$(USE_LOCAL))
$(DOCKER_CMD_PREFIX) up
else
(trap 'kill 0' SIGINT; $(MANAGE) runserver & yarn --cwd frontend serve)
endif

# Database
migrate:
PYTHONPATH=`pwd` DJANGO_SETTINGS_MODULE=$(SETTINGS) $(MANAGE) migrate
$(MANAGE) migrate --noinput

migrations:
$(MANAGE) makemigrations

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

initproject: migrate createcachetable

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

cmd=black apps && flake8 apps
lint:
ifeq (,$(USE_LOCAL))
$(DOCKER_CMD_PREFIX) run backend sh -c "$(cmd)"
else
$(cmd)
endif

djangotest:
$(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:
PYTHONPATH=`pwd` DJANGO_SETTINGS_MODULE=$(SETTINGS) $(MANAGE) collectstatic --noinput
.PHONY: test syncdb migrate
@echo Collecting static files
$(MANAGE) collectstatic --noinput

eslint:
ifeq (,$(USE_LOCAL))
$(DOCKER_CMD_PREFIX) run frontend sh -c "yarn --cwd /app/frontend lint src --fix"
else
yarn --cwd frontend lint src --fix
endif

# Deploy
DEPLOY_CONT_ID=fortytwotesttask
build:
ifeq (,$(USE_LOCAL))
docker build --tag=$(DEPLOY_CONT_ID):latest .
else
yarn --cwd frontend install
yarn --cwd frontend build
$(MAKE) collectstatic
endif

server:
ifeq (,$(USE_LOCAL))
docker run -p=8000:8000 --rm --name $(DEPLOY_CONT_ID) $(DEPLOY_CONT_ID):latest
else
./docker/backend-cmd.sh
endif

h-deploy:
ifeq (,$(HEROKU_APP_NAME))
@echo Missing HEROKU_APP_NAME env var
else
@echo Deploying on heroku
heroku container:push web --app $(HEROKU_APP_NAME)
heroku container:release web --app $(HEROKU_APP_NAME)
endif
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
42-test template
===========================

A Django 1.6+ project template
A Django 3+ project template

Use fortytwo_test_task.settings when deploying with getbarista.com
### System requirements
* global: yarn, node (tested on v15), python>=3.7 (tested on 3.9) *OR* docker

### Recomendations
* apps in apps/ folder
* use per-app templates folders
* use per-app static folders
* use migrations
* use settings.local for different environments
* common templates live in templates/
* common static lives in assets/
### Code/deployment requirements
* management commands should be proxied to single word make commands, e.g make test
* app should be deployed on a server of your choice (heroku, pythonanywhere, aws, etc.)

### Initial setup
You can develop app in docker container or directly on host, check `.env.example`
Most usable commands are proxied in the `Makefile`
1 change: 0 additions & 1 deletion apps/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

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)
5 changes: 5 additions & 0 deletions apps/hello/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class HelloConfig(AppConfig):
name = "apps.hello"
Empty file.
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.
6 changes: 0 additions & 6 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.

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

services:
backend:
build:
context: .
dockerfile: docker/backend.Dockerfile
container_name: backend
restart: always
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
gunicorn fortytwo.wsgi -b 0.0.0.0:"${PORT:-8000}" --reload
12 changes: 12 additions & 0 deletions docker/backend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3-alpine

WORKDIR /app
COPY . /app

# gcc, libc-dev are flake8 (typed-ast) deps
RUN apk update \
&& apk add make gcc libc-dev \
&& pip install -r requirements/dev.txt \
&& 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 "$@"
1 change: 1 addition & 0 deletions docker/frontend-cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn --cwd /app/frontend serve
7 changes: 7 additions & 0 deletions docker/frontend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:15-alpine

WORKDIR /app
COPY . /app
RUN yarn --cwd /app/frontend install

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