Improve 'last played' strings

This commit is contained in:
Keith Edmunds 2021-08-14 23:06:16 +01:00
parent 89d49f3e34
commit 0836f74d17
2 changed files with 34 additions and 2 deletions

View File

@ -3,7 +3,13 @@ from datetime import datetime, date
def get_relative_date(past_date, reference_date=None):
"""
Return relative date as string.
Return how long before reference_date past_date is as string.
Params:
@past_date: datetime
@reference_date: datetime, defaults to current date and time
@return: string
"""
if not past_date:
@ -19,7 +25,15 @@ def get_relative_date(past_date, reference_date=None):
if weeks == days == 0:
# Played today, so return time instead
return past_date.strftime("%H:%M")
return f"{weeks} weeks, {days} days ago"
if weeks == 1:
weeks_str = "week"
else:
weeks_str = "weeks"
if days == 1:
days_str = "day"
else:
days_str = "days"
return f"{weeks} {weeks_str}, {days} {days_str} ago"
def ms_to_mmss(ms, decimals=0, negative=False):

View File

@ -148,6 +148,24 @@ class Playdates(Base):
(func.time(Playdates.lastplayed) > after),
).order_by(Playdates.lastplayed.desc()).first()
@staticmethod
def last_show(session, track):
"""
Return datetime track last played during show time or None
FIXME: hard coded times in here
"""
# dayofweek: 1 = Sunday, 2 = Monday, ..., 7 = Saturday.
friday = 6
after = time(20, 0)
return session.query(Playdates).filter(
(Playdates.track_id == track.id),
(func.dayofweek(Playdates.lastplayed) == friday),
(func.time(Playdates.lastplayed) > after),
).order_by(Playdates.lastplayed.desc()).first()
class Playlists(Base):
"""