From 426919fa9f5be4b38af26a666f1e28794721394e Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Sat, 25 Mar 2017 16:18:34 +0000 Subject: [PATCH] refactor: break document-only stuff into the paperless app The `SessionOrBasicAuthMixin` and `StandardPagination` classes were living in the documents app and I needed them in the new `reminders` app, so this commit breaks them out of `documents` and puts them in the central `paperless` app instead. --- src/documents/mixins.py | 48 ----------------------------------------- src/documents/views.py | 12 +++-------- src/paperless/mixins.py | 46 +++++++++++++++++++++++++++++++++++++++ src/paperless/views.py | 7 ++++++ 4 files changed, 56 insertions(+), 57 deletions(-) create mode 100644 src/paperless/mixins.py create mode 100644 src/paperless/views.py diff --git a/src/documents/mixins.py b/src/documents/mixins.py index a031dd50f..4d4e9783f 100644 --- a/src/documents/mixins.py +++ b/src/documents/mixins.py @@ -1,8 +1,3 @@ -from django.contrib.auth.mixins import AccessMixin -from django.contrib.auth import authenticate, login -import base64 - - class Renderable(object): """ A handy mixin to make it easier/cleaner to print output based on a @@ -12,46 +7,3 @@ class Renderable(object): def _render(self, text, verbosity): if self.verbosity >= verbosity: print(text) - - -class SessionOrBasicAuthMixin(AccessMixin): - """ - Session or Basic Authentication mixin for Django. - It determines if the requester is already logged in or if they have - provided proper http-authorization and returning the view if all goes - well, otherwise responding with a 401. - - Base for mixin found here: https://djangosnippets.org/snippets/3073/ - """ - - def dispatch(self, request, *args, **kwargs): - - # check if user is authenticated via the session - if request.user.is_authenticated: - - # Already logged in, just return the view. - return super(SessionOrBasicAuthMixin, self).dispatch( - request, *args, **kwargs - ) - - # apparently not authenticated via session, maybe via HTTP Basic? - if 'HTTP_AUTHORIZATION' in request.META: - auth = request.META['HTTP_AUTHORIZATION'].split() - if len(auth) == 2: - # NOTE: Support for only basic authentication - if auth[0].lower() == "basic": - authString = base64.b64decode(auth[1]).decode('utf-8') - uname, passwd = authString.split(':') - user = authenticate(username=uname, password=passwd) - if user is not None: - if user.is_active: - login(request, user) - request.user = user - return super( - SessionOrBasicAuthMixin, self - ).dispatch( - request, *args, **kwargs - ) - - # nope, really not authenticated - return self.handle_no_permission() diff --git a/src/documents/views.py b/src/documents/views.py index 3598472af..b57a0a571 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -2,15 +2,16 @@ from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt from django.views.generic import DetailView, FormView, TemplateView from django_filters.rest_framework import DjangoFilterBackend -from rest_framework.filters import SearchFilter, OrderingFilter from paperless.db import GnuPG +from paperless.mixins import SessionOrBasicAuthMixin +from paperless.views import StandardPagination +from rest_framework.filters import OrderingFilter, SearchFilter from rest_framework.mixins import ( DestroyModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin ) -from rest_framework.pagination import PageNumberPagination from rest_framework.permissions import IsAuthenticated from rest_framework.viewsets import ( GenericViewSet, @@ -27,7 +28,6 @@ from .serialisers import ( LogSerializer, TagSerializer ) -from .mixins import SessionOrBasicAuthMixin class IndexView(TemplateView): @@ -92,12 +92,6 @@ class PushView(SessionOrBasicAuthMixin, FormView): return HttpResponse("0") -class StandardPagination(PageNumberPagination): - page_size = 25 - page_size_query_param = "page-size" - max_page_size = 100000 - - class CorrespondentViewSet(ModelViewSet): model = Correspondent queryset = Correspondent.objects.all() diff --git a/src/paperless/mixins.py b/src/paperless/mixins.py new file mode 100644 index 000000000..f4f1fcdec --- /dev/null +++ b/src/paperless/mixins.py @@ -0,0 +1,46 @@ +from django.contrib.auth.mixins import AccessMixin +from django.contrib.auth import authenticate, login +import base64 + + +class SessionOrBasicAuthMixin(AccessMixin): + """ + Session or Basic Authentication mixin for Django. + It determines if the requester is already logged in or if they have + provided proper http-authorization and returning the view if all goes + well, otherwise responding with a 401. + + Base for mixin found here: https://djangosnippets.org/snippets/3073/ + """ + + def dispatch(self, request, *args, **kwargs): + + # check if user is authenticated via the session + if request.user.is_authenticated: + + # Already logged in, just return the view. + return super(SessionOrBasicAuthMixin, self).dispatch( + request, *args, **kwargs + ) + + # apparently not authenticated via session, maybe via HTTP Basic? + if 'HTTP_AUTHORIZATION' in request.META: + auth = request.META['HTTP_AUTHORIZATION'].split() + if len(auth) == 2: + # NOTE: Support for only basic authentication + if auth[0].lower() == "basic": + authString = base64.b64decode(auth[1]).decode('utf-8') + uname, passwd = authString.split(':') + user = authenticate(username=uname, password=passwd) + if user is not None: + if user.is_active: + login(request, user) + request.user = user + return super( + SessionOrBasicAuthMixin, self + ).dispatch( + request, *args, **kwargs + ) + + # nope, really not authenticated + return self.handle_no_permission() diff --git a/src/paperless/views.py b/src/paperless/views.py new file mode 100644 index 000000000..d2ce23aaf --- /dev/null +++ b/src/paperless/views.py @@ -0,0 +1,7 @@ +from rest_framework.pagination import PageNumberPagination + + +class StandardPagination(PageNumberPagination): + page_size = 25 + page_size_query_param = "page-size" + max_page_size = 100000