def _save_tag(self, tags, standard_attr_id): ctx = context.get_admin_context() for tag in tags: with ctx.session.begin(subtransactions=True): tag_db = tag_module.Tag(standard_attr_id=standard_attr_id, tag=tag) ctx.session.add(tag_db)
def update_tag(self, context, resource, resource_id, tag): res = self._get_resource(context, resource, resource_id) if any(tag == tag_db.tag for tag_db in res.standard_attr.tags): return try: with context.session.begin(subtransactions=True): tag_db = tag_model.Tag(standard_attr_id=res.standard_attr_id, tag=tag) context.session.add(tag_db) except db_exc.DBDuplicateEntry: pass
def update_tags(self, context, resource, resource_id, body): res = self._get_resource(context, resource, resource_id) new_tags = set(body['tags']) old_tags = {tag_db.tag for tag_db in res.standard_attr.tags} tags_added = new_tags - old_tags tags_removed = old_tags - new_tags with context.session.begin(subtransactions=True): for tag_db in res.standard_attr.tags: if tag_db.tag in tags_removed: context.session.delete(tag_db) for tag in tags_added: tag_db = tag_model.Tag(standard_attr_id=res.standard_attr_id, tag=tag) context.session.add(tag_db) return body