From 0e1a08683b888606fb9566113cbaff41c6b65d39 Mon Sep 17 00:00:00 2001 From: Kyle Blanker Date: Tue, 12 Sep 2017 14:46:33 -0400 Subject: Fixed up login page, added auth middleware, adding company crud operations --- app/app/settings.py | 4 ++ app/app/urls.py | 2 +- app/dispatch/middleware.py | 31 ++++++++++ app/dispatch/templates/dispatch/base.html | 30 ++++++++-- .../templates/dispatch/companies/create.html | 15 +++++ .../templates/dispatch/companies/detail.html | 10 ++++ .../templates/dispatch/companies/edit.html | 15 +++++ .../templates/dispatch/companies/list.html | 39 ++++++++++++ app/dispatch/templates/dispatch/index.html | 36 +++++++++-- app/dispatch/templates/dispatch/login.html | 70 +++++++++------------- app/dispatch/urls.py | 12 +++- app/dispatch/views.py | 57 +++++++++++------- 12 files changed, 246 insertions(+), 75 deletions(-) create mode 100644 app/dispatch/middleware.py create mode 100644 app/dispatch/templates/dispatch/companies/create.html create mode 100644 app/dispatch/templates/dispatch/companies/detail.html create mode 100644 app/dispatch/templates/dispatch/companies/edit.html create mode 100644 app/dispatch/templates/dispatch/companies/list.html diff --git a/app/app/settings.py b/app/app/settings.py index 60e03fd..bbfc325 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -48,6 +48,7 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'dispatch.middleware.LoginRequiredMiddleware', ] ROOT_URLCONF = 'app.urls' @@ -119,3 +120,6 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' + +LOGIN_URL = 'login/' +LOGIN_REDIRECT_URL = '/' \ No newline at end of file diff --git a/app/app/urls.py b/app/app/urls.py index 89d5431..6e1b869 100644 --- a/app/app/urls.py +++ b/app/app/urls.py @@ -17,6 +17,6 @@ from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ - url(r'^dispatch/', include('dispatch.urls')), + url(r'^', include('dispatch.urls')), url(r'^admin/', admin.site.urls), ] diff --git a/app/dispatch/middleware.py b/app/dispatch/middleware.py new file mode 100644 index 0000000..2b3e349 --- /dev/null +++ b/app/dispatch/middleware.py @@ -0,0 +1,31 @@ +from django.http import HttpResponseRedirect +from django.conf import settings +from re import compile +from django.core.urlresolvers import reverse + +EXEMPT_URLS = [reverse('login'),reverse('logout')] + +if hasattr(settings, 'LOGIN_EXEMPT_URLS'): + EXEMPT_URLS += settings.LOGIN_EXEMPT_URLS + +""" +Middleware that requires a user to be authenticated to view any page other +than LOGIN_URL. Exemptions to this requirement can optionally be specified +in settings via a list of regular expressions in LOGIN_EXEMPT_URLS (which +you can copy from your urls.py). + +Requires authentication middleware and template context processors to be +loaded. You'll get an error if they aren't. +""" +class LoginRequiredMiddleware(object): + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if not request.user or not request.user.is_authenticated(): + path = request.path_info + if path not in EXEMPT_URLS: + login_uri = '%s?next=%s' % (settings.LOGIN_URL, request.path_info) + return HttpResponseRedirect(login_uri) + + return self.get_response(request) \ No newline at end of file diff --git a/app/dispatch/templates/dispatch/base.html b/app/dispatch/templates/dispatch/base.html index 8654d2e..d7a85a9 100644 --- a/app/dispatch/templates/dispatch/base.html +++ b/app/dispatch/templates/dispatch/base.html @@ -12,18 +12,40 @@ - +