-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add tables + APIs for bestbook feature #10398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6c312cf
Adding db schema and models for best book awards
noobCoderVP 7a3b2bb
Adding best book awards count API
noobCoderVP aa07d5d
Adds best book models and APIs
noobCoderVP 243ab48
update tests and core bestbook functionality
mekarpeles 04d16f6
Add bestbook to account anonymization flow
mekarpeles 65201fe
add bestbooks to resolve redirects flow
mekarpeles e61521f
rename submitter to username
mekarpeles 3dd9f45
updating typehints
mekarpeles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,193 @@ | ||||||
from openlibrary.core.bookshelves import Bookshelves | ||||||
|
||||||
from . import db | ||||||
|
||||||
|
||||||
class Bestbook(db.CommonExtras): | ||||||
"""Best book award operations""" | ||||||
|
||||||
TABLENAME = "bestbooks" | ||||||
PRIMARY_KEY = "nomination_id" | ||||||
ALLOW_DELETE_ON_CONFLICT = False | ||||||
|
||||||
class AwardConditionsError(Exception): | ||||||
pass | ||||||
|
||||||
@classmethod | ||||||
def prepare_query( | ||||||
cls, | ||||||
select: str = "*", | ||||||
work_id: str | None = None, | ||||||
username: str | None = None, | ||||||
topic: str | None = None, | ||||||
) -> tuple[str, dict]: | ||||||
"""Prepare query for fetching bestbook awards""" | ||||||
conditions = [] | ||||||
filters = { | ||||||
'work_id': work_id, | ||||||
'username': username, | ||||||
'topic': topic, | ||||||
} | ||||||
vars = {} | ||||||
|
||||||
for key, value in filters.items(): | ||||||
if value is not None: | ||||||
conditions.append(f"{key}=${key}") | ||||||
vars[key] = value | ||||||
query = f"SELECT {select} FROM {cls.TABLENAME}" | ||||||
if conditions: | ||||||
query += " WHERE " + " AND ".join(conditions) | ||||||
mekarpeles marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return query, vars | ||||||
|
||||||
@classmethod | ||||||
def get_count( | ||||||
cls, | ||||||
work_id: str | None = None, | ||||||
username: str | None = None, | ||||||
topic: str | None = None, | ||||||
) -> int: | ||||||
"""Used to get count of awards with different filters""" | ||||||
oldb = db.get_db() | ||||||
query, vars = cls.prepare_query( | ||||||
select="count(*)", work_id=work_id, username=username, topic=topic | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alias the count column to ensure consistency (e.g. use
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
) | ||||||
result = oldb.query(query, vars=vars) | ||||||
return result[0]['count'] if result else 0 | ||||||
|
||||||
@classmethod | ||||||
def get_awards( | ||||||
cls, | ||||||
work_id: str | None = None, | ||||||
username: str | None = None, | ||||||
topic: str | None = None, | ||||||
) -> list: | ||||||
"""Fetches a list of bestbook awards based on the provided filters. | ||||||
|
||||||
This method queries the database to retrieve awards associated with a | ||||||
specific work, submitted by a particular user, or related to a given topic. | ||||||
""" | ||||||
oldb = db.get_db() | ||||||
query, vars = cls.prepare_query( | ||||||
select="*", work_id=work_id, username=username, topic=topic | ||||||
) | ||||||
result = oldb.query(query, vars=vars) | ||||||
return list(result) if result else [] | ||||||
|
||||||
@classmethod | ||||||
def add( | ||||||
cls, | ||||||
username: str, | ||||||
work_id: str, | ||||||
topic: str, | ||||||
comment: str = "", | ||||||
edition_id: int | None = None, | ||||||
) -> int | None: | ||||||
"""Add award to database only if award doesn't exist previously | ||||||
or raises Bestbook.AwardConditionsError | ||||||
""" | ||||||
# Raise cls.AwardConditionsError if any failing conditions | ||||||
cls._check_award_conditions(username, work_id, topic) | ||||||
|
||||||
oldb = db.get_db() | ||||||
|
||||||
return oldb.insert( | ||||||
cls.TABLENAME, | ||||||
username=username, | ||||||
work_id=work_id, | ||||||
edition_id=edition_id, | ||||||
topic=topic, | ||||||
comment=comment, | ||||||
) | ||||||
|
||||||
@classmethod | ||||||
def remove( | ||||||
cls, username: str, work_id: str | None = None, topic: str | None = None | ||||||
) -> int: | ||||||
"""Remove any award for this username where either work_id or topic matches.""" | ||||||
if not work_id and not topic: | ||||||
raise ValueError("Either work_id or topic must be specified.") | ||||||
|
||||||
oldb = db.get_db() | ||||||
|
||||||
# Build WHERE clause dynamically | ||||||
conditions = [] | ||||||
if work_id: | ||||||
conditions.append("work_id = $work_id") | ||||||
if topic: | ||||||
conditions.append("topic = $topic") | ||||||
|
||||||
# Combine with AND for username and OR for other conditions | ||||||
where_clause = f"username = $username AND ({' OR '.join(conditions)})" | ||||||
|
||||||
try: | ||||||
return oldb.delete( | ||||||
cls.TABLENAME, | ||||||
where=where_clause, | ||||||
vars={ | ||||||
'username': username, | ||||||
'work_id': work_id, | ||||||
'topic': topic, | ||||||
}, | ||||||
) | ||||||
except LookupError: # No matching rows found | ||||||
return 0 | ||||||
|
||||||
@classmethod | ||||||
def get_leaderboard(cls) -> list[dict]: | ||||||
"""Get the leaderboard of best books""" | ||||||
oldb = db.get_db() | ||||||
result = db.select( | ||||||
cls.TABLENAME, | ||||||
what='work_id, COUNT(*) AS count', | ||||||
group='work_id', | ||||||
order='count DESC', | ||||||
) | ||||||
return list(result) if result else [] | ||||||
|
||||||
@classmethod | ||||||
def _check_award_conditions(cls, username: str, work_id: str, topic: str) -> bool: | ||||||
""" | ||||||
Validates the conditions for adding a bestbook award. | ||||||
|
||||||
This method checks if the provided work ID and topic meet the necessary | ||||||
conditions for adding a best book award. It ensures that: | ||||||
- Both a work ID and a topic are provided. | ||||||
- The user has marked the book as read. | ||||||
- The work has not already been nominated for a best book award by the user. | ||||||
- The topic has not already been nominated for a best book award by the user. | ||||||
|
||||||
If any of these conditions are not met, it raises a Bestbook.AwardConditionsError | ||||||
with the appropriate error messages. | ||||||
""" | ||||||
errors = [] | ||||||
|
||||||
if not (work_id and topic): | ||||||
errors.append( | ||||||
"A work ID and a topic are both required for best book awards" | ||||||
) | ||||||
|
||||||
else: | ||||||
has_read_book = Bookshelves.user_has_read_work( | ||||||
username=username, work_id=work_id | ||||||
) | ||||||
awarded_book = cls.get_awards(username=username, work_id=work_id) | ||||||
awarded_topic = cls.get_awards(username=username, topic=topic) | ||||||
|
||||||
if not has_read_book: | ||||||
errors.append( | ||||||
"Only books which have been marked as read may be given awards" | ||||||
) | ||||||
if awarded_book: | ||||||
errors.append( | ||||||
"A work may only be nominated one time for a best book award" | ||||||
) | ||||||
if awarded_topic: | ||||||
errors.append( | ||||||
f"A topic may only be nominated one time for a best book award: " | ||||||
f"The work {awarded_topic[0].work_id} has already been nominated " | ||||||
f"for topic {awarded_topic[0].topic}" | ||||||
) | ||||||
|
||||||
if errors: | ||||||
raise cls.AwardConditionsError(" ".join(errors)) | ||||||
return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.