From 15ba3bbdec2163ad5280f6fc196bb7fe3dae9f43 Mon Sep 17 00:00:00 2001 From: Clinton Blackburn Date: Tue, 22 Jan 2019 20:03:28 -0800 Subject: [PATCH] Cleaned up settings - Removed unneeded settings - Restored logging - Cleaned up some imports --- .../templates/dailyIncid/getQuery.html | 4 - showCrime/dailyIncid/views.py | 46 +-- showCrime/showCrime/settings.py | 347 ++++++++---------- 3 files changed, 160 insertions(+), 237 deletions(-) diff --git a/showCrime/dailyIncid/templates/dailyIncid/getQuery.html b/showCrime/dailyIncid/templates/dailyIncid/getQuery.html index bb8c5b0..1be1ebe 100755 --- a/showCrime/dailyIncid/templates/dailyIncid/getQuery.html +++ b/showCrime/dailyIncid/templates/dailyIncid/getQuery.html @@ -37,11 +37,7 @@

Related resources

-
-

-

{{siteUrl}}

{% endblock %} diff --git a/showCrime/dailyIncid/views.py b/showCrime/dailyIncid/views.py index f548822..27b7e25 100755 --- a/showCrime/dailyIncid/views.py +++ b/showCrime/dailyIncid/views.py @@ -1,35 +1,20 @@ -from django.http import HttpResponse, HttpResponseRedirect -from django.shortcuts import get_object_or_404, render -# django 2.0 -# from django.core.urlresolvers import reverse -from django.urls import reverse - -from django.db.models import Min, Max, Q -from django.db.models.lookups import IExact -# from django.db import transaction -# from django.db import IntegrityError -from django.template import Context - -from django.contrib.gis.geos import Polygon -# from django.contrib import admin -# from django.contrib.gis.admin import GeoModelAdmin -from django.contrib.gis.measure import D - -from rest_framework import generics # viewsets - -# from datetime import datetime, timedelta - import logging -import pytz import random -import geojson - -from showCrime.settings import PLOT_PATH, SITE_URL +import geojson +import pytz +from django.conf import settings +from django.contrib.gis.geos import Polygon +from django.contrib.gis.measure import D +from django.db.models import Max, Min, Q +from django.http import HttpResponse, HttpResponseRedirect +from django.shortcuts import render +from rest_framework import generics +from dailyIncid import serializers from .forms import * from .models import * -from dailyIncid import serializers + def awareDT(naiveDT): utc=pytz.UTC @@ -72,9 +57,8 @@ def getQuery(request): logger.info('user=%s getQuery-nonPost' % (userName)) qform = twoTypeQ() - return render(request, 'dailyIncid/getQuery.html', {'form': qform, 'siteUrl': SITE_URL}) + return render(request, 'dailyIncid/getQuery.html', {'form': qform}) -import os import matplotlib @@ -86,8 +70,6 @@ def getQuery(request): import pylab as p from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas -from matplotlib.figure import Figure -from matplotlib.dates import DateFormatter from datetime import datetime,timedelta,date import matplotlib.dates as mdates @@ -270,7 +252,7 @@ def plotResults(request,beat,crimeCat,crimeCat2=None): f1.autofmt_xdate() figDPI=200 - fullPath = PLOT_PATH+fname+'_'+runTime+'.png' + fullPath = settings.PLOT_PATH+fname+'_'+runTime+'.png' logger.info('user=%s plotting %d/%d (%6.2f sec) to %s' % (userName,totBeat,totCity,qryTime.total_seconds(),fullPath)) # 2do: 181218 fix plot file permission @@ -298,9 +280,7 @@ def otherUtil(request): ## GeoDjango -from django.contrib.gis.geos import GEOSGeometry from django.contrib.gis.geos import Point -from django.contrib.gis.utils import LayerMapping @login_required diff --git a/showCrime/showCrime/settings.py b/showCrime/showCrime/settings.py index 2731e7e..e7eca1c 100644 --- a/showCrime/showCrime/settings.py +++ b/showCrime/showCrime/settings.py @@ -1,21 +1,10 @@ -""" -django app for https://OakCrime.org -""" - -__author__ = "rik@electronicArtifacts.com" -__credits__ = ["clinton.blackburn@gmail.com","actionspeakslouder@gmail.com"] -__date__ = "181217" -__version__ = "0.3" +import os import environ -# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -root = environ.Path(__file__) - 2 +project_root = environ.Path(__file__) - 2 env = environ.Env(DEBUG=(bool, False), ) -import os - - # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = env('SECRET_KEY') @@ -29,71 +18,58 @@ # Application definition -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', +DJANGO_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', 'django.contrib.sites', - 'django.contrib.messages', - 'django.contrib.staticfiles', + 'django.contrib.messages', + 'django.contrib.staticfiles', 'django.contrib.flatpages', - 'django.contrib.gis', - 'rest_framework', - 'dailyIncid', + 'django.contrib.gis', ] -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware', +THIRD_PARTY_APPS = [ + 'rest_framework', ] -# PUBLIC_ROOT = env('PUBLIC_ROOT') -MEDIA_ROOT = os.path.join(root, "media") -MEDIA_URL = '/media/' -STATIC_ROOT = root("static") -STATIC_URL = '/static/' - -if DEBUG: - INSTALLED_APPS.append('debug_toolbar') +LOCAL_APPS = [ + 'dailyIncid', +] - # The order of MIDDLEWARE_CLASSES is important. You should include the - # Debug Toolbar middleware as early as possible in the list. However, it - # must come after any other middleware that encodes the response's - # content, such as GZipMiddleware. - MIDDLEWARE.insert(0,'debug_toolbar.middleware.DebugToolbarMiddleware') - STATICFILES_DIRS = [ ] - INTERNAL_IPS = ['127.0.0.1' ] # for debug_toolbar -else: - STATICFILES_DIRS = [ env('STATICFILES_DIRS'), ] - INTERNAL_IPS = [] +INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware', +] ROOT_URLCONF = 'showCrime.urls' TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(root, 'templates')], - - # not allowed to have APP_DIRS true when using explicit loaders - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + str(project_root.path('templates')), + ], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', 'django.template.context_processors.i18n', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, + 'django.contrib.messages.context_processors.messages', + ], + }, + }, ] WSGI_APPLICATION = 'showCrime.wsgi.application' @@ -102,32 +78,31 @@ # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { - 'default': { - # LOCAL DATABASE - 'ENGINE': 'django.contrib.gis.db.backends.postgis', - 'NAME': env('DB_NAME'), - 'USER': env('DB_USER'), - 'PASSWORD': env('DB_PW'), - 'HOST': env('DB_HOST') - }, + 'default': env.db(), } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + +PASSWORD_HASHERS = [ + 'django.contrib.auth.hashers.Argon2PasswordHasher', + 'django.contrib.auth.hashers.PBKDF2PasswordHasher', + 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', ] # Internationalization @@ -141,15 +116,20 @@ USE_L10N = True -# https://docs.djangoproject.com/en/2.1/topics/i18n/timezones/#database -# ... if you’re using PostgreSQL, you can switch between USE_TZ = False and USE_TZ = True freely. -# The database connection’s time zone will be set to TIME_ZONE or UTC respectively, so that Django obtains correct datetimes in all cases. -# You don’t need to perform any data conversions USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ +PUBLIC_ROOT = env('PUBLIC_ROOT', cast=str, default=str(project_root.path('public'))) +MEDIA_ROOT = str(environ.Path(PUBLIC_ROOT).path('media')) +MEDIA_URL = '/media/' +STATIC_ROOT = str(environ.Path(PUBLIC_ROOT).path('static')) +STATIC_URL = '/static/' + +STATICFILES_DIRS = [ + str(project_root.path('static')), +] SITE_ID = 1 @@ -157,123 +137,90 @@ 'default': env.cache(default='locmemcache://showCrime'), } -# 2do: enable logging, but excluding INFO from boxsdk, ... - -# def generate_file_handler(filename): -# """ Generates a logging handler that writes to a file. -# -# If the `ENABLE_LOGGING_TO_FILE` setting is `False`, `logging.NullHandler` will be used instead -# of `logging.FileHandler`. -# -# Args: -# filename (str): Name of the file to which logs are written. -# -# Returns: -# dict -# """ -# handler = { -# 'level': 'INFO', -# 'formatter': 'standard', -# } -# if ENABLE_LOGGING_TO_FILE: -# handler.update({ -# 'class': 'logging.FileHandler', -# 'filename': environ.Path(LOG_FILE_PATH).path(filename), -# }) -# else: -# handler['class'] = 'logging.NullHandler' -# -# return handler -# -# -# LOG_FILE_PATH = os.path.join(root, "logs") -# ENABLE_LOGGING_TO_FILE = env('ENABLE_LOGGING_TO_FILE', cast=bool, default=True) -# LOGGING = { -# 'version': 1, -# 'formatters': { -# 'standard': { -# 'format': '%(asctime)s %(levelname)s %(process)d %(pathname)s:%(lineno)d - %(message)s', -# }, -# }, -# 'handlers': { -# 'console': { -# 'level': 'INFO', -# 'class': 'logging.StreamHandler', -# 'formatter': 'standard' -# }, -# 'file_app': generate_file_handler('app.log'), -# 'null': { -# 'class': 'logging.NullHandler', -# } -# }, -# 'loggers': { -# '': { -# 'handlers': ['file_app', 'console', ], -# 'level': 'INFO', -# 'propagate': True, -# }, -# }, -# 'dailyIncid': { -# 'handlers': ['console', 'mail_admins'], -# 'level': 'INFO', -# } -# } -REST_FRAMEWORK = { - 'DEFAULT_PERMISSION_CLASSES': [ - 'rest_framework.permissions.IsAuthenticated', # 'rest_framework.permissions.IsAdminUser', - ], - 'PAGE_SIZE': 10, - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', +def generate_file_handler(filename): + """ Generates a logging handler that writes to a file. + + If the `ENABLE_LOGGING_TO_FILE` setting is `False`, `logging.NullHandler` will be used instead + of `logging.FileHandler`. + + Args: + filename (str): Name of the file to which logs are written. + + Returns: + dict + """ + handler = { + 'level': 'INFO', + 'formatter': 'standard', + } + if ENABLE_LOGGING_TO_FILE: + handler.update({ + 'class': 'logging.FileHandler', + 'filename': environ.Path(LOG_FILE_PATH).path(filename), + }) + else: + handler['class'] = 'logging.NullHandler' + + return handler + + +LOG_FILE_PATH = env('LOG_FILE_PATH', cast=str, default=str(project_root.path('logs'))) +ENABLE_LOGGING_TO_FILE = env('ENABLE_LOGGING_TO_FILE', cast=bool, default=False) +LOGGING = { + 'version': 1, + 'formatters': { + 'standard': { + 'format': '%(asctime)s %(levelname)s %(process)d %(pathname)s:%(lineno)d - %(message)s', + }, + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'standard', + }, + 'file_app': generate_file_handler('app.log'), + 'null': { + 'class': 'logging.NullHandler', + } + }, + 'loggers': { + '': { + 'handlers': ['file_app', 'console', ], + 'level': 'INFO', + 'propagate': True, + }, + 'boxsdk': { + 'handlers': ['file_app', 'console'], + 'level': 'WARNING', + 'propagate': True, + }, + 'django': { + 'handlers': ['console'], + 'propagate': True, + }, + }, } +REST_FRAMEWORK = { + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.IsAuthenticated', + ], + 'PAGE_SIZE': 10, + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', +} LEAFLET_CONFIG = { - 'DEFAULT_CENTER': (52.00,20.00), - 'DEFAULT_ZOOM': 6, - 'MIN_ZOOM': 1, - 'MAX_ZOOM': 20, + 'DEFAULT_CENTER': (52.00, 20.00), + 'DEFAULT_ZOOM': 6, + 'MIN_ZOOM': 1, + 'MAX_ZOOM': 20, } -CRON_CLASSES = [ - "dailyIncid.cron.HarvestSocrataJob", - 'django_cron.cron.FailedRunsNotificationCronJob', -] +PLOT_PATH = os.path.join(project_root, 'plots') -# Email config, ala https://docs.webfaction.com/software/django/getting-started.html?highlight=django%2520email#configuring-django-to-send-email-messages -ADMIN = ((env('ADMIN_USER'),env('ADMIN_EMAIL'))) -EMAIL_HOST = env('EMAIL_HOST') -EMAIL_HOST_USER = env('EMAIL_HOST_USER') -EMAIL_HOST_PASSWORD = env('EMAIL_PW') +# Email configuration SERVER_EMAIL = env('SERVER_EMAIL') -SITE_URL = env('SITE_URL') - -DEBUG_TOOLBAR_PATCH_SETTINGS = False -# disables DEBUG_TOOLBAR -# DEBUG_TOOLBAR_CONFIG = { -# 'SHOW_TOOLBAR_CALLBACK': lambda r: False, } - -LOG_FILE_PATH = os.path.join(root, "logs") -PLOT_PATH = os.path.join(root, "plots") - -################### -# echo settings - -import django -print('settings: django version',django.__version__) -print('settings: DEBUG',DEBUG) - -import socket -HostName = socket.gethostname() -print('settings: HostName', HostName) -print('settings: root', root) -print('settings: STATIC_URL', STATIC_URL) -print('settings: SITE_URL', SITE_URL) -print('settings: STATICFILES_DIRS', STATICFILES_DIRS) -print('settings: MEDIA_ROOT', MEDIA_ROOT) -print('settings: LOG_FILE_PATH', LOG_FILE_PATH) -print('settings: PLOT_PATH', PLOT_PATH) -print('settings: database hosted at %s:%s' % (DATABASES['default']["HOST"],DATABASES['default']["NAME"])) -print('settings: DEBUG',DEBUG) - - +EMAIL_CONFIG = env.email_url('EMAIL_URL') +vars().update(EMAIL_CONFIG)