From fef4ee45b1913003e31e24dfcc66160583d4ee3f Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 30 Aug 2020 18:13:39 +0200 Subject: [PATCH] Add newt week resume for calendar --- db/Calendar.py | 14 ++++++++++++++ extensions/calendar.py | 32 ++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/db/Calendar.py b/db/Calendar.py index f2b5a7c..4afef6b 100644 --- a/db/Calendar.py +++ b/db/Calendar.py @@ -59,6 +59,15 @@ class Calendar(Base): events.append(e) return list(events) + def day_embed(self, date: datetime.date): + embed = Embed(title=f"Day calendar: {self.name}", description=date.strftime("%d/%m/%Y")) + + for e in self.events(date, date): + embed.add_field(name=f"{e.begin.strftime('%H:%M')} - {e.end.strftime('%H:%M')}", + value=f"{e.name} | {e.location} - {e.organizer}", inline=False) + + return embed + def week_embed(self, date: datetime.date): date -= timedelta(days=date.weekday()) embed = Embed(title=f"Week calendar: {self.name}", @@ -93,6 +102,11 @@ class CalendarNotify(Base): await channel.send(embed=embed) async def next_day_resume(self, bot: Bot): + channel = bot.get_channel(self.channel) + if channel: + await channel.send(embed=self.calendar.day_embed((datetime.now() + timedelta(days=1)).date())) + + async def next_week_resume(self, bot: Bot): channel = bot.get_channel(self.channel) if channel: await channel.send(embed=self.calendar.week_embed((datetime.now() + timedelta(days=1)).date())) diff --git a/extensions/calendar.py b/extensions/calendar.py index 6dcbfab..21b7e1e 100644 --- a/extensions/calendar.py +++ b/extensions/calendar.py @@ -124,10 +124,9 @@ class Calendar(commands.Cog): date = datetime.strptime(day, "%d/%m/%Y").date() except ValueError: raise BadArgument() - embed = Embed(title=f"Day calendar: {c.name}", description=date.strftime("%d/%m/%Y")) - for e in c.events(date, date): - embed.add_field(name=f"{e.begin.strftime('%H:%M')} - {e.end.strftime('%H:%M')}", - value=f"{e.name} | {e.location} - {e.organizer}", inline=False) + + embed = c.day_embed(date) + s = db.Session() if s.is_modified(c): s.add(c) @@ -233,23 +232,32 @@ class Calendar(commands.Cog): async def calendar_notify_loop(self): s = db.Session() now = datetime.now().astimezone(tz=None) + for c in s.query(db.Calendar).all(): - if now.isoweekday() not in [5, 6] and now.time() >= time(hour=20) and\ - c.last_notify.astimezone(tz=None) < now.replace(hour=20, minute=00): - c.last_notify = datetime.now() + if now.time() >= time(hour=20) and c.last_notify.astimezone(tz=None) < now.replace(hour=20, minute=00) and\ + now.isoweekday() not in [5, 6]: + c.last_notify = now + s.add(c) + s.commit() + for n in c.calendars_notify: - await n.next_day_resume(self.bot) + if now.isoweekday() == 7: + await n.next_week_resume(self.bot) + else: + await n.next_day_resume(self.bot) for e in c.events(now.date(), now.date()): if xor(c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=30) <= now, c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=10) <= now): - c.last_notify = datetime.now() + c.last_notify = now + s.add(c) + s.commit() + for n in c.calendars_notify: await n.notify(self.bot, e) + break - if s.is_modified(c): - s.add(c) - s.commit() + s.close() @commands.Cog.listener()