def test_links_and_backlinks(session, test_user): bm1 = make_bookmark() bm2 = make_bookmark() bm3 = make_bookmark() bm4 = make_bookmark() set_bookmark(session, test_user.user_uuid, bm1) set_bookmark(session, test_user.user_uuid, bm2) set_bookmark(session, test_user.user_uuid, bm3) set_bookmark(session, test_user.user_uuid, bm4) upsert_links(session, bm1.url, {bm2.url, bm3.url}) upsert_links(session, bm4.url, {bm1.url}) (bm1_view,) = ( f for f in BookmarkViewQueryBuilder(session, test_user).execute() if f.bookmark == bm1 ) assert bm1_view.link_count == 2 assert bm1_view.backlink_count == 1 bm1_links = ( BookmarkViewQueryBuilder(session, test_user).links(bm1.url.url_uuid).execute() ) bm1_links_bvs = {bv.bookmark for bv in bm1_links} assert bm1_links_bvs == {bm2, bm3} bm1_backlinks = ( BookmarkViewQueryBuilder(session, test_user) .backlinks(bm1.url.url_uuid) .execute() ) bm1_links_bvs = {bv.bookmark for bv in bm1_backlinks} assert bm1_links_bvs == {bm4}
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 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 my_bookmarks(current_user: User) -> flask.Response: page = int(flask.request.args.get("page", "1")) qb = BookmarkViewQueryBuilder(db.session, current_user, page=page) if "q" in flask.request.args: search_str = flask.request.args["q"] tquery_str = parse_search_str(search_str) log.debug('search_str, tquery_str = ("%s", "%s")', search_str, tquery_str) qb = qb.text_search(tquery_str).order_by_search_rank() page_title = search_str else: qb = qb.order_by_created() page_title = "Quarchive" if page > 1: page_title += " (page %s)" % page prev_page_exists = qb.has_previous_page() next_page_exists = qb.has_next_page() return flask.make_response( flask.render_template( "bookmarks.html", h1="My bookmarks", page_title=page_title, bookmark_views=qb.execute(), page=page, prev_page_exists=prev_page_exists, next_page_exists=next_page_exists, search_query=flask.request.args.get("q", ""), user_tags=user_tags_data_fn(db.session, current_user), ))
def test_doubling_issue(session, test_user): """Test to check that a row doubling issue between tags and discussions has not been regressed. This is for bug #65. """ epoch_start = datetime(2018, 1, 3) bm = make_bookmark( tag_triples=set([("a", epoch_start, False), ("b", epoch_start, False)]) ) set_bookmark(session, test_user.user_uuid, bm) discussions = [ Discussion( external_id=str(random_numeric_id()), source=DiscussionSource.REDDIT, url=bm.url, comment_count=1, created_at=datetime(2018, 1, 3), title="example", ), Discussion( external_id=str(random_numeric_id()), source=DiscussionSource.HN, url=bm.url, comment_count=0, created_at=datetime(2018, 1, 3), title="example", ), ] upsert_discussions(session, discussions) bookmarks = list(f for f in BookmarkViewQueryBuilder(session, test_user).execute()) assert len(bookmarks) == 1
def test_discussion_digests_no_discussions(session, test_user): bm = make_bookmark() set_bookmark(session, test_user.user_uuid, bm) (bm1_view,) = (f for f in BookmarkViewQueryBuilder(session, test_user).execute()) assert bm1_view.discussion_digest.comment_count == 0 assert bm1_view.discussion_digest.discussion_count == 0 assert bm1_view.discussion_digest.sources == set()
def user_tag(current_user: User, username: str, tag: str) -> flask.Response: bookmark_views: Iterable[BookmarkView] = BookmarkViewQueryBuilder( db.session, current_user).with_tag(tag).execute() return flask.make_response( flask.render_template( "user_tag.html", bookmark_views=bookmark_views, tag=tag, page_title="Tagged as '%s'" % tag, user_tags=user_tags_data_fn(db.session, current_user), ))
def user_netloc(current_user: User, username: str, netloc: str) -> flask.Response: bookmark_views: Iterable[BookmarkView] = BookmarkViewQueryBuilder( db.session, current_user).with_netloc(netloc).execute() return flask.make_response( flask.render_template( "user_netloc.html", bookmark_views=bookmark_views, netloc=netloc, page_title="Bookmarks from '%s'" % netloc, user_tags=user_tags_data_fn(db.session, current_user), ))
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 test_discussion_digests(session, test_user): bm = make_bookmark() set_bookmark(session, test_user.user_uuid, bm) discussions = [ Discussion( external_id=str(random_numeric_id()), source=DiscussionSource.HN, url=bm.url, comment_count=1, created_at=datetime(2018, 1, 3), title="example", ), Discussion( external_id=str(random_numeric_id()), source=DiscussionSource.HN, url=bm.url, comment_count=0, created_at=datetime(2018, 1, 3), title="example", ), Discussion( external_id=str(random_numeric_id()), source=DiscussionSource.HN, url=bm.url, comment_count=100, created_at=datetime(2018, 1, 3), title="example", ), Discussion( external_id=str(random_numeric_id()), source=DiscussionSource.REDDIT, url=bm.url, comment_count=1, created_at=datetime(2018, 1, 3), title="example", ), ] upsert_discussions(session, discussions) (bm1_view,) = (f for f in BookmarkViewQueryBuilder(session, test_user).execute()) assert bm1_view.discussion_digest.comment_count == 102 assert bm1_view.discussion_digest.discussion_count == 4 assert bm1_view.discussion_digest.sources == { DiscussionSource.HN, DiscussionSource.REDDIT, }
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 test_icon_uuids_url_icon(session, test_user, canonical_url): bm1 = make_bookmark() set_bookmark(session, test_user.user_uuid, bm1) if canonical_url: canonical_url = bm1.url.follow("canonical.html") upsert_url(session, canonical_url) session.add( CanonicalUrl( non_canonical_url_uuid=bm1.url.url_uuid, canonical_url_uuid=canonical_url.url_uuid, ) ) icon_url = URL.from_string("http://example.com/" + random_string() + "/icon.png") upsert_url(session, icon_url) random_hash = random_bytes(64) icon_uuid = record_page_icon(session, icon_url, bm1.url, random_hash) (bm1_view,) = (f for f in BookmarkViewQueryBuilder(session, test_user).execute()) assert bm1_view.icon_uuid == icon_uuid
def test_simple_bookmark(session, test_user): bm = make_bookmark() set_bookmark(session, test_user.user_uuid, bm) (found,) = list(BookmarkViewQueryBuilder(session, test_user).execute()) assert found.bookmark == bm