106 lines
3.5 KiB
Python
106 lines
3.5 KiB
Python
from django.shortcuts import get_object_or_404
|
|
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
|
|
from zoo.zoo_data.models import Entity
|
|
|
|
from . import utils
|
|
|
|
|
|
def individu_to_response(individu):
|
|
'''Serialize a person'''
|
|
d = individu.content.copy()
|
|
d['id'] = individu.id
|
|
|
|
if hasattr(individu, 'age_label'):
|
|
d['age_label'] = individu.age_label
|
|
if hasattr(individu, 'age'):
|
|
d['age'] = individu.age
|
|
if hasattr(individu, 'similarity'):
|
|
d['score'] = individu.similarity
|
|
if hasattr(individu, 'adresses'):
|
|
d['adresses'] = individu.adresses
|
|
if hasattr(individu, 'responsabilite_legale'):
|
|
d['responsabilite_legale'] = individu.responsabilite_legale
|
|
if hasattr(individu, 'enfants'):
|
|
d['enfants'] = [individu_to_response(enfant) for enfant in individu.enfants]
|
|
if hasattr(individu, 'parents'):
|
|
d['parents'] = [individu_to_response(parent) for parent in individu.parents]
|
|
if hasattr(individu, 'union'):
|
|
d['union'] = individu_to_response(individu.union)
|
|
d['union_statut'] = individu.union_statut
|
|
return d
|
|
|
|
|
|
class SearchView(APIView):
|
|
def get(self, request, format=None):
|
|
try:
|
|
limit = int(request.GET.get('limit', ''))
|
|
except ValueError:
|
|
limit = 100
|
|
try:
|
|
offset = int(request.GET.get('offset', ''))
|
|
except ValueError:
|
|
offset = 0
|
|
try:
|
|
threshold = float(request.GET.get('threshold', ''))
|
|
except ValueError:
|
|
threshold = 0.2
|
|
search = utils.PersonSearch(limit=threshold)
|
|
if 'q' in request.GET:
|
|
search = search.search_query(request.GET['q'])
|
|
else:
|
|
prenom = request.GET.get('prenom')
|
|
nom = request.GET.get('nom')
|
|
date_de_naissance = request.GET.get('date_de_naissance')
|
|
cle = request.GET.get('cle')
|
|
email = request.GET.get('email', '').strip()
|
|
if prenom or nom:
|
|
search = search.search_name(prenom, nom)
|
|
if date_de_naissance and search.match_birthdate(date_de_naissance):
|
|
search = search.search_birthdate(date_de_naissance)
|
|
if cle:
|
|
search = search.search_identifier(cle)
|
|
if email:
|
|
search = search.search_email(email)
|
|
data = [individu_to_response(person) for person in search[offset:offset + limit]]
|
|
return Response({
|
|
'err': 0,
|
|
'offset': offset,
|
|
'limit': limit,
|
|
'count': len(data),
|
|
'data': data,
|
|
'meta': {
|
|
'applications': utils.PersonSearch.applications(),
|
|
}
|
|
})
|
|
|
|
search = SearchView.as_view()
|
|
|
|
|
|
class ReseauView(APIView):
|
|
def get(self, request, identifier, format=None):
|
|
qs = Entity.objects.prefetch_related(
|
|
'left_relations__schema', 'left_relations__right',
|
|
'right_relations__schema', 'right_relations__left',
|
|
)
|
|
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
|
utils.PersonSearch.decorate_individu(individu)
|
|
return Response({
|
|
'err': 0,
|
|
'data': individu_to_response(individu),
|
|
'meta': {
|
|
'applications': utils.PersonSearch.applications(),
|
|
}
|
|
})
|
|
|
|
reseau = ReseauView.as_view()
|
|
|
|
|
|
class CreateIndividu(APIView):
|
|
def post(self, request):
|
|
return Response({})
|
|
|
|
create_individu = CreateIndividu.as_view()
|