def create(self, request, *args, **kwargs): self.permission_classes = (CanEditParentDictionary, ) if not self.parent_resource: return HttpResponse(status=status.HTTP_405_METHOD_NOT_ALLOWED) serializer = self.get_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): self.pre_save(serializer.object) save_kwargs = { 'force_insert': True, 'parent_resource': self.parent_resource, } self.object = serializer.save(**save_kwargs) if serializer.is_valid(): self.post_save(self.object, created=True) headers = self.get_success_headers(serializer.data) serializer = MappingDetailSerializer( self.object, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) return Response( { 'errors': (('' if k == '__all__' else k + ' : ') + v[0]) for k, v in serializer.errors.items() }, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): self.permission_classes = (CanEditParentDictionary, ) self.serializer_class = MappingUpdateSerializer partial = True self.object = self.get_object() created = False save_kwargs = {'force_update': True} if 'update_comment' in request.DATA: save_kwargs = { 'force_update': True, 'update_comment': request.DATA.get('update_comment') } else: save_kwargs = {'force_update': True} success_status_code = status.HTTP_200_OK serializer = self.get_serializer(self.object, data=request.DATA, files=request.FILES, partial=partial) if serializer.is_valid(): try: self.pre_save(serializer.object) except ValidationError as e: return Response(e.messages, status=status.HTTP_400_BAD_REQUEST) self.object = serializer.save(**save_kwargs) self.post_save(self.object, created=created) serializer = MappingDetailSerializer(self.object, context={'request': request}) return Response(serializer.data, status=success_status_code) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def retrieve(self, request, *args, **kwargs): super(SourceRetrieveUpdateDestroyView, self).retrieve(request, *args, **kwargs) self.object = self.get_object() serializer = self.get_serializer(self.object) data = serializer.data source_version = None offset = request.QUERY_PARAMS.get(OFFSET_PARAM, DEFAULT_OFFSET) try: offset = int(offset) except ValueError: offset = DEFAULT_OFFSET limit = settings.REST_FRAMEWORK.get('MAX_PAGINATE_BY', self.paginate_by) include_retired = False include_concepts = request.QUERY_PARAMS.get(INCLUDE_CONCEPTS_PARAM, False) include_mappings = request.QUERY_PARAMS.get(INCLUDE_MAPPINGS_PARAM, False) updated_since = None if include_concepts or include_mappings: source_version = SourceVersion.get_latest_version_of(self.object) paginate_by = self.get_paginate_by(EmptyQuerySet()) if paginate_by: limit = min(limit, paginate_by) include_retired = request.QUERY_PARAMS.get(INCLUDE_RETIRED_PARAM, False) updated_since = parse_updated_since_param(request) if include_concepts: source_version_concepts = source_version.concepts queryset = ConceptVersion.objects.filter(is_active=True) if not include_retired: queryset = queryset.filter(~Q(retired=True)) if updated_since: queryset = queryset.filter(updated_at__gte=updated_since) queryset = queryset.filter(id__in=source_version_concepts) queryset = queryset[offset:offset + limit] serializer = ConceptVersionDetailSerializer(queryset, many=True) data['concepts'] = serializer.data if include_mappings: all_children = source_version.mappings queryset = Mapping.objects.filter(is_active=True) if not include_retired: queryset = queryset.filter(~Q(retired=True)) queryset = queryset.filter(id__in=all_children) if updated_since: queryset = queryset.filter(updated_at__gte=updated_since) queryset = queryset[offset:offset + limit] serializer = MappingDetailSerializer(queryset, many=True) data['mappings'] = serializer.data return Response(data)
def create(self, request, *args, **kwargs): self.permission_classes = (CanEditParentDictionary, ) if not self.parent_resource: return HttpResponse(status=status.HTTP_405_METHOD_NOT_ALLOWED) serializer = self.get_serializer(data=request.DATA, files=request.FILES) if serializer.is_valid(): self.pre_save(serializer.object) self.object = serializer.save(force_insert=True, parent_resource=self.parent_resource) if serializer.is_valid(): self.post_save(self.object, created=True) headers = self.get_success_headers(serializer.data) serializer = MappingDetailSerializer( self.object, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)