fix redis cache & add cache to custom types search
This commit is contained in:
28
api/views.py
28
api/views.py
@@ -2,6 +2,7 @@ from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
from django.db.models import Q
|
||||
from django.contrib.auth import models as auth_models
|
||||
from django.core.cache import cache
|
||||
from rest_framework import viewsets, permissions, settings
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
@@ -39,24 +40,27 @@ class AcquisitionViewSet(viewsets.ModelViewSet):
|
||||
|
||||
@api_view(['POST'])
|
||||
def custom_types_search(request):
|
||||
items = []
|
||||
cache_key = f"custom_types_search_{request.data}"
|
||||
items = cache.get(cache_key, [])
|
||||
|
||||
for q in request.data:
|
||||
conditions = Q()
|
||||
for k in q.keys():
|
||||
value = q[k]
|
||||
if not items:
|
||||
for q in request.data:
|
||||
conditions = Q()
|
||||
for k in q.keys():
|
||||
value = q[k]
|
||||
|
||||
token = k.split('___')
|
||||
key, mods = token[0], token[1:]
|
||||
token = k.split('___')
|
||||
key, mods = token[0], token[1:]
|
||||
|
||||
cond = Q(**{key: value})
|
||||
cond = Q(**{key: value})
|
||||
|
||||
if "not" in mods:
|
||||
cond = ~cond
|
||||
if "not" in mods:
|
||||
cond = ~cond
|
||||
|
||||
conditions = conditions & cond
|
||||
conditions = conditions & cond
|
||||
|
||||
items.extend(sde_models.SDEType.objects.filter(conditions))
|
||||
items.extend(sde_models.SDEType.objects.filter(conditions))
|
||||
cache.set(cache_key, items, 60 * 60)
|
||||
|
||||
paginator = settings.api_settings.DEFAULT_PAGINATION_CLASS()
|
||||
result_page = paginator.paginate_queryset(items, request)
|
||||
|
||||
Reference in New Issue
Block a user