django-gallery/gallery/utility.py
David Todd 51a117ca5f
Add configuration to connect to a postgres database (#2)
CircleCI's free service only allows running 1 container at a time.
This causes problems if I want to use a database.
A workaround that I found seems to work by running the database server directly within the test container. This does increase test time however
2018-03-08 21:25:54 -06:00

30 lines
1.2 KiB
Python

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)