Exemple #1
0
    def _update_multiple_resources(self,
                                   model,
                                   resource,
                                   data,
                                   user,
                                   ids,
                                   previous_instances,
                                   prefetch=False):
        """
        Update several resources:
            * check they are really updated,
            * update _meta field
            * pass on to validation.
        """
        # Check that old instances are different than new
        # and update _meta field in new ones.
        changed = []
        versional_comment = data.get('_versional_comment', None)
        instance_dict = instance_list_to_dict(deepcopy(previous_instances))

        for identifier in ids:
            old_instance = instance_dict[identifier]
            new_instance = data['instances'][identifier]
            new_with_meta = self._create_new_instance_meta(
                old_instance, new_instance, user, versional_comment)
            if new_with_meta:
                changed.append(new_with_meta)
            else:
                self._output_instances.append(old_instance)

        # Go through changed and validate it
        return self._validate_sequentially(None, changed)
Exemple #2
0
    def _check_previous_exist(self, previous_instances, error, ids, data,
                              model, user):
        """Check we found all the previous instances"""
        if len(previous_instances) < len(ids):
            raise tornado.web.HTTPError(
                400, 'Asked to update %s instances'
                'but only found %s in the database' %
                (len(ids), len(previous_instances)))

        if 'instances' in data:
            if isinstance(data['instances'], list):
                # Convert to a dict
                new_instances_as_dict = {}
                for instance in data['instances']:
                    new_instances_as_dict[instance['_id']] = instance
                    data['instances'] = new_instances_as_dict

        elif 'fields' in data:
            # Make the field based approach the same as the explicit instance
            # Make the new instances
            data['instances'] = instance_list_to_dict(
                deepcopy(previous_instances))

            for instance in data['instances']:
                data['instances'][instance].update(data['fields'])
            #Now it ends up like self._update_multiple_resources
            del data['ids']
            del data['fields']

        return self._update_multiple_resources(model, model['_id'], data, user,
                                               ids, previous_instances, True)
Exemple #3
0
    def _handle_embedded_models(list_of_embedded_models,
                                error,
                                model,
                                instance,
                                success):
        """When looping through the model results has finished,
        validate the instance."""
        embedded_models = instance_list_to_dict(list_of_embedded_models)

        return success(
            model=model,
            embedded_models=embedded_models)
Exemple #4
0
    def _validate_modifier(models,
                           error,
                           model_name, modifier,
                           embedded_model_names,
                           unknown_names, success):
        """Validate modifier."""
        embedded_models = instance_list_to_dict(models)
        model = embedded_models.pop(model_name)

        validate_modification(model,
                              modifier,
                              handle_none=False,
                              embedded_models=embedded_models,
                              callback=success)
Exemple #5
0
    def _check_previous_exist(self,
                              previous_instances,
                              error,
                              ids,
                              data,
                              model,
                              user):
        """Check we found all the previous instances"""
        if len(previous_instances) < len(ids):
            raise tornado.web.HTTPError(
                400, 'Asked to update %s instances'
                'but only found %s in the database' % (
                    len(ids),
                    len(previous_instances)))

        if 'instances' in data:
            if isinstance(data['instances'], list):
                # Convert to a dict
                new_instances_as_dict = {}
                for instance in data['instances']:
                    new_instances_as_dict[instance['_id']] = instance
                    data['instances'] = new_instances_as_dict

        elif 'fields' in data:
            # Make the field based approach the same as the explicit instance
            # Make the new instances
            data['instances'] = instance_list_to_dict(
                deepcopy(previous_instances))

            for instance in data['instances']:
                data['instances'][instance].update(data['fields'])
            #Now it ends up like self._update_multiple_resources
            del data['ids']
            del data['fields']

        return self._update_multiple_resources(
            model,
            model['_id'],
            data,
            user,
            ids,
            previous_instances,
            True)
Exemple #6
0
    def _update_multiple_resources(self,
                                   model,
                                   resource,
                                   data,
                                   user,
                                   ids,
                                   previous_instances,
                                   prefetch=False):
        """
        Update several resources:
            * check they are really updated,
            * update _meta field
            * pass on to validation.
        """
        # Check that old instances are different than new
        # and update _meta field in new ones.
        changed = []
        versional_comment = data.get('_versional_comment', None)
        instance_dict = instance_list_to_dict(
            deepcopy(previous_instances))

        for identifier in ids:
            old_instance = instance_dict[identifier]
            new_instance = data['instances'][identifier]
            new_with_meta = self._create_new_instance_meta(
                old_instance,
                new_instance,
                user,
                versional_comment)
            if new_with_meta:
                changed.append(new_with_meta)
            else:
                self._output_instances.append(old_instance)

        # Go through changed and validate it
        return self._validate_sequentially(None, changed)