Skip to content

Commit 68665f1

Browse files
committed
#45 #55 #56 Updated base settings file for django 3, adding python 3 code and typing annotations
1 parent c526e47 commit 68665f1

File tree

3 files changed

+56
-51
lines changed

3 files changed

+56
-51
lines changed

Diff for: src/odm2cvs/cvinterface/views/base_views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def get_initial(self):
256256

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

261261
if not captcha_response:
262262
form.add_error(None, 'You are not human!!')

Diff for: src/odm2cvs/odm2cvs/settings/base.py

+51-46
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""
2-
Django settings for odm2cvs project.
2+
Django settings for djangoProject project.
3+
4+
Generated by 'django-admin startproject' using Django 3.1.2.
35
46
For more information on this file, see
5-
https://docs.djangoproject.com/en/1.7/topics/settings/
7+
https://docs.djangoproject.com/en/3.1/topics/settings/
68
79
For the full list of settings and their values, see
8-
https://docs.djangoproject.com/en/1.7/ref/settings/
10+
https://docs.djangoproject.com/en/3.1/ref/settings/
911
1012
To setup the settings json file:
1113
1. rename settings_template.json to settings.json
@@ -14,67 +16,68 @@
1416
4. Profit!
1517
"""
1618

17-
18-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
19-
import os
19+
import sys
2020
import json
21+
from pathlib import Path
22+
23+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
24+
from typing import Dict, Any, TextIO, List, Tuple, Union
2125

22-
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
26+
BASE_DIR: Path = Path(__file__).resolve().parent.parent
27+
28+
29+
config: Dict[str, Any] = {}
2330

24-
data = {}
2531
try:
26-
with open(os.path.join(BASE_DIR, 'settings', 'settings.json')) as data_file:
27-
data = json.load(data_file)
28-
except IOError:
29-
print("You need to setup the settings data file (see instructions in base.py file.)")
32+
data_file: TextIO
33+
with open(BASE_DIR / 'settings' / 'settings.json') as data_file:
34+
config = json.load(data_file)
35+
except IOError as ioe:
36+
sys.exit('You need to setup the settings data file (see instructions in base.py file.)')
3037

3138

3239
try:
33-
SECRET_KEY = data["secret_key"]
40+
SECRET_KEY: str = config['secret_key']
3441
except KeyError:
35-
print("The secret key is required in the settings.json file.")
36-
exit(1)
42+
print()
43+
exit('The secret key is required in the settings.json file.')
3744

38-
RECAPTCHA_KEY = data["recaptcha_secret_key"] if "recaptcha_secret_key" in data else ""
39-
RECAPTCHA_USER_KEY = data["recaptcha_user_key"] if "recaptcha_user_key" in data else ""
40-
RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
45+
RECAPTCHA_KEY: str = config['recaptcha_secret_key'] if 'recaptcha_secret_key' in config else ''
46+
RECAPTCHA_USER_KEY: str = config['recaptcha_user_key'] if 'recaptcha_user_key' in config else ''
47+
RECAPTCHA_VERIFY_URL: str = 'https://www.google.com/recaptcha/api/siteverify'
4148

42-
ALLOWED_HOSTS = []
49+
ALLOWED_HOSTS: List[str] = []
4350

4451

4552
# Application definition
4653

47-
INSTALLED_APPS = [
54+
INSTALLED_APPS: List[str] = [
4855
'django.contrib.admin',
4956
'django.contrib.auth',
5057
'django.contrib.contenttypes',
5158
'django.contrib.sessions',
5259
'django.contrib.messages',
5360
'django.contrib.staticfiles',
54-
'tastypie',
5561
'cvservices',
5662
'cvinterface',
57-
'rdfserializer',
58-
'widget_tweaks',
5963
]
6064

61-
MIDDLEWARE_CLASSES = [
65+
MIDDLEWARE: List[str] = [
6266
'django.middleware.security.SecurityMiddleware',
6367
'django.contrib.sessions.middleware.SessionMiddleware',
6468
'django.middleware.common.CommonMiddleware',
6569
'django.middleware.csrf.CsrfViewMiddleware',
6670
'django.contrib.auth.middleware.AuthenticationMiddleware',
67-
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
6871
'django.contrib.messages.middleware.MessageMiddleware',
6972
'django.middleware.clickjacking.XFrameOptionsMiddleware',
7073
]
7174

7275
ROOT_URLCONF = 'odm2cvs.urls'
7376

74-
TEMPLATES = [
77+
TEMPLATES: List[Dict[str, Any]] = [
7578
{
7679
'BACKEND': 'django.template.backends.django.DjangoTemplates',
77-
'DIRS': [os.path.join(BASE_DIR, 'templates')]
80+
'DIRS': [BASE_DIR / 'templates']
7881
,
7982
'APP_DIRS': True,
8083
'OPTIONS': {
@@ -88,12 +91,12 @@
8891
},
8992
]
9093

91-
WSGI_APPLICATION = 'odm2cvs.wsgi.application'
94+
WSGI_APPLICATION: str = 'odm2cvs.wsgi.application'
9295

9396

9497
# Databases
95-
DATABASES = {}
96-
for database in data['databases']:
98+
DATABASES: Dict[str, Dict[str: Any]] = {}
99+
for database in config['databases']:
97100
DATABASES[database['name']] = {
98101
'ENGINE': database['engine'],
99102
'NAME': database['schema'],
@@ -105,9 +108,9 @@
105108
}
106109

107110
# Password validation
108-
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
111+
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
109112

110-
AUTH_PASSWORD_VALIDATORS = [
113+
AUTH_PASSWORD_VALIDATORS: List[Dict[str, str]] = [
111114
{
112115
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
113116
},
@@ -124,30 +127,32 @@
124127

125128
# Internationalization
126129

127-
LANGUAGE_CODE = 'en-us'
130+
LANGUAGE_CODE: str = 'en-us'
131+
132+
TIME_ZONE: str = 'UTC'
128133

129-
TIME_ZONE = 'UTC'
134+
USE_I18N: bool = True
130135

131-
USE_I18N = True
136+
USE_L10N: bool = True
132137

133-
USE_L10N = True
138+
USE_TZ: bool = True
134139

135-
USE_TZ = True
140+
SITE_ID: int = 1
136141

137142

138-
TASTYPIE_DEFAULT_FORMATS = ['json']
143+
TASTYPIE_DEFAULT_FORMATS: List[str] = ['json']
139144

140-
API_LIMIT_PER_PAGE = 0
145+
API_LIMIT_PER_PAGE: int = 0
141146

142147

143-
EMAIL_HOST = 'mail.usu.edu'
148+
EMAIL_HOST: str = 'mail.usu.edu'
144149

145-
EMAIL_SENDER = data['email_sender'] if 'email_sender' in data else '',
146-
EMAIL_SENDER = EMAIL_SENDER[0] if isinstance(EMAIL_SENDER, tuple) else EMAIL_SENDER
150+
EMAIL_SENDER: Union[Tuple, str] = config['email_sender'] if 'email_sender' in config else '',
151+
EMAIL_SENDER: str = EMAIL_SENDER[0] if isinstance(EMAIL_SENDER, tuple) else EMAIL_SENDER
147152

148-
EMAIL_RECIPIENTS = list(data['email_recipients']) if 'email_recipients' in data else [],
149-
EMAIL_RECIPIENTS = EMAIL_RECIPIENTS[0] if isinstance(EMAIL_RECIPIENTS, tuple) else EMAIL_RECIPIENTS
153+
EMAIL_RECIPIENTS: Union[Tuple, str] = list(config['email_recipients']) if 'email_recipients' in config else [],
154+
EMAIL_RECIPIENTS: str = EMAIL_RECIPIENTS[0] if isinstance(EMAIL_RECIPIENTS, tuple) else EMAIL_RECIPIENTS
150155

151-
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
156+
EMAIL_BACKEND: str = 'django.core.mail.backends.smtp.EmailBackend'
152157

153-
DATABASE_ROUTERS = ['odm2cvs.db_routers.ControlledVocabularyRouter']
158+
DATABASE_ROUTERS: List[str] = ['odm2cvs.db_routers.ControlledVocabularyRouter']

Diff for: src/odm2cvs/rdfserializer/api.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ def to_skos(self, data, options=None):
215215
SKOS['inScheme'], URIRef(scheme.uri))))
216216

217217
# Add labels to each concept class.
218-
for x in concept.data:
219-
label = concept.data[x]
218+
for x in concept.config:
219+
label = concept.config[x]
220220
if isinstance(label, type(None)):
221221
label = ''
222222
if isinstance(label, int):
@@ -263,8 +263,8 @@ def to_skos(self, data, options=None):
263263
SKOS['inScheme'], URIRef(scheme.uri))))
264264

265265
# Add labels within concept class.
266-
for field in data.data.keys():
267-
label = data.data[field]
266+
for field in data.config.keys():
267+
label = data.config[field]
268268
if isinstance(label, type(None)):
269269
label = ''
270270
if isinstance(label, int):

0 commit comments

Comments
 (0)