Skip to content

Commit

Permalink
Limit calculation of top_manual users to last 4 weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
hackenbergstefan committed Aug 12, 2024
1 parent 32afa98 commit c6c24d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion coffeebuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import OperationalError

__version__ = "1.2.0"
__version__ = "1.2.1"

db = SQLAlchemy()
login_manager = flask_login.LoginManager()
Expand Down
11 changes: 10 additions & 1 deletion coffeebuddy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,23 @@ def update_bill(self, newbill):
flask.current_app.db.session.add(Pay(user=self, amount=self.unpayed - newbill))

@staticmethod
def top_selected_manually(limit: int = 5) -> List["User"]:
def top_selected_manually(
limit: int = 5,
since=timedelta(weeks=4),
) -> List["User"]:
db = flask.current_app.db
now = date.today()
since = (
datetime.combine(now - timedelta(now.weekday()), datetime.min.time())
- since
)
return db.session.scalars(
db.select(User)
.where(
(Drink.userid == User.id)
& (Drink.host == socket.gethostname())
& (Drink.selected_manually)
& (db.func.Date(Drink.timestamp) >= since)
)
.group_by(User.id)
.limit(limit)
Expand Down

0 comments on commit c6c24d8

Please sign in to comment.