Skip to content

Commit 75ee14b

Browse files
committed
discord memes - weeks % sunday
1 parent 26139bd commit 75ee14b

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

dojo_plugin/api/v1/discord.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,41 @@ class CourseMemes(Resource):
139139
@authed_only
140140
@dojo_route
141141
def get(self, dojo):
142+
143+
from datetime import timedelta, timezone
144+
145+
def bucket_from_start_then_sundays(ts, course_start):
146+
start = course_start.astimezone(timezone.utc)
147+
148+
wd = start.weekday()
149+
days_until_next_sunday = (6 - wd) % 7
150+
if days_until_next_sunday == 0:
151+
days_until_next_sunday = 7 # if start is Sunday, first boundary is a week later
152+
153+
first_boundary = start + timedelta(days=days_until_next_sunday) # first Sunday 00:00 after start
154+
155+
if ts < first_boundary:
156+
return 0
157+
return 1 + (ts.date() - first_boundary.date()).days // 7
158+
142159
discord_user = DiscordUsers.query.filter_by(user_id=get_current_user().id).first()
160+
161+
if not discord_user:
162+
return {"success": False, "error": "Discord not linked"}
143163
course_start = dojo.course.get("start_date", None)
144164
if course_start is None:
145165
return {"success": False, "error": "No course start"}
146166
course_start = datetime.fromisoformat(course_start)
147167

148-
memes = (discord_user.memes(start=course_start, end=course_start + timedelta(weeks=16))
149-
.order_by(DiscordUserActivity.message_timestamp))
150-
valid_meme_cnt = len(set((meme.message_timestamp.astimezone(timezone.utc) - course_start).days // 7
151-
for meme in memes))
168+
memes = (
169+
discord_user.memes(start=course_start, end=course_start + timedelta(weeks=17))
170+
.order_by(DiscordUserActivity.message_timestamp)
171+
)
172+
173+
valid_meme_cnt = len({
174+
bucket_from_start_then_sundays(m.message_timestamp, course_start)
175+
for m in memes
176+
})
152177

153178
return {"success": False, "memes": valid_meme_cnt}
154179

0 commit comments

Comments
 (0)