def edit_bookmark(username: str, url_uuid: UUID) -> flask.Response: owner = get_user_or_fail(db.session, username) require_access_or_fail( BookmarkAccessObject(user_uuid=owner.user_uuid, url_uuid=url_uuid), Access.WRITE, ) form = flask.request.form existing_bookmark = get_bookmark_by_url_uuid(db.session, owner.user_uuid, url_uuid) if existing_bookmark is None: raise exc.NotFound() updated_bookmark = Bookmark( url=existing_bookmark.url, created=existing_bookmark.created, title=form["title"], description=form["description"], unread="unread" in form, deleted="deleted" in form, updated=datetime.utcnow().replace(tzinfo=timezone.utc), tag_triples=tag_triples_from_form( form, current=existing_bookmark.tag_triples), ) merged_bookmark = updated_bookmark.merge(existing_bookmark) set_bookmark(db.session, get_cache(), owner.user_uuid, merged_bookmark) db.session.commit() flask.flash("Edited: %s" % merged_bookmark.title) return flask.make_response("ok")
def bookmark_from_sqla(url: URL, sqla_obj: SQLABookmark) -> Bookmark: return Bookmark( url=url, created=sqla_obj.created, description=sqla_obj.description, updated=sqla_obj.updated, unread=sqla_obj.unread, deleted=sqla_obj.deleted, title=sqla_obj.title, tag_triples=frozenset( (btag.tag_obj.tag_name, btag.updated, btag.deleted) for btag in sqla_obj.bookmark_tag_objs), )
def create_bookmark(username: str) -> flask.Response: owner = get_user_or_fail(db.session, username) # FIXME: sort out optional url_uuid require_access_or_fail( UserBookmarksAccessObject(user_uuid=owner.user_uuid), Access.WRITE, ) form = flask.request.form creation_time = datetime.utcnow().replace(tzinfo=timezone.utc) tag_triples = tag_triples_from_form(form) url_str = form["url"] try: # As it's a user entering this url, help them along with getting a # sufficiently canonicalised url url = URL.from_string(url_str, coerce_canonicalisation=True) except DisallowedSchemeException: log.warning("user tried to create url: %s (disallowed scheme)", url_str) flask.abort(400, "invalid url (disallowed scheme)") bookmark = Bookmark( url=url, title=form["title"], description=form["description"], unread="unread" in form, deleted=False, updated=creation_time, created=creation_time, tag_triples=tag_triples, ) url_uuid = set_bookmark(db.session, get_cache(), owner.user_uuid, bookmark) db.session.commit() publish_message( message_lib.BookmarkCreated(user_uuid=owner.user_uuid, url_uuid=url.url_uuid), environ["QM_RABBITMQ_BG_WORKER_TOPIC"], ) flask.flash("Bookmarked: %s" % bookmark.title) response = flask.make_response("Redirecting...", 303) response.headers["Location"] = flask.url_for( "quarchive.edit_bookmark_form", url_uuid=url_uuid, username=owner.username, ) return response
def create_bookmark() -> flask.Response: form = flask.request.form creation_time = datetime.utcnow().replace(tzinfo=timezone.utc) tag_triples = tag_triples_from_form(form) bookmark = Bookmark( url=URL.from_string(form["url"]), title=form["title"], description=form["description"], unread="unread" in form, deleted=False, updated=creation_time, created=creation_time, tag_triples=tag_triples, ) url_uuid = set_bookmark(db.session, get_current_user().user_uuid, bookmark) db.session.commit() flask.flash("Bookmarked: %s" % bookmark.title) response = flask.make_response("Redirecting...", 303) response.headers["Location"] = flask.url_for( "quarchive.edit_bookmark_form", url_uuid=url_uuid ) return response