Skip to content

Commit

Permalink
Added get_or_create endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
u8sand committed Nov 20, 2020
1 parent 4cf1e10 commit f5cd8b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions FAIRshakeAPI/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DigitalObjectSerializer(IdentifiableModelMixinSerializer):
projects = serializers.PrimaryKeyRelatedField(
queryset=models.Project.objects.filter(id__isnull=False),
many=True,
required=False,
)

class Meta:
Expand Down
30 changes: 24 additions & 6 deletions FAIRshakeAPI/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from django.db.models import Q, Avg, Count, Prefetch
from django.forms import ModelChoiceField
from django.urls import reverse
from rest_framework import views, viewsets, schemas, response, mixins, decorators, renderers, permissions
from rest_framework import views, viewsets, schemas, response, mixins, decorators, renderers, permissions, status
from functools import reduce
from collections import defaultdict, OrderedDict

Expand All @@ -36,8 +36,8 @@ def get_or_create(model, **kwargs):
k: v for k, v in kwargs.items() if v
})
obj.save()
return obj
return objects.last()
return obj, False
return objects.last(), True

def redirect_with_params(request, *args, **kwargs):
return shortcuts.redirect(
Expand Down Expand Up @@ -121,6 +121,24 @@ def save_form(self, request, form):
instance = form.save()
return instance

@swagger_auto_schema(methods=['post'])
@decorators.action(
detail=False, methods=['post'],
renderer_classes=[
renderers.JSONRenderer,
CustomBrowsableAPIRenderer,
],
)
def get_or_create(self, request, **kwargs):
''' Probe a digital object for the answers to various assessments
'''
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
item, found = get_or_create(self.get_model(), **serializer.validated_data)
serialized_item = self.get_serializer(item)
headers = self.get_success_headers(serialized_item.data)
return response.Response(serialized_item.data, status=status.HTTP_200_OK if found else status.HTTP_201_CREATED, headers=headers)

@swagger_auto_schema(methods=['get'], auto_schema=None)
@decorators.action(
detail=True, methods=['get'], schema=None,
Expand Down Expand Up @@ -391,7 +409,7 @@ def get_assessment(self):

# Get or create the assessment
if project_id:
assessment = get_or_create(models.Assessment,
assessment, _ = get_or_create(models.Assessment,
published=False,
project=models.Project.objects.get(id=project_id),
target=models.DigitalObject.objects.get(id=target_id),
Expand All @@ -400,7 +418,7 @@ def get_assessment(self):
methodology='user',
)
else:
assessment = get_or_create(models.Assessment,
assessment, _ = get_or_create(models.Assessment,
published=False,
project=None,
target=models.DigitalObject.objects.get(id=target_id),
Expand All @@ -411,7 +429,7 @@ def get_assessment(self):

# Ensure answers are created
for metric in assessment.rubric.metrics.all():
answer = get_or_create(models.Answer,
answer, _ = get_or_create(models.Answer,
assessment=assessment,
metric=metric,
)
Expand Down

0 comments on commit f5cd8b9

Please sign in to comment.