def test_share_params(): user_uuid = uuid4() url_uuid = uuid4() subj = BookmarkAccessObject(user_uuid=user_uuid, url_uuid=url_uuid) assert subj.to_params() == { "user_uuid": user_uuid.hex, "url_uuid": url_uuid.hex, } assert BookmarkAccessObject.from_params(subj.to_params()) == subj
def backlinks(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.READ) bookmark = get_bookmark_by_url_uuid_or_fail(db.session, owner.user_uuid, url_uuid) page = int(flask.request.args.get("page", "1")) qb = (BookmarkViewQueryBuilder( db.session, owner, page=page).backlinks(url_uuid).order_by_created()) title = f"Links to '{bookmark.title}'" current_user = get_current_user() if current_user is not None: user_tags = user_tags_data_fn(db.session, current_user) else: user_tags = [] return flask.make_response( flask.render_template( "bookmarks.html", h1=title, page_title=title, bookmark_views=qb.execute(), page=page, prev_page_exists=qb.has_previous_page(), next_page_exists=qb.has_next_page(), search_query=False, user_tags=user_tags, ))
def view_share(username: str, url_uuid: UUID, base64_share_token: str) -> flask.Response: owner = get_user_or_fail(db.session, username) access_object = BookmarkAccessObject(user_uuid=owner.user_uuid, url_uuid=url_uuid) require_access_or_fail( access_object, Access.READACCESS, ) share_grant = share_grant_or_fail(db.session, base64_share_token) bv = list( BookmarkViewQueryBuilder(db.session, owner).only_url(url_uuid).execute())[0] sharelink = flask.url_for( "quarchive.sharelink", base64_share_token=share_grant.base64_token(), _external=True, ) return flask.make_response( flask.render_template( "sharelinks.html", bookmark_view=bv, share_grant=share_grant, sharelink=sharelink, ))
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 edit_bookmark_form(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) bookmark = get_bookmark_by_url_uuid_or_fail(db.session, owner.user_uuid, url_uuid) # Step one, load the template kwargs from the bookmark template_kwargs: Dict[str, Any] = dict( url=bookmark.url.to_string(), title=bookmark.title, description=bookmark.description, page_title="Edit %s" % bookmark.title, url_uuid=url_uuid, tags=bookmark.current_tags(), owner=owner, ) if bookmark.unread: template_kwargs["unread"] = "on" # Then update it from the querystring template_kwargs.update(form_fields_from_querystring(flask.request.args)) template_kwargs["user_tags"] = user_tags_data_fn(db.session, owner) template_kwargs["deleted"] = bookmark.deleted return flask.make_response( flask.render_template("edit_bookmark.html", **template_kwargs))
def get_share_grant_by_token(session: Session, share_token: bytes) -> Optional[ShareGrant]: rs = (session.query( SQLShareGrant.share_token, SQLShareGrant.revoked, SQLAccessObject.access_object_name, SQLAccessObject.params, SQLShareGrant.access_verb_id, ).join( SQLAccessObject, SQLShareGrant.access_object_id == SQLAccessObject.access_object_id, ).filter(SQLShareGrant.share_token == share_token).first()) if rs is None: return None else: share_token, revoked, ao_name, ao_params, av_id = rs access_object = BookmarkAccessObject.from_params(ao_params) access_verb = Access(av_id) return ShareGrant( share_token=share_token, expiry=None, access_object=access_object, access_verb=access_verb, revoked=revoked, )
def share_form(username: str, url_uuid: UUID): owner = get_user_or_fail(db.session, username) require_access_or_fail( BookmarkAccessObject(user_uuid=owner.user_uuid, url_uuid=url_uuid), Access.WRITEACCESS, ) bv = list( BookmarkViewQueryBuilder(db.session, owner).only_url(url_uuid).execute())[0] return flask.make_response( flask.render_template("share-form.html", bookmark_view=bv))
def bookmark_archives(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.READ) bookmark = get_bookmark_by_url_uuid_or_fail(db.session, owner.user_uuid, url_uuid) archive_links = get_archive_links(bookmark.url, circa=bookmark.created) return flask.make_response( flask.render_template( "archives.html", page_title=f'Archives of "{bookmark.title}"', bookmark=bookmark, archive_links=archive_links, Archive=Archive, ))
def quick_add_tag(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) bookmark = get_bookmark_by_url_uuid_or_fail(db.session, owner.user_uuid, url_uuid) tag = flask.request.form["tag"] set_bookmark(db.session, owner.user_uuid, bookmark.with_tag(tag)) db.session.commit() flask.flash(f"Tagged '{bookmark.title}' with '{tag}'") 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 discussions(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.READ) (bookmark_view, ) = (BookmarkViewQueryBuilder( db.session, owner).only_url(url_uuid).execute()) discussion_views = get_discussions_by_url(db.session, bookmark_view.bookmark.url) print(discussion_views) return flask.make_response( flask.render_template( "discussions.html", page_title=f'Discussions on "{bookmark_view.title()}"', bookmark_view=bookmark_view, discussion_views=discussion_views, Archive=Archive, ))
def view_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.READ, ) qb = BookmarkViewQueryBuilder(db.session, owner).only_url(url_uuid) current_user = get_current_user() if current_user is not None: user_tags = user_tags_data_fn(db.session, current_user) else: user_tags = [] return flask.make_response( flask.render_template( "bookmarks.html", bookmark_views=qb.execute(), search_query=False, pagination=False, user_tags=user_tags, ))
def create_share(username: str, url_uuid: UUID) -> flask.Response: owner = get_user_or_fail(db.session, username) access_object = BookmarkAccessObject(user_uuid=owner.user_uuid, url_uuid=url_uuid) require_access_or_fail( access_object, Access.WRITEACCESS, ) share_grant = create_share_grant(db.session, access_object, Access.READ) db.session.commit() base64_share_token = share_grant.base64_token() response = flask.make_response("Redirecting...", 303) response.headers["Location"] = flask.url_for( "quarchive.view_share", username=username, url_uuid=url_uuid, base64_share_token=base64_share_token, ) return response
owner = User( user_uuid=uuid4(), username="******", email=None, timezone=pytz.UTC, registered=datetime.utcnow(), ) other = User( user_uuid=uuid4(), username="******", email=None, timezone=pytz.UTC, registered=datetime.utcnow(), ) test_subject = BookmarkAccessObject(user_uuid=owner.user_uuid, url_uuid=uuid4()) test_grant = ShareGrant( share_token=b"", expiry=None, access_object=test_subject, access_verb=Access.READ, revoked=False, ) irrelevant_grant = ShareGrant( share_token=b"", expiry=None, access_object=BookmarkAccessObject(user_uuid=uuid4(), url_uuid=uuid4()), access_verb=Access.READWRITE, revoked=False, )