def get_draft(draft_public_id, version, namespace_id, db_session): valid_public_id(draft_public_id) if version is None: raise InputError("Must specify draft version") try: version = int(version) except ValueError: raise InputError("Invalid draft version") try: draft = ( db_session.query(Message) .filter( Message.public_id == draft_public_id, Message.namespace_id == namespace_id, ) .one() ) except NoResultFound: raise NotFoundError("Couldn't find draft {}".format(draft_public_id)) if draft.is_sent or not draft.is_draft: raise InputError("Message {} is not a draft".format(draft_public_id)) if draft.version != version: raise ConflictError( "Draft {0}.{1} has already been updated to version {2}".format( draft_public_id, version, draft.version ) ) return draft
def thread_api_update(public_id): try: valid_public_id(public_id) thread = g.db_session.query(Thread).filter( Thread.public_id == public_id, Thread.namespace_id == g.namespace.id).one() except NoResultFound: raise NotFoundError("Couldn't find thread `{0}` ".format(public_id)) data = request.get_json(force=True) if not set(data).issubset({'add_tags', 'remove_tags', 'version'}): raise InputError('Can only add or remove tags from thread.') if (data.get('version') is not None and data.get('version') != thread.version): raise ConflictError('Thread {} has been updated to version {}'. format(thread.public_id, thread.version)) removals = data.get('remove_tags', []) for tag_identifier in removals: tag = g.db_session.query(Tag).filter( Tag.namespace_id == g.namespace.id, or_(Tag.public_id == tag_identifier, Tag.name == tag_identifier)).first() if tag is None: raise NotFoundError("Couldn't find tag {}".format(tag_identifier)) if not tag.user_removable: raise InputError('Cannot remove tag {}'.format(tag_identifier)) try: thread.remove_tag(tag, execute_action=True) except ActionError as e: return err(e.error, str(e)) additions = data.get('add_tags', []) for tag_identifier in additions: tag = g.db_session.query(Tag).filter( Tag.namespace_id == g.namespace.id, or_(Tag.public_id == tag_identifier, Tag.name == tag_identifier)).first() if tag is None: raise NotFoundError("Couldn't find tag {}".format(tag_identifier)) if not tag.user_addable: raise InputError('Cannot remove tag {}'.format(tag_identifier)) try: thread.apply_tag(tag, execute_action=True) except ActionError as e: return err(e.error, str(e)) g.db_session.commit() return g.encoder.jsonify(thread)