def build(cls, request) -> 'CloneRequestDTO': dto = cls() dto.requester = api_logging.get_user_object(request) dto.validation_name = cls._get_field(request, 'validation_name', fallback_value='') dto.notes = cls._get_field(request, 'notes') dto.validation_id = cls._get_field(request, 'validation_id') dto.site_url = request.build_absolute_uri('/') return dto
def update(self, request, *args, **kwargs): to_activate = False if 'to_activate' in request.data: to_activate = True del request.data['to_activate'] response = super().update(request, *args, **kwargs) user = get_user_object(request) if to_activate: profile_data = response.data # set 'active' to false for other profiles user.profiles.exclude(id=profile_data['id']).update(active=False) # return User data with just updated profile return Response(UserSerializer(user).data)
def post(self, request): user = get_user_object(request) serializer = ProfileSerializer(data=request.data) if serializer.is_valid(): try: profile = serializer.save(user=user) except IntegrityError: raise ValidationError( {'integrity error': 'Duplicate creation attempt'}) except Exception as e: raise ValidationError({'detail': e}) else: user.profiles.add(profile) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) user = get_user_object(request) change_reason = request.data.get('change_reason') serializer = self.perform_update(serializer, user=user, reason=change_reason) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return Response(serializer.data)
def build(cls, request) -> 'ImportRequestDTO': dto = cls() dto.file = cls._get_field(request, 'file') dto.is_url_import = cls._extract_boolean(request, 'is_url_import') dto.validation_id = cls.__extract_validation_id(request) dto.name = cls._get_field(request, 'validation_name') dto.notes = cls._get_field(request, 'notes') dto.date = cls.__extract_date(request) dto.source_file = cls._get_field(request, 'source_file') dto.force_run = cls._extract_boolean(request, 'force_run') dto.requester = api_logging.get_user_object(request) dto.site_url = request.build_absolute_uri('/') dto.force_item = cls._extract_boolean(request, 'force_item') dto.import_reason = cls._get_field(request, 'import_reason') dto.validation_type = cls._get_field(request, 'validation_type') return dto
def update(self, request, *args, **kwargs): ids = [int(item['id']) for item in request.data] if ids: instances = Result.objects.filter(id__in=ids) else: raise ValidationError({'integrity error': 'No results for update'}) serializer = self.get_serializer(instances, data=request.data, partial=False, many=True) serializer.is_valid(raise_exception=True) user = get_user_object(request) # Change reason for each items is the same change_reason = request.data[0]['change_reason'] self.perform_update(serializer, user, change_reason, *args, **kwargs) return Response(serializer.data)
def post(self, request): file = request.data.get('file') component = request.data.get('component') # check for missing parameters missing_params = [] for param_name, value in zip(('file', 'component'), (file, component)): if not value or value in ('undefined', 'null'): missing_params.append(param_name) if missing_params: if len(missing_params) == 1: msg = f"'{missing_params[0]}' parameter is missing in form data." else: msg = f"{', '.join(missing_params)} parameters are missing in form data." raise ParseError(msg) user = get_user_object(request) outcome = import_features(file, user, component) data = outcome.build() code = status.HTTP_200_OK if outcome.is_success( ) else status.HTTP_422_UNPROCESSABLE_ENTITY return Response(data=data, status=code)
def get(self, request, *args, **kwargs): validations = self.get_queryset() return Response( get_mappings_by_validations(self.serializer_class, validations, get_user_object(request)))
def has_object_permission(self, request, view, obj): user = get_user_object(request) if obj.owner == user or user.is_staff: return True return False
def delete(self, request, *args, **kwargs): super().delete(request, *args, **kwargs) user = get_user_object(request) return Response(UserSerializer(user).data)
def get(self, request): user_object = get_user_object(request) user_data = UserSerializer(user_object).data return Response(user_data)
def perform_update(self, serializer, **kwargs): serializer.save(updated_by=get_user_object(self.request)) return super().perform_update(serializer)
def perform_create(self, serializer): serializer.save(created_by=get_user_object(self.request)) return super().perform_create(serializer)