Implement remainder of the api functionality
This commit is contained in:
parent
1bbed5eecc
commit
1b62922706
2 changed files with 98 additions and 49 deletions
121
app/views.py
121
app/views.py
|
@ -11,6 +11,9 @@ from django.core.paginator import Paginator
|
|||
from django.http import FileResponse, HttpResponsePermanentRedirect, HttpResponse, JsonResponse
|
||||
from django.urls import reverse
|
||||
from django.shortcuts import get_object_or_404
|
||||
import operator
|
||||
from django.db.models import Q
|
||||
from functools import reduce
|
||||
import os.path as ospath
|
||||
from shutil import move
|
||||
|
||||
|
@ -18,7 +21,7 @@ from os import makedirs
|
|||
from tempfile import gettempdir
|
||||
from .formats import *
|
||||
from .convert import reindex_data, refresh_data
|
||||
from .models import Person, RangesPeople, TaxaPeople, FieldsPeople, MethodsPeople, Range
|
||||
from .models import Person, RangesPeople, TaxaPeople, FieldsPeople, MethodsPeople, Range, Field, Taxon, Resource
|
||||
|
||||
# Get temporary file storage
|
||||
UPLOAD_PATH = gettempdir()
|
||||
|
@ -162,36 +165,7 @@ class ConfigurationHomePageView(TemplateView):
|
|||
return context
|
||||
|
||||
|
||||
class SearchView(View):
|
||||
|
||||
def get(self, request):
|
||||
page = int(self.request.GET.get('page', 1))
|
||||
per_page = int(self.request.GET.get('per_page', 10))
|
||||
q = self.request.GET.get('q', '').strip()
|
||||
if not q or len(q) < 3:
|
||||
query_set = Person.objects.all()
|
||||
else:
|
||||
query_set = Person.objects.filter(field_indexer__icontains=q.split(" "))
|
||||
|
||||
q_country = self.request.GET.get('country', '')
|
||||
q_range = self.request.GET.get('range', '')
|
||||
q_field = self.request.GET.get('field', '')
|
||||
q_taxon = self.request.GET.get('taxon', '')
|
||||
if len(q_country) > 2:
|
||||
query_set = query_set.filter(country__icontains=q_country.strip().lower())
|
||||
if len(q_range) > 2:
|
||||
ranges_people = RangesPeople.objects.filter(range__name__icontains=q_range.strip().lower())
|
||||
r_people_ids = [rp.person_id for rp in ranges_people]
|
||||
query_set = query_set.filter(id__in=r_people_ids)
|
||||
if len(q_field) > 2:
|
||||
fields_people = FieldsPeople.objects.filter(field__name__icontains=q_field.strip().lower())
|
||||
f_people_ids = [fp.person_id for fp in fields_people]
|
||||
query_set = query_set.filter(id__in=f_people_ids)
|
||||
if len(q_taxon) > 2:
|
||||
taxa_people = TaxaPeople.objects.filter(taxon__name__icontains=q_taxon.strip().lower())
|
||||
t_people_ids = [tp.person_id for tp in taxa_people]
|
||||
query_set = query_set.filter(id__in=t_people_ids)
|
||||
query_set = query_set.order_by('last_name')
|
||||
def get_paginated(query_set, page, per_page):
|
||||
paginator = Paginator(query_set, per_page).page(page)
|
||||
filters = {
|
||||
'country': [],
|
||||
|
@ -223,7 +197,41 @@ class SearchView(View):
|
|||
'page': page, 'pages': paginator.paginator.num_pages, 'total': paginator.paginator.count,
|
||||
'has_next': paginator.has_next(), 'has_prev': paginator.has_previous()
|
||||
}
|
||||
return JsonResponse(return_data)
|
||||
return return_data
|
||||
|
||||
|
||||
class SearchView(View):
|
||||
|
||||
def get(self, request):
|
||||
page = int(self.request.GET.get('page', 1))
|
||||
per_page = int(self.request.GET.get('per_page', 10))
|
||||
q = self.request.GET.get('q', '').strip()
|
||||
if not q or len(q) < 3:
|
||||
query_set = Person.objects.all()
|
||||
else:
|
||||
query = reduce(operator.or_, (Q(field_indexer__icontains=item) for item in q.split(" ")))
|
||||
query_set = Person.objects.filter(query)
|
||||
|
||||
q_country = self.request.GET.get('country', '')
|
||||
q_range = self.request.GET.get('range', '')
|
||||
q_field = self.request.GET.get('field', '')
|
||||
q_taxon = self.request.GET.get('taxon', '')
|
||||
if len(q_country) > 2:
|
||||
query_set = query_set.filter(country__icontains=q_country.strip().lower())
|
||||
if len(q_range) > 2:
|
||||
ranges_people = RangesPeople.objects.filter(range__name__icontains=q_range.strip().lower())
|
||||
r_people_ids = [rp.person_id for rp in ranges_people]
|
||||
query_set = query_set.filter(id__in=r_people_ids)
|
||||
if len(q_field) > 2:
|
||||
fields_people = FieldsPeople.objects.filter(field__name__icontains=q_field.strip().lower())
|
||||
f_people_ids = [fp.person_id for fp in fields_people]
|
||||
query_set = query_set.filter(id__in=f_people_ids)
|
||||
if len(q_taxon) > 2:
|
||||
taxa_people = TaxaPeople.objects.filter(taxon__name__icontains=q_taxon.strip().lower())
|
||||
t_people_ids = [tp.person_id for tp in taxa_people]
|
||||
query_set = query_set.filter(id__in=t_people_ids)
|
||||
query_set = query_set.order_by('last_name')
|
||||
return JsonResponse(get_paginated(query_set, page, per_page))
|
||||
|
||||
|
||||
class PeopleDetailView(View):
|
||||
|
@ -241,6 +249,55 @@ class PeopleDetailView(View):
|
|||
return JsonResponse(return_data)
|
||||
|
||||
|
||||
class PeopleListView(View):
|
||||
def get(self, request):
|
||||
page = int(self.request.GET.get('page', 1))
|
||||
per_page = int(self.request.GET.get('per_page', 10))
|
||||
query_set = Person.objects.all().order_by('last_name')
|
||||
return JsonResponse(get_paginated(query_set, page, per_page))
|
||||
|
||||
|
||||
class ResourceListView(View):
|
||||
def get(self, request):
|
||||
page = int(self.request.GET.get('page', 1))
|
||||
per_page = int(self.request.GET.get('per_page', 10))
|
||||
query_set = Resource.objects.all().order_by('title')
|
||||
return JsonResponse(get_paginated(query_set, page, per_page))
|
||||
|
||||
|
||||
MAX_FILTER_RESULTS = 50
|
||||
|
||||
|
||||
class RangesListView(View):
|
||||
def get(self, request):
|
||||
q = self.request.GET.get('q', '').strip()
|
||||
if not q or len(q) < 3:
|
||||
query_set = Range.objects.all().order_by('name')[:MAX_FILTER_RESULTS]
|
||||
else:
|
||||
query_set = Range.objects.filter(name__icontains=q.strip().lower())
|
||||
return JsonResponse(list(query_set.values()), safe=False)
|
||||
|
||||
|
||||
class FieldsListView(View):
|
||||
def get(self, request):
|
||||
q = self.request.GET.get('q', '').strip()
|
||||
if not q or len(q) < 3:
|
||||
query_set = Field.objects.all().order_by('name')[:MAX_FILTER_RESULTS]
|
||||
else:
|
||||
query_set = Field.objects.filter(name__icontains=q.strip().lower())
|
||||
return JsonResponse(list(query_set.values()), safe=False)
|
||||
|
||||
|
||||
class TaxaListView(View):
|
||||
def get(self, request):
|
||||
q = self.request.GET.get('q', '').strip()
|
||||
if not q or len(q) < 3:
|
||||
query_set = Taxon.objects.all().order_by('name')[:MAX_FILTER_RESULTS]
|
||||
else:
|
||||
query_set = Taxon.objects.filter(name__icontains=q.strip().lower())
|
||||
return JsonResponse(list(query_set.values()), safe=False)
|
||||
|
||||
|
||||
class UploadView(TemplateView):
|
||||
template_name = "app/admin/config.html"
|
||||
|
||||
|
|
|
@ -18,7 +18,8 @@ from django.urls import path
|
|||
from django.views.generic import TemplateView
|
||||
from app.views import (
|
||||
HomePageView, OfflinePageView, send_from_file, DemoPageView, ConfigurationPageView, get_progress, UploadView,
|
||||
ReindexPageView, RefreshPageView, ConfigurationHomePageView, SearchView, PeopleDetailView
|
||||
ReindexPageView, RefreshPageView, ConfigurationHomePageView, SearchView, PeopleDetailView, TaxaListView,
|
||||
FieldsListView, RangesListView, ResourceListView, PeopleListView
|
||||
)
|
||||
|
||||
|
||||
|
@ -39,18 +40,9 @@ urlpatterns = [
|
|||
path('refresh', RefreshPageView.as_view(), name='refresh'),
|
||||
path('api/search', SearchView.as_view(), name='api-search'),
|
||||
path('api/people/<int:people_id>', PeopleDetailView.as_view(), name='api-people-detail'),
|
||||
|
||||
|
||||
# TODO Implement the following
|
||||
# path('reindex/', admin.site.urls),
|
||||
# path('progress/', admin.site.urls),
|
||||
# path('refresh/', admin.site.urls),
|
||||
# path('upload/', admin.site.urls),
|
||||
# path('api/taxa', admin.site.urls),
|
||||
# path('api/fields', admin.site.urls),
|
||||
# path('api/ranges', admin.site.urls),
|
||||
# path('api/resources', admin.site.urls),
|
||||
# path('api/people', admin.site.urls),
|
||||
# path('api/people/<int:people_id>', admin.site.urls),
|
||||
# path('api/search', admin.site.urls),
|
||||
path('api/taxa', TaxaListView.as_view(), name='api-taxa-list'),
|
||||
path('api/fields', FieldsListView.as_view(), name='api-field-list'),
|
||||
path('api/ranges', RangesListView.as_view(), name='api-range-list'),
|
||||
path('api/resources', ResourceListView.as_view(), name='api-resource-list'),
|
||||
path('api/people', PeopleListView.as_view(), name='api-people-list'),
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue