Beispiel #1
0
def _setting_update(values, setting_uuid):
    """
    Used internally by setting_create and setting_update

    :param values: A dict of attributes to set
    :param setting_uuid: If None, create the setting, otherwise,
                        find and update it
    """
    session = get_session()
    with session.begin():
        # should not update uuid
        values.pop("uuid", None)
        # Remove the properties passed in the values mapping. We
        # handle properties separately from base setting attributes,
        # and leaving properties in the values mapping will cause
        # a SQLAlchemy model error because SQLAlchemy expects the
        # properties attribute of an setting model to be a list and
        # not a dict.
        if setting_uuid:
            setting_ref = setting_get(setting_uuid, session=session)
        else:
            setting_ref = models.Settings()
        if setting_ref:
            # Don't drop created_at if we're passing it in...
            _drop_protected_attrs(models.Settings, values)
            values['updated_at'] = timeutils.utcnow()
        setting_ref.update(values)
        # Should not set duplicate level and type pair
        if setting_ref.level is not None and setting_ref.type is not None:
            try:
                setting_dup = setting_get_by_lever_type(setting_ref.level,
                                                    setting_ref.type)
            except exception.NotFound:
                setting_dup = None
            if setting_dup and not setting_uuid:
                raise exception.Duplicate(_("Setting level(%s)"
                                          "-type(%s) pair already exists!") % \
                                          (setting_ref.level,
                                           setting_ref.type))
        # Validate the attributes before we go any further. From my
        # investigation, the @validates decorator does not validate
        # on new records, only on existing records, which is, well,
        # idiotic.
        _update_values(setting_ref, values)

        try:
            setting_ref.save(session=session)
        except sqlalchemy.exc.IntegrityError:
            raise exception.Duplicate(_("Setting uuid already exists!"))

    return setting_get(setting_ref.uuid)
Beispiel #2
0
 def delete(self, session=None):
     """Delete this object"""
     self.deleted = True
     self.deleted_at = timeutils.utcnow()
     self.save(session=session)