Implement remainder of the api functionality

This commit is contained in:
PCoder 2021-08-31 21:55:15 +05:30
parent 1bbed5eecc
commit 1b62922706
2 changed files with 98 additions and 49 deletions

View file

@ -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,6 +165,41 @@ class ConfigurationHomePageView(TemplateView):
return context
def get_paginated(query_set, page, per_page):
paginator = Paginator(query_set, per_page).page(page)
filters = {
'country': [],
'range': [],
'field': [],
'taxon': [],
}
for p in paginator.object_list:
filters['country'].append(p.country)
for r in p.rangespeople_set.all():
filters['range'].append(r.name)
for r in p.fieldspeople_set.all():
filters['field'].append(r.name)
for r in p.taxapeople_set.all():
filters['taxon'].append(r.name)
filters = {
'country': sorted(set(filters['country'])),
'range': sorted(set(filters['range'])),
'field': sorted(set(filters['field'])),
'taxon': sorted(set(filters['taxon'])),
}
if len(query_set.all()) > len(paginator.object_list) and paginator.paginator.count == 1:
paginator.object_list = query_set.all()
paginator.count(len(paginator.object_list))
return_data = {
'items': [p.dict() for p in paginator.object_list],
'filters': filters,
'page': page, 'pages': paginator.paginator.num_pages, 'total': paginator.paginator.count,
'has_next': paginator.has_next(), 'has_prev': paginator.has_previous()
}
return return_data
class SearchView(View):
def get(self, request):
@ -171,7 +209,8 @@ class SearchView(View):
if not q or len(q) < 3:
query_set = Person.objects.all()
else:
query_set = Person.objects.filter(field_indexer__icontains=q.split(" "))
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', '')
@ -192,38 +231,7 @@ class SearchView(View):
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')
paginator = Paginator(query_set, per_page).page(page)
filters = {
'country': [],
'range': [],
'field': [],
'taxon': [],
}
for p in paginator.object_list:
filters['country'].append(p.country)
for r in p.rangespeople_set.all():
filters['range'].append(r.name)
for r in p.fieldspeople_set.all():
filters['field'].append(r.name)
for r in p.taxapeople_set.all():
filters['taxon'].append(r.name)
filters = {
'country': sorted(set(filters['country'])),
'range': sorted(set(filters['range'])),
'field': sorted(set(filters['field'])),
'taxon': sorted(set(filters['taxon'])),
}
if len(query_set.all()) > len(paginator.object_list) and paginator.paginator.count == 1:
paginator.object_list = query_set.all()
paginator.count(len(paginator.object_list))
return_data = {
'items': [p.dict() for p in paginator.object_list],
'filters': filters,
'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 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"

View file

@ -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'),
]