Skip to content

Commit

Permalink
#45 #55 #56 Updated base settings file for django 3, adding python 3 …
Browse files Browse the repository at this point in the history
…code and typing annotations
  • Loading branch information
jcaraballo17 committed Oct 28, 2020
1 parent c526e47 commit 68665f1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/odm2cvs/cvinterface/views/base_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def get_initial(self):

def is_captcha_valid(self, form):
url = settings.RECAPTCHA_VERIFY_URL
captcha_response = form.data.get('g-recaptcha-response')
captcha_response = form.config.get('g-recaptcha-response')

if not captcha_response:
form.add_error(None, 'You are not human!!')
Expand Down
97 changes: 51 additions & 46 deletions src/odm2cvs/odm2cvs/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""
Django settings for odm2cvs project.
Django settings for djangoProject project.
Generated by 'django-admin startproject' using Django 3.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
https://docs.djangoproject.com/en/3.1/ref/settings/
To setup the settings json file:
1. rename settings_template.json to settings.json
Expand All @@ -14,67 +16,68 @@
4. Profit!
"""


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import sys
import json
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
from typing import Dict, Any, TextIO, List, Tuple, Union

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR: Path = Path(__file__).resolve().parent.parent


config: Dict[str, Any] = {}

data = {}
try:
with open(os.path.join(BASE_DIR, 'settings', 'settings.json')) as data_file:
data = json.load(data_file)
except IOError:
print("You need to setup the settings data file (see instructions in base.py file.)")
data_file: TextIO
with open(BASE_DIR / 'settings' / 'settings.json') as data_file:
config = json.load(data_file)
except IOError as ioe:
sys.exit('You need to setup the settings data file (see instructions in base.py file.)')


try:
SECRET_KEY = data["secret_key"]
SECRET_KEY: str = config['secret_key']
except KeyError:
print("The secret key is required in the settings.json file.")
exit(1)
print()
exit('The secret key is required in the settings.json file.')

RECAPTCHA_KEY = data["recaptcha_secret_key"] if "recaptcha_secret_key" in data else ""
RECAPTCHA_USER_KEY = data["recaptcha_user_key"] if "recaptcha_user_key" in data else ""
RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
RECAPTCHA_KEY: str = config['recaptcha_secret_key'] if 'recaptcha_secret_key' in config else ''
RECAPTCHA_USER_KEY: str = config['recaptcha_user_key'] if 'recaptcha_user_key' in config else ''
RECAPTCHA_VERIFY_URL: str = 'https://www.google.com/recaptcha/api/siteverify'

ALLOWED_HOSTS = []
ALLOWED_HOSTS: List[str] = []


# Application definition

INSTALLED_APPS = [
INSTALLED_APPS: List[str] = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tastypie',
'cvservices',
'cvinterface',
'rdfserializer',
'widget_tweaks',
]

MIDDLEWARE_CLASSES = [
MIDDLEWARE: List[str] = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'odm2cvs.urls'

TEMPLATES = [
TEMPLATES: List[Dict[str, Any]] = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
Expand All @@ -88,12 +91,12 @@
},
]

WSGI_APPLICATION = 'odm2cvs.wsgi.application'
WSGI_APPLICATION: str = 'odm2cvs.wsgi.application'


# Databases
DATABASES = {}
for database in data['databases']:
DATABASES: Dict[str, Dict[str: Any]] = {}
for database in config['databases']:
DATABASES[database['name']] = {
'ENGINE': database['engine'],
'NAME': database['schema'],
Expand All @@ -105,9 +108,9 @@
}

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

AUTH_PASSWORD_VALIDATORS = [
AUTH_PASSWORD_VALIDATORS: List[Dict[str, str]] = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
Expand All @@ -124,30 +127,32 @@

# Internationalization

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE: str = 'en-us'

TIME_ZONE: str = 'UTC'

TIME_ZONE = 'UTC'
USE_I18N: bool = True

USE_I18N = True
USE_L10N: bool = True

USE_L10N = True
USE_TZ: bool = True

USE_TZ = True
SITE_ID: int = 1


TASTYPIE_DEFAULT_FORMATS = ['json']
TASTYPIE_DEFAULT_FORMATS: List[str] = ['json']

API_LIMIT_PER_PAGE = 0
API_LIMIT_PER_PAGE: int = 0


EMAIL_HOST = 'mail.usu.edu'
EMAIL_HOST: str = 'mail.usu.edu'

EMAIL_SENDER = data['email_sender'] if 'email_sender' in data else '',
EMAIL_SENDER = EMAIL_SENDER[0] if isinstance(EMAIL_SENDER, tuple) else EMAIL_SENDER
EMAIL_SENDER: Union[Tuple, str] = config['email_sender'] if 'email_sender' in config else '',
EMAIL_SENDER: str = EMAIL_SENDER[0] if isinstance(EMAIL_SENDER, tuple) else EMAIL_SENDER

EMAIL_RECIPIENTS = list(data['email_recipients']) if 'email_recipients' in data else [],
EMAIL_RECIPIENTS = EMAIL_RECIPIENTS[0] if isinstance(EMAIL_RECIPIENTS, tuple) else EMAIL_RECIPIENTS
EMAIL_RECIPIENTS: Union[Tuple, str] = list(config['email_recipients']) if 'email_recipients' in config else [],
EMAIL_RECIPIENTS: str = EMAIL_RECIPIENTS[0] if isinstance(EMAIL_RECIPIENTS, tuple) else EMAIL_RECIPIENTS

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_BACKEND: str = 'django.core.mail.backends.smtp.EmailBackend'

DATABASE_ROUTERS = ['odm2cvs.db_routers.ControlledVocabularyRouter']
DATABASE_ROUTERS: List[str] = ['odm2cvs.db_routers.ControlledVocabularyRouter']
8 changes: 4 additions & 4 deletions src/odm2cvs/rdfserializer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def to_skos(self, data, options=None):
SKOS['inScheme'], URIRef(scheme.uri))))

# Add labels to each concept class.
for x in concept.data:
label = concept.data[x]
for x in concept.config:
label = concept.config[x]
if isinstance(label, type(None)):
label = ''
if isinstance(label, int):
Expand Down Expand Up @@ -263,8 +263,8 @@ def to_skos(self, data, options=None):
SKOS['inScheme'], URIRef(scheme.uri))))

# Add labels within concept class.
for field in data.data.keys():
label = data.data[field]
for field in data.config.keys():
label = data.config[field]
if isinstance(label, type(None)):
label = ''
if isinstance(label, int):
Expand Down

0 comments on commit 68665f1

Please sign in to comment.