Skip to content

Commit

Permalink
Merge pull request #173 from fga-eps-mds/release/v0.2.0
Browse files Browse the repository at this point in the history
Release/v0.2.0
  • Loading branch information
vitorcx authored Nov 12, 2019
2 parents 898efad + 4e717e3 commit 64067d6
Show file tree
Hide file tree
Showing 57 changed files with 2,173 additions and 214 deletions.
6 changes: 0 additions & 6 deletions .env

This file was deleted.

6 changes: 6 additions & 0 deletions .env-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DATABASE_USER=postgres
DATABASE_PASSWORD=
DATABASE_NAME=postgres
DATABASE_HOST=db
DATABASE_PORT=5432
HOST=http://localhost:8000/
7 changes: 7 additions & 0 deletions .env-staging
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

DATABASE_USER=${{secrets.DATABASE_USER}}
DATABASE_PASSWORD=${{secrets.DATABASE_PASSWORD}}
DATABASE_NAME=${{secrets.DATABASE_NAME}}
DATABASE_HOST=${{secrets.DATABASE_HOST}}
DATABASE_PORT=5432
PORT=8000
30 changes: 30 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CD

on:
push:
branches:
- develop

jobs:
deploy:
runs-on: ubuntu-latest
env:
HEROKU_API_KEY: ${{secrets.HEROKU_API_KEY}}
steps:
- uses: actions/checkout@master
- name: setup environment
run: docker build -t acacia-back-heroku .
- name: Run container
run: docker run -d -p 8000:8000 --env-file .env-staging acacia-back-heroku
- name: heroku container push
uses: actions/heroku@master
with:
args: "container:login"
- name: heroku container push
uses: actions/heroku@master
with:
args: "container:push web -a acacia-backend-staging"
- name: heroku container release
uses: actions/heroku@master
with:
args: "container:release web -a acacia-backend-staging"
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup enviroment
run: docker-compose up -d --build
- name: Run tests
run: docker exec acacia_backend bash -c "cd src/ && coverage run manage.py test && coverage xml -o cov.xml"
run: docker exec acacia_backend bash -c "coverage run manage.py test && coverage xml -o cov.xml"
- name: Upload coverage to Codecov
uses: codecov/[email protected]
with:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: style

on: push

jobs:
style-check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@master
- name: Setup enviroment
run: docker-compose up -d --build
- name: Run linter
run: docker exec acacia_backend bash -c "flake8"
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ db.sqlite3
media
.vscode/
env/
migrations/
.env-staging
pictures/
# Ignore automatic migrations
src/*/migrations/*_auto_*.py
src/*/migrations/*_merge_*.py
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
WORKDIR /code/src
CMD python manage.py runserver 0.0.0.0:$PORT
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ services:
build:
context: .
dockerfile: Dockerfile
env_file: .env-dev
command: bash -c "
cd src/ &&
python manage.py makemigrations &&
python manage.py migrate &&
chmod +x scripts/create_superuser.sh &&
./scripts/create_superuser.sh &&
python manage.py runserver 0.0.0.0:8080"
python manage.py runserver 0.0.0.0:8000"
ports:
- "8080:8080"
- "8000:8000"
volumes:
- .:/code
depends_on:
Expand Down
8 changes: 6 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ django-phonenumber-field==3.0
phonenumbers==8.10
psycopg2==2.8.3
django-cors-headers==3.1.0
djangorestframework-simplejwt
coverage
flake8==3.7.8
djangorestframework-simplejwt==4.3.0
coverage==4.5.4
django-localflavor==2.2
pillow
whitenoise
35 changes: 35 additions & 0 deletions src/acacia/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
from django.urls import path
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny


def list_all_endpoints(urlpatterns, app_name=None):

class ListAllEndpoints(APIView):
permission_classes = (AllowAny,)

def get(self, request, format=None):
link = os.environ.get('HOST')
endpoints = {}

for url in urlpatterns:
url = url.pattern._route[:-1]

if url:

if app_name:
endpoints[url] = f"{link}{app_name}/{url}/"

else:
endpoints[url] = f"{link}{url}/"

return Response(endpoints, status=status.HTTP_200_OK)

urlpatterns += [
path('', ListAllEndpoints.as_view()),
]

return urlpatterns
59 changes: 43 additions & 16 deletions src/acacia/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
"""

import os
from scripts.wait_for_db import start_services
from django.utils.translation import ugettext_lazy as _

from .wait_db import start_services
from datetime import timedelta

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -29,29 +28,36 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['0.0.0.0']
ALLOWED_HOSTS = ['0.0.0.0', 'localhost', 'acacia-backend-staging.herokuapp.com']

AUTH_USER_MODEL = 'users.User'

# Application definition
INSTALLED_APPS = [
DEFAULT_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

# libs
THIRD_PARTY_APPS = [
'phonenumber_field',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
]

# my apps
LOCAL_APPS = [
'users',
'harvest',
'property',
'tree'
]

INSTALLED_APPS = DEFAULT_APPS + THIRD_PARTY_APPS + LOCAL_APPS

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -62,8 +68,11 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

ROOT_URLCONF = 'acacia.urls'

TEMPLATES = [
Expand All @@ -88,10 +97,16 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': '5432',
'NAME': os.environ.get('DATABASE_NAME'),
'USER': os.environ.get('DATABASE_USER'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': os.environ.get('DATABASE_HOST'),
'PORT': os.environ.get('DATABASE_PORT'),
},

'sqlite': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

Expand Down Expand Up @@ -140,21 +155,33 @@

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],

'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
}

# CORS headers to responses

CORS_ORIGIN_WHITELIST = [
"http://localhost:8080",
"http://localhost:8000",
"http://localhost:8080",
"http://0.0.0.0:8080",
"http://0.0.0.0:8000",
"http://localhost:8080",
"https://acacia-staging.herokuapp.com",
]

SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=5),
'REFRESH_TOKEN_LIFETIME': timedelta(days=10),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
}
24 changes: 8 additions & 16 deletions src/acacia/urls.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
"""acacia URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from .helpers import list_all_endpoints


urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
]
path('harvests/', include('harvest.urls')),
path('properties/', include('property.urls')),
]


urlpatterns = list_all_endpoints(urlpatterns)
19 changes: 9 additions & 10 deletions src/acacia/wait_db.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import importlib
import os
import time

import os
import logging

SERVICES_STARTED = False
Expand All @@ -21,14 +20,14 @@ def start_services():


def start_postgres():
# settings_path = os.environ['DJANGO_SETTINGS_MODULE']
# settings = importlib.import_module(settings_path)

# db = settings.DATABASES['default']
dbname = 'postgres' # db['NAME']
user = 'postgres' # db['USER']
password = '' # db['PASSWORD']
host = 'db' # db['HOST']
settings_path = os.environ['DJANGO_SETTINGS_MODULE']
settings = importlib.import_module(settings_path)

db = settings.DATABASES['default']
dbname = db['NAME']
user = db['USER']
password = db['PASSWORD']
host = db['HOST']

for _ in range(100):
if can_connect(dbname, user, password, host):
Expand Down
15 changes: 15 additions & 0 deletions src/harvest/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib import admin
from .models import Harvest, RulesHarvest


class HarvestAdmin(admin.ModelAdmin):
list_display = (
'date',
'description',
'status',
'equipment',
)


admin.site.register(Harvest)
admin.site.register(RulesHarvest)
5 changes: 5 additions & 0 deletions src/harvest/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class HarvestConfig(AppConfig):
name = 'harvest'
Loading

0 comments on commit 64067d6

Please sign in to comment.