add get-data endpoint

This commit is contained in:
Emmanuel Cazenave 2020-04-16 16:05:24 +02:00
parent d709c5bbf5
commit ac9ca6d7da
1 changed files with 32 additions and 0 deletions

View File

@ -367,6 +367,38 @@ query getDossiers($demarcheNumber: Int!, $createdSince: ISO8601DateTime, $first:
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
return response
@endpoint(
methods=['get'], perm='can_access', name='get-data',
description=_('Get data by sequence or siren')
)
def get_data(self, request, sequence=None, siren=None):
def build_result(entreprise):
return {
'data': {attr: getattr(entreprise, attr) for attr in COLUMNS_KEYNAMES}
}
if sequence is None and siren is None:
raise APIError('Need sequence or siren')
if sequence:
try:
entreprise = Entreprise.objects.get(resource=self, sequence=sequence)
return build_result(entreprise)
except Entreprise.DoesNotExist:
pass
if siren:
try:
entreprise = Entreprise.objects.get(resource=self, siren=siren)
return build_result(entreprise)
except Entreprise.DoesNotExist:
pass
return {
'data': {}
}
class Entreprise(models.Model):