Esempio n. 1
0
 def update(self, instance, validated_data):
     instance = ModelSerializer.update(self, instance, validated_data)
     instance.max_slices = EqualSequenceSlicer().get_max_slices(
         instance.candies)
     instance.is_valid_cake = CakeValidator().is_valid(instance)
     instance.save()
     return instance
Esempio n. 2
0
 def update(self, instance, validated_data):
     if not self.partial:
         self._check_required_fields_availability(validated_data)
         self._check_at_least_one_required_constraints(validated_data)
         available_field_keys = self._get_available_field_keys(
             validated_data)
         application_type = instance.application_type
         not_required_field_keys = application_type.field_set.filter(
             required=False, ).values_list('key', flat=True)
         missed_not_required_field_keys = (set(not_required_field_keys) -
                                           set(available_field_keys))
         instance.value_set.filter(
             field__key__in=missed_not_required_field_keys).delete()
     for value in validated_data['value_set']:
         field = value['field']
         if 'typified_value' not in value:
             instance.value_set.filter(field=field).delete()
             continue
         try:
             value_object = instance.value_set.get(field=field)
         except Value.DoesNotExist:
             value_object = Value(
                 field=field,
                 application=instance,
             )
         value_object.value = value['typified_value']
         value_object.save()
     validated_data_without_values = {
         item_key: item_value
         for (item_key, item_value) in validated_data.items()
         if item_key != 'value_set'
     }
     return ModelSerializer.update(self, instance,
                                   validated_data_without_values)
Esempio n. 3
0
	def update(self, instance, validated_data):
		print("==========  CALLED UPDATE! ===========")
		print(str(validated_data))
		print(str(instance.id))
		fields = ModelSerializer.get_fields(self)
		info = model_meta.get_field_info(instance)
		for attr, value in validated_data.items():
			if attr in info.relations and info.relations[attr].to_many:
				print("setting multiple " + str(instance) + " " + str(attr) + " " + str(value))
				set_many(instance, attr, value)
			else:
				print("setting single " + str(instance) + " " + str(attr) + " " + str(value))
				setattr(instance, attr, value)
		instance.save()
		print(fields)
		return ModelSerializer.update(self, instance, validated_data)
Esempio n. 4
0
 def update(self, instance, validated_data):
     if not self.partial:
         self._check_required_fields_availability(validated_data)
         self._check_at_least_one_required_constraints(validated_data)
         available_field_keys = self._get_available_field_keys(
             validated_data
         )
         application_type = instance.application_type
         not_required_field_keys = application_type.field_set.filter(
             required=False,
         ).values_list(
             'key',
             flat=True
         )
         missed_not_required_field_keys = (
             set(not_required_field_keys) - set(available_field_keys)
         )
         instance.value_set.filter(
             field__key__in=missed_not_required_field_keys
         ).delete()
     for value in validated_data['value_set']:
         field = value['field']
         if 'typified_value' not in value:
             instance.value_set.filter(
                 field=field
             ).delete()
             continue
         try:
             value_object = instance.value_set.get(field=field)
         except Value.DoesNotExist:
             value_object = Value(
                 field=field,
                 application=instance,
             )
         value_object.value = value['typified_value']
         value_object.save()
     validated_data_without_values = {
         item_key: item_value
         for (item_key, item_value)
         in validated_data.items()
         if item_key != 'value_set'
     }
     return ModelSerializer.update(
         self,
         instance,
         validated_data_without_values
     )
Esempio n. 5
0
    def update(self, instance, validated_data):
        request = self.context.get("request")

        if _only_admin(request):
            errors = dict()
            try:
                _data = UserUpdateInput(**request.data)
                has_email_update = self.update_email(_data, instance, errors)
                has_username_update = self.update_username(
                    _data, instance, errors)

                if len(errors.keys()) > 0:
                    raise ValidationError(detail=errors)

                if has_email_update or has_username_update:
                    ActivityService.log(
                        UserActions.basic_info_updated,
                        model_instance=instance,
                        request=request,
                    )

            except PydanticValidationError as e:
                for error in e.errors():
                    errors[error.get("loc")[0]] = [error.get("msg")]
                raise ValidationError(detail=errors)

            # ignore if user is admin and attempting to update his groups and status
            if request.user != instance:
                self.update_groups(request, instance)
                # update status
                self.update_status(request, instance)

        has_basic_info_changed = (
            validated_data.get("first_name") != instance.first_name
            or validated_data.get("last_name") != instance.last_name)

        saved = ModelSerializer.update(self, instance, validated_data)

        if has_basic_info_changed:
            ActivityService.log(UserActions.basic_info_updated,
                                model_instance=instance,
                                request=request)

        return saved
Esempio n. 6
0
 def update(self, instance, validated_data):
     instance.edit_date = timezone.now()
     instance.save()
     return ModelSerializer.update(self, instance, validated_data)