Skip to content

Commit

Permalink
Merge a1235e5 into e76f335
Browse files Browse the repository at this point in the history
  • Loading branch information
Estaer committed Aug 31, 2018
2 parents e76f335 + a1235e5 commit 995ef65
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 57 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ install:
- pip install coveralls

script:
- python manage.py test
- coverage run --source=authors/ manage.py test
- coverage report -m
- coveralls
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python manage.py collectstatic --noinput; gunicorn authors.wsgi
84 changes: 61 additions & 23 deletions authors/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
"""

import os
import sys



import re

# 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 @@ -23,15 +20,13 @@
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY")
SECRET_KEY = os.environ.get("SECRET_KEY", None)


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True



ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']

# Application definition

Expand Down Expand Up @@ -81,11 +76,6 @@

WSGI_APPLICATION = 'authors.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases



# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -140,16 +130,64 @@
'DEFAULT_AUTHENTICATION_CLASSES': (
#'authors.apps.authentication.backends.JWTAuthentication',
),

}

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'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', ''),
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

ENV = os.environ.get("ENVIRONMENT", None)

print(ENV)

if not ENV:
raise Exception("ENV variable `ENVIRONMENT` not set: Please set it before running the application.")

if ENV.lower().startswith == "p":
from authors.settings.production import *

elif ENV.lower().startswith == "t":
from authors.settings.testing import *

else:
from authors.settings.development import *

if not DATABASE_URL:
raise Exception("ENV variable `DATABASE_URL` not set: Please set it before running the application.")

DATABASES = {}

if re.match("^postgres://\w+:\w+@.+(:\d{4})?/\w+$", DATABASE_URL):

array = DATABASE_URL.split('//')[1]

data = array.split(':')

USER = data[0]

PASSWORD = data[1].split('@')[0]
HOST = data[1].split('@')[1]

PORT = data[2].split('/')[0] if len(data) > 1 and data[2] else ""

DB_NAME = data[2].split('/')[1]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': USER,
'PASSWORD': PASSWORD,
'HOST': HOST,
'PORT': PORT,
}
}
}

else:
raise Exception("ENV variable `DATABASE_URL` does not match the pattern: "
"(`postgres://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name>`).")

if not DATABASES:
raise Exception("DATABASES settings have not been configured.")
9 changes: 3 additions & 6 deletions authors/settings/development.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from authors.settings.base import *

# override base.py here

DEBUG = True
ALLOWED_HOSTS= ['*']
import os

DATABASE_URL = os.environ.get("DATABASE_URL_DEVELOPMENT", None)
print("development");
11 changes: 3 additions & 8 deletions authors/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from authors.settings.base import *
import dj_database_url

# override base.py here
DEBUG = False
ALLOWED_HOSTS= ['*']
POSTGRES_URL = os.environ.get("DATABASE_URL")
DATABASES['default'] = dj_database_url.parse(POSTGRES_URL, conn_max_age=600)
import os

DATABASE_URL = os.environ.get("DATABASE_URL_PRODUCTION", None)
print("production")
11 changes: 0 additions & 11 deletions authors/settings/staging.py

This file was deleted.

10 changes: 3 additions & 7 deletions authors/settings/testing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from authors.settings.base import *
import os

# override base.py here

DEBUG = True
ALLOWED_HOSTS = ['*']
DATABASES['default']['NAME'] = os.environ.get("DATABASE_TEST_NAME")

DATABASE_URL = os.environ.get("DATABASE_URL_TESTING", None)
print("testing")
print(DATABASE_URL)
4 changes: 4 additions & 0 deletions authors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path

from authors.settings.base import STATIC_URL, STATIC_ROOT

urlpatterns = [
path('admin/', admin.site.urls),

path('api/', include(('authors.apps.authentication.urls', 'authentication'), namespace='authentication')),
]

urlpatterns += static(STATIC_URL, document_root=STATIC_ROOT)
2 changes: 1 addition & 1 deletion authors/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authors.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authors.settings.base")

application = get_wsgi_application()
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authors.settings.development")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authors.settings.base")
try:
from django.core.management import execute_from_command_line
except ImportError:
Expand Down
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
atomicwrites==1.2.0
attrs==18.1.0
dj-database-url==0.5.0
Django==2.1
django-cors-headers==2.4.0
django-cors-middleware==1.3.1
django-extensions==2.1.2
djangorestframework==3.8.2
djangorestframework-jwt==1.11.0
gunicorn==19.9.0
more-itertools==4.3.0
pluggy==0.7.1
psycopg2==2.7.5
psycopg2-binary==2.7.5
py==1.6.0
PyJWT==1.6.4
pytest==3.7.3
pytest-django==3.4.2
pytz==2018.5
six==1.11.0
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.6.6

0 comments on commit 995ef65

Please sign in to comment.