mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Create task to purge old announcements
This commit is contained in:
parent
d7262595c6
commit
d75831f848
2 changed files with 28 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from datetime import datetime
|
||||
import datetime
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.models import UforaCourse, UforaAnnouncement
|
||||
|
|
@ -8,8 +8,8 @@ from database.models import UforaCourse, UforaAnnouncement
|
|||
|
||||
async def get_courses_with_announcements(session: AsyncSession) -> list[UforaCourse]:
|
||||
"""Get all courses where announcements are enabled"""
|
||||
query = select(UforaCourse).where(UforaCourse.log_announcements)
|
||||
return (await session.execute(query)).scalars().all()
|
||||
statement = select(UforaCourse).where(UforaCourse.log_announcements)
|
||||
return (await session.execute(statement)).scalars().all()
|
||||
|
||||
|
||||
async def create_new_announcement(
|
||||
|
|
@ -22,3 +22,14 @@ async def create_new_announcement(
|
|||
session.add(new_announcement)
|
||||
await session.commit()
|
||||
return new_announcement
|
||||
|
||||
|
||||
async def remove_old_announcements(session: AsyncSession):
|
||||
"""Delete all announcements that are > 8 days old
|
||||
The RSS feed only goes back 7 days, so all of these old announcements never have to
|
||||
be checked again when checking if an announcement is fresh or not.
|
||||
"""
|
||||
limit = datetime.datetime.utcnow() - datetime.timedelta(days=8)
|
||||
statement = delete(UforaAnnouncement).where(UforaAnnouncement.publication_date < limit)
|
||||
await session.execute(statement)
|
||||
await session.commit()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue