Merge pull request #83 from danielquinn/issue/80

Added basic filtering + search
This commit is contained in:
Daniel Quinn 2016-03-09 01:08:59 +00:00
commit 8beda76a49
4 changed files with 112 additions and 2 deletions

View File

@ -1,11 +1,13 @@
Django==1.9.2
Pillow==3.1.1
django-crispy-forms==1.6.0
django-extensions==1.6.1
django-filter==0.12.0
djangorestframework==3.3.2
python-dotenv==0.3.0
filemagic==1.6
langdetect==1.0.5
Pillow==3.1.1
pyocr==0.3.1
python-dateutil==2.4.2
python-dotenv==0.3.0
python-gnupg==0.3.8
pytz==2015.7

97
src/documents/filters.py Normal file
View File

@ -0,0 +1,97 @@
import django_filters
from rest_framework import filters
from .models import Document, Correspondent, Tag
class DocumentFilter(filters.FilterSet):
title__startswith = django_filters.CharFilter(
name="title", lookup_type="startswith",
label="Title starts with (case sensitive)"
)
title__istartswith = django_filters.CharFilter(
name="title", lookup_type="istartswith",
label="Title starts with (case insensitive)"
)
title__endswith = django_filters.CharFilter(
name="title", lookup_type="endswith",
label="Title ends with (case sensitive)"
)
title__iendswith = django_filters.CharFilter(
name="title", lookup_type="endswith",
label="Title ends with (case insensitive)"
)
title__contains = django_filters.CharFilter(
name="title", lookup_type="contains",
label="Title contains (case sensitive)"
)
title__icontains = django_filters.CharFilter(
name="title", lookup_type="icontains",
label="Title contains (case insensitive)"
)
content__contains = django_filters.CharFilter(
name="content", lookup_type="contains")
content__icontains = django_filters.CharFilter(
name="content", lookup_type="icontains")
class Meta(object):
model = Document
fields = ["title"]
class SluggableFilter(filters.FilterSet):
name__startswith = django_filters.CharFilter(
name="name", lookup_type="startswith",
label="Name starts with (case sensitive)"
)
name__istartswith = django_filters.CharFilter(
name="name", lookup_type="istartswith",
label="Name starts with (case insensitive)"
)
name__endswith = django_filters.CharFilter(
name="name", lookup_type="endswith",
label="Name ends with (case sensitive)"
)
name__iendswith = django_filters.CharFilter(
name="name", lookup_type="endswith",
label="Name ends with (case insensitive)"
)
name__contains = django_filters.CharFilter(
name="name", lookup_type="contains",
label="Name contains (case sensitive)"
)
name__icontains = django_filters.CharFilter(
name="name", lookup_type="icontains",
label="Name contains (case insensitive)"
)
slug__istartswith = django_filters.CharFilter(
name="slug", lookup_type="istartswith",
label="Slug starts with (case insensitive)"
)
slug__iendswith = django_filters.CharFilter(
name="slug", lookup_type="endswith",
label="Slug ends with (case insensitive)"
)
slug__icontains = django_filters.CharFilter(
name="slug", lookup_type="icontains",
label="Slug contains (case insensitive)"
)
class CorrespondentFilter(SluggableFilter):
class Meta(object):
model = Correspondent
fields = ["name"]
class TagFilter(SluggableFilter):
class Meta(object):
model = Tag
fields = ["name"]

View File

@ -3,6 +3,7 @@ from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import FormView, DetailView, TemplateView
from rest_framework import filters
from rest_framework.mixins import (
RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, ListModelMixin)
from rest_framework.pagination import PageNumberPagination
@ -12,6 +13,7 @@ from rest_framework.viewsets import (
from paperless.db import GnuPG
from .filters import DocumentFilter, CorrespondentFilter, TagFilter
from .forms import UploadForm
from .models import Correspondent, Tag, Document, Log
from .serialisers import (
@ -92,6 +94,8 @@ class CorrespondentViewSet(ModelViewSet):
serializer_class = CorrespondentSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CorrespondentFilter
class TagViewSet(ModelViewSet):
@ -100,6 +104,8 @@ class TagViewSet(ModelViewSet):
serializer_class = TagSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filter_class = TagFilter
class DocumentViewSet(RetrieveModelMixin,
@ -112,6 +118,9 @@ class DocumentViewSet(RetrieveModelMixin,
serializer_class = DocumentSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter)
filter_class = DocumentFilter
search_fields = ("title", "correspondent__name", "content")
class LogViewSet(ReadOnlyModelViewSet):
@ -120,3 +129,4 @@ class LogViewSet(ReadOnlyModelViewSet):
serializer_class = LogSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)

View File

@ -46,6 +46,7 @@ INSTALLED_APPS = [
"documents",
"rest_framework",
"crispy_forms",
]