Exemplo n.º 1
0
    def _validate_type(self):
        """
        Assert that the requested type is valid and uploadable via the
        project endpoint.
        """
        cls = psqlgraph.Node.get_subclass(self.entity_type)

        if not cls:
            return self.record_error(
                'Invalid entity type: {}.{}'.format(
                    self.entity_type, get_suggestion(self.entity_type, [
                        n.label for n in psqlgraph.Node.get_subclasses()])),
                keys=['type'],
                type=EntityErrors.INVALID_TYPE,
            )

        if 'project_id' not in cls.get_pg_properties():
            msg = (
                '{} is not an entity that can be upload via the project'
                ' endpoint.'
            )
            self.record_error(
                msg.format(cls.label), keys=['id'],
                type=EntityErrors.INVALID_TYPE
            )
Exemplo n.º 2
0
    def _set_node_properties(self):
        """
        Take the key, values from the dictionary (minus system keys) and set
        the value on the instances node, recording any errors.
        """
        self.logger.debug('Setting properties on {}'.format(self.node))
        entry = dictionary.schema.get(self.node.label, {})
        systemProperties = set(entry.get('systemProperties', []))

        special_keys = ['type', 'id', 'created_datetime', 'updated_datetime']
        pg_props = self.node.get_pg_properties()
        prop_keys = (pg_props.keys() + self.node._pg_links.keys()+special_keys)
        self.node.project_id = self.transaction.project_id
        default_props = self.get_system_property_defaults()

        # Set properties
        for key, val in self.doc.iteritems():

            # Does this key exist?
            if key not in prop_keys:
                msg = (
                    "Key '{}' is not a valid property for type '{}'.{}"
                    .format(
                        key, self.entity_type, get_suggestion(key, prop_keys)
                    ),
                )
                self.record_error(
                    msg, keys=[key], type=EntityErrors.INVALID_PROPERTY
                )

            # Skip type and id
            elif key in special_keys:
                pass

            # If key is a link, skip for now
            elif key in self.node._pg_links.keys():
                pass

            # Is it a system property?
            elif key in systemProperties:

                # If the property isn't set on the node, set the default
                if self.node._props.get(key, None) is None:
                    default = default_props.get(key, None)
                    self.logger.debug(
                        "{}: setting null system property '{}' to {}"
                        .format(self.node, key, default))
                    self.node._props[key] = default

                elif self.node._props.get(key) != val:
                    self.record_error(
                        ("Key '{}' is a system property and cannot be updated "
                         "from '{}' to '{}'")
                        .format(key, self.node._props.get(key), val),
                        keys=[key],
                        type=EntityErrors.INVALID_PERMISSIONS,
                    )

            # Otherwise, set the value
            else:
                try:
                    self.node._props[key] = val
                except Exception as e:  # pylint: disable=broad-except
                    self.record_error(
                        'Invalid property ({}): {}'.format(key, str(e)),
                        keys=[key],
                        type=EntityErrors.INVALID_PROPERTY,
                    )