Add configuration to connect to a postgres database

This commit is contained in:
c0de 2018-03-08 20:02:11 -06:00
parent 14945df19b
commit 21cf5da2ab
3 changed files with 90 additions and 5 deletions

27
gallery/gzip.py Normal file
View File

@ -0,0 +1,27 @@
import logging
from StringIO import StringIO
import zlib
class UnzipRequestMiddleware(object):
"""A middleware that unzips POSTed data.
For this middleware to kick in, the client must provide a value
for the ``Content-Encoding`` header. The only accepted value is
``gzip``. Any other value is ignored.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
encoding = environ.get('HTTP_CONTENT_ENCODING')
if encoding == 'gzip':
data = environ['wsgi.input'].read()
try:
uncompressed = zlib.decompress(data, 31)
environ['wsgi.input'] = StringIO(uncompressed)
environ['CONTENT_LENGTH'] = len(uncompressed)
except zlib.error:
logging.warning(u"Could not decompress request data.", exc_info=True)
environ['wsgi.input'] = StringIO(data)
return self.app(environ, start_response)

View File

@ -15,7 +15,6 @@ import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
@ -23,10 +22,21 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '^2)m5jqumpm6-1io$18tjwo@dnukf=r-kl#8&q1#qf!&(&y_6h'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = os.environ.get('PORTAL_DEBUG', 'false').lower() == 'true'
ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']
if DEBUG:
INTERNAL_IPS = ('localhost',)
# SMTP server/relay to send error emails (to ADMINS) from
ADMINS = [('c0de', 'c0de@c0defox.es')]
SERVER_EMAIL = 'portal@imagehost'
EMAIL_HOST = ''
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
# Application definition
@ -37,9 +47,21 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'inline_actions',
]
if DEBUG:
INSTALLED_APPS += [
'debug_panel'
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'PAGE_SIZE': 10
}
MIDDLEWARE = [
'django.middleware.gzip.GZipMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
@ -48,6 +70,9 @@ MIDDLEWARE = [
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
if DEBUG:
MIDDLEWARE[2:0] = [
'debug_panel.middleware.DebugPanelMiddleware']
ROOT_URLCONF = 'gallery.urls'
@ -75,8 +100,12 @@ WSGI_APPLICATION = 'gallery.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'imagehost_db',
'USER': 'imagehost',
'PASSWORD': 'totallyHacked1337',
'HOST': 'postgres',
'PORT': '',
}
}

29
gallery/utility.py Normal file
View File

@ -0,0 +1,29 @@
from django.http import HttpResponse
from django.template import loader
from django.contrib import auth
# Usage:
# template - relative path to the template you wish to render
# request - The request object for the user
# message - An optional message to me shown to the user, set to None or ''
# context - A dictionary of arbitrary key-value combos for use in the App
def _ResponseTemplate(template, request, message='', context=None):
if isinstance(context, dict) and \
not (context.get('message', False)):
context['message'] = str(message)
elif isinstance(context, dict) and \
(context.get('message', False)):
context['message'] += ' -- ' + str(message)
else:
context = {
'message': str(message)
}
template = loader.get_template(str(template))
return HttpResponse(template.render(context, request))
# Logs out a user, sending them to the login screen with an optional message and context
# Same arguments as _ResponseTemplate, except for the template
def _ForceLogout(request, message='', context=None):
auth.logout(request)
return _ResponseTemplate('account/login.html', request, message, context)