Skip to content

Commit

Permalink
moved functions to the correct file
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinc16 committed Jun 6, 2024
1 parent ac53068 commit 5af03de
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
32 changes: 0 additions & 32 deletions openlibrary/core/bookshelves_events.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import date, datetime
from enum import IntEnum

from openlibrary.utils.dateutil import DATE_ONE_MONTH_AGO, DATE_ONE_WEEK_AGO
from . import db


Expand All @@ -19,20 +18,6 @@ class BookshelvesEvents(db.CommonExtras):
TABLENAME = 'bookshelves_events'
NULL_EDITION_ID = -1

@classmethod
def summary(cls):
return {
'total_yearly_reading_goals': {
'total': BookshelvesEvents.total_yearly_reading_goals(),
'month': BookshelvesEvents.total_yearly_reading_goals(
since=DATE_ONE_MONTH_AGO
),
'week': BookshelvesEvents.total_yearly_reading_goals(
since=DATE_ONE_WEEK_AGO
),
},
}

# Create methods:
@classmethod
def create_event(
Expand Down Expand Up @@ -140,23 +125,6 @@ def select_distinct_by_user_type_and_year(cls, username, event_type, year):

return list(oldb.query(query, vars=data))

@classmethod
def total_yearly_reading_goals(cls, since: date | None = None) -> int:
"""Returns a Storage object of <Storage {'count': int}> where 'count' specifies the
number reading goals updated. `since` may be used
to limit the result to those reading goals updated since a specific
date. Any python datetime.date type should work.
:param since: returns all reading goals after date
"""
oldb = db.get_db()

query = "SELECT count(*) from yearly_reading_goals"
if since:
query += " WHERE updated >= $since"
results = tuple(oldb.query(query, vars={'since': since}))
print('total_yearly_reading_goals', results)
return results[0]

# Update methods:
@classmethod
def update_event(cls, pid, edition_id=None, event_date=None, data=None):
Expand Down
34 changes: 33 additions & 1 deletion openlibrary/core/yearly_reading_goals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from datetime import datetime
from datetime import date, datetime

from openlibrary.utils.dateutil import DATE_ONE_MONTH_AGO, DATE_ONE_WEEK_AGO
from . import db


class YearlyReadingGoals:
TABLENAME = 'yearly_reading_goals'

@classmethod
def summary(cls):
return {
'total_yearly_reading_goals': {
'total': YearlyReadingGoals.total_yearly_reading_goals(),
'month': YearlyReadingGoals.total_yearly_reading_goals(
since=DATE_ONE_MONTH_AGO
),
'week': YearlyReadingGoals.total_yearly_reading_goals(
since=DATE_ONE_WEEK_AGO
),
},
}

# Create methods:
@classmethod
def create(cls, username: str, year: int, target: int):
Expand Down Expand Up @@ -52,6 +68,22 @@ def has_reached_goal(cls, username: str, year: int) -> bool:
else:
return results[0]['current'] >= results[0]['target']

@classmethod
def total_yearly_reading_goals(cls, since: date | None = None) -> int:
"""Returns a Storage object of <Storage {'count': int}> where 'count' specifies the
number reading goals updated. `since` may be used
to limit the result to those reading goals updated since a specific
date. Any python datetime.date type should work.
:param since: returns all reading goals after date
"""
oldb = db.get_db()

query = f"SELECT count(*) from {cls.TABLENAME}"
if since:
query += " WHERE updated >= $since"
results = list(oldb.query(query, vars={'since': since}))
return results[0]

# Update methods:
@classmethod
def update_current_count(cls, username: str, year: int, current_count: int):
Expand Down
6 changes: 4 additions & 2 deletions openlibrary/tests/core/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,12 @@ def teardown_method(self):
self.db.query('delete from yearly_reading_goals')

def test_total_yearly_reading_goals(self):
assert BookshelvesEvents.total_yearly_reading_goals()['count(*)'] == 3
assert YearlyReadingGoals.total_yearly_reading_goals()['count(*)'] == 3
# Combination of issues
assert (
BookshelvesEvents.total_yearly_reading_goals(since="2022-12-25")['count(*)']
YearlyReadingGoals.total_yearly_reading_goals(since="2022-12-25")[
'count(*)'
]
== 1
)

Expand Down
4 changes: 2 additions & 2 deletions openlibrary/views/loanstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..core.booknotes import Booknotes
from ..core.follows import PubSub
from ..core.bookshelves import Bookshelves
from ..core.bookshelves_events import BookshelvesEvents
from ..core.yearly_reading_goals import YearlyReadingGoals
from ..core.ratings import Ratings
from ..plugins.admin.code import get_counts
from ..plugins.worksearch.code import get_solr_works
Expand All @@ -37,7 +37,7 @@ def reading_log_summary():
delegate.fakeload()

stats = Bookshelves.summary()
stats.update(BookshelvesEvents.summary())
stats.update(YearlyReadingGoals.summary())
stats.update(Ratings.summary())
stats.update(Observations.summary())
stats.update(Booknotes.summary())
Expand Down

0 comments on commit 5af03de

Please sign in to comment.