1
0
Fork 0

Add newt week resume for calendar

This commit is contained in:
Ethanell 2020-08-30 18:13:39 +02:00
parent 45d219e20e
commit fef4ee45b1
2 changed files with 34 additions and 12 deletions

View file

@ -59,6 +59,15 @@ class Calendar(Base):
events.append(e) events.append(e)
return list(events) 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): def week_embed(self, date: datetime.date):
date -= timedelta(days=date.weekday()) date -= timedelta(days=date.weekday())
embed = Embed(title=f"Week calendar: {self.name}", embed = Embed(title=f"Week calendar: {self.name}",
@ -93,6 +102,11 @@ class CalendarNotify(Base):
await channel.send(embed=embed) await channel.send(embed=embed)
async def next_day_resume(self, bot: Bot): 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) channel = bot.get_channel(self.channel)
if channel: if channel:
await channel.send(embed=self.calendar.week_embed((datetime.now() + timedelta(days=1)).date())) await channel.send(embed=self.calendar.week_embed((datetime.now() + timedelta(days=1)).date()))

View file

@ -124,10 +124,9 @@ class Calendar(commands.Cog):
date = datetime.strptime(day, "%d/%m/%Y").date() date = datetime.strptime(day, "%d/%m/%Y").date()
except ValueError: except ValueError:
raise BadArgument() raise BadArgument()
embed = Embed(title=f"Day calendar: {c.name}", description=date.strftime("%d/%m/%Y"))
for e in c.events(date, date): embed = c.day_embed(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)
s = db.Session() s = db.Session()
if s.is_modified(c): if s.is_modified(c):
s.add(c) s.add(c)
@ -233,23 +232,32 @@ class Calendar(commands.Cog):
async def calendar_notify_loop(self): async def calendar_notify_loop(self):
s = db.Session() s = db.Session()
now = datetime.now().astimezone(tz=None) now = datetime.now().astimezone(tz=None)
for c in s.query(db.Calendar).all(): for c in s.query(db.Calendar).all():
if now.isoweekday() not in [5, 6] and now.time() >= time(hour=20) and\ if now.time() >= time(hour=20) and c.last_notify.astimezone(tz=None) < now.replace(hour=20, minute=00) and\
c.last_notify.astimezone(tz=None) < now.replace(hour=20, minute=00): now.isoweekday() not in [5, 6]:
c.last_notify = datetime.now() c.last_notify = now
s.add(c)
s.commit()
for n in c.calendars_notify: for n in c.calendars_notify:
if now.isoweekday() == 7:
await n.next_week_resume(self.bot)
else:
await n.next_day_resume(self.bot) await n.next_day_resume(self.bot)
for e in c.events(now.date(), now.date()): for e in c.events(now.date(), now.date()):
if xor(c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=30) <= now, 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.astimezone(tz=None) < e.begin - timedelta(minutes=10) <= now):
c.last_notify = datetime.now() c.last_notify = now
for n in c.calendars_notify:
await n.notify(self.bot, e)
break
if s.is_modified(c):
s.add(c) s.add(c)
s.commit() s.commit()
for n in c.calendars_notify:
await n.notify(self.bot, e)
break
s.close() s.close()
@commands.Cog.listener() @commands.Cog.listener()