mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-17 10:13:56 -05:00
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.
This commit is contained in:
parent
e47c152b81
commit
426919fa9f
@ -1,8 +1,3 @@
|
|||||||
from django.contrib.auth.mixins import AccessMixin
|
|
||||||
from django.contrib.auth import authenticate, login
|
|
||||||
import base64
|
|
||||||
|
|
||||||
|
|
||||||
class Renderable(object):
|
class Renderable(object):
|
||||||
"""
|
"""
|
||||||
A handy mixin to make it easier/cleaner to print output based on a
|
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):
|
def _render(self, text, verbosity):
|
||||||
if self.verbosity >= verbosity:
|
if self.verbosity >= verbosity:
|
||||||
print(text)
|
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()
|
|
||||||
|
@ -2,15 +2,16 @@ from django.http import HttpResponse
|
|||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.views.generic import DetailView, FormView, TemplateView
|
from django.views.generic import DetailView, FormView, TemplateView
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
from rest_framework.filters import SearchFilter, OrderingFilter
|
|
||||||
from paperless.db import GnuPG
|
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 (
|
from rest_framework.mixins import (
|
||||||
DestroyModelMixin,
|
DestroyModelMixin,
|
||||||
ListModelMixin,
|
ListModelMixin,
|
||||||
RetrieveModelMixin,
|
RetrieveModelMixin,
|
||||||
UpdateModelMixin
|
UpdateModelMixin
|
||||||
)
|
)
|
||||||
from rest_framework.pagination import PageNumberPagination
|
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.viewsets import (
|
from rest_framework.viewsets import (
|
||||||
GenericViewSet,
|
GenericViewSet,
|
||||||
@ -27,7 +28,6 @@ from .serialisers import (
|
|||||||
LogSerializer,
|
LogSerializer,
|
||||||
TagSerializer
|
TagSerializer
|
||||||
)
|
)
|
||||||
from .mixins import SessionOrBasicAuthMixin
|
|
||||||
|
|
||||||
|
|
||||||
class IndexView(TemplateView):
|
class IndexView(TemplateView):
|
||||||
@ -92,12 +92,6 @@ class PushView(SessionOrBasicAuthMixin, FormView):
|
|||||||
return HttpResponse("0")
|
return HttpResponse("0")
|
||||||
|
|
||||||
|
|
||||||
class StandardPagination(PageNumberPagination):
|
|
||||||
page_size = 25
|
|
||||||
page_size_query_param = "page-size"
|
|
||||||
max_page_size = 100000
|
|
||||||
|
|
||||||
|
|
||||||
class CorrespondentViewSet(ModelViewSet):
|
class CorrespondentViewSet(ModelViewSet):
|
||||||
model = Correspondent
|
model = Correspondent
|
||||||
queryset = Correspondent.objects.all()
|
queryset = Correspondent.objects.all()
|
||||||
|
46
src/paperless/mixins.py
Normal file
46
src/paperless/mixins.py
Normal file
@ -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()
|
7
src/paperless/views.py
Normal file
7
src/paperless/views.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user