Skip to content

Commit

Permalink
Update streak last day
Browse files Browse the repository at this point in the history
  • Loading branch information
daya0576 committed Oct 1, 2024
1 parent 72fc307 commit 91799e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
21 changes: 5 additions & 16 deletions beaverhabits/frontend/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class CalendarHeatmap:
"""Habit records by weeks"""

today: datetime.date
month_last_day: datetime.date

headers: list[str]
data: list[list[datetime.date]]
Expand All @@ -195,18 +194,11 @@ class CalendarHeatmap:
def build(
cls, today: datetime.date, weeks: int, firstweekday: int = calendar.MONDAY
):
month_last_day = cls.get_month_last_day(today)

data = cls.generate_calendar_days(today, weeks, firstweekday)
headers = cls.generate_calendar_headers(data[0])
week_day_abbr = [calendar.day_abbr[(firstweekday + i) % 7] for i in range(7)]

return cls(today, month_last_day, headers, data, week_day_abbr)

@staticmethod
def get_month_last_day(today: datetime.date) -> datetime.date:
month_days = calendar.monthrange(today.year, today.month)[1]
return datetime.date(today.year, today.month, month_days)
return cls(today, headers, data, week_day_abbr)

@staticmethod
def generate_calendar_headers(days: list[datetime.date]) -> list[str]:
Expand Down Expand Up @@ -235,13 +227,10 @@ def generate_calendar_days(
total_weeks: int,
firstweekday: int = calendar.MONDAY, # 0 = Monday, 6 = Sunday
) -> list[list[datetime.date]]:
# First find last day of the month
last_date_of_month = CalendarHeatmap.get_month_last_day(today)

# Then find the last day of the week
# Find the last day of the week
lastweekday = (firstweekday - 1) % 7
days_delta = (lastweekday - last_date_of_month.weekday()) % 7
last_date_of_calendar = last_date_of_month + datetime.timedelta(days=days_delta)
days_delta = (lastweekday - today.weekday()) % 7
last_date_of_calendar = today + datetime.timedelta(days=days_delta)

return [
[
Expand Down Expand Up @@ -320,7 +309,7 @@ def habit_heat_map(
for i, weekday_days in enumerate(habit_calendar.data):
with ui.row(wrap=False).classes("gap-0"):
for day in weekday_days:
if day <= habit_calendar.month_last_day:
if day <= habit_calendar.today:
CalendarCheckBox(habit, day, today, ticked_data, is_bind_data)
else:
ui.label().style("width: 20px; height: 20px;")
Expand Down
5 changes: 3 additions & 2 deletions beaverhabits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ async def get_or_create_user_habit_list(

async def export_user_habit_list(habit_list: HabitList, user_identify: str) -> None:
# json to binary
now = datetime.datetime.now()
if isinstance(habit_list, DictHabitList):
data = {
"user_email": user_identify,
"exported_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"exported_at": now.strftime("%Y-%m-%d %H:%M:%S"),
**habit_list.data,
}
binary_data = json.dumps(data).encode()
file_name = f"habits_{int(float(time.time()))}.json"
file_name = f"beaverhabits_{now.strftime('%Y_%m_%d')}.json"
ui.download(binary_data, file_name)
else:
ui.notification("Export failed, please try again later.")

0 comments on commit 91799e9

Please sign in to comment.