mirror of https://github.com/stijndcl/didier
Store announcement date in db
parent
6873cab955
commit
d7262595c6
|
@ -1,8 +1,8 @@
|
||||||
"""Initial migration: Ufora announcements
|
"""Initial migration
|
||||||
|
|
||||||
Revision ID: 9e8ce58c0a26
|
Revision ID: 4ec79dd5b191
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2022-06-17 01:36:02.767151
|
Create Date: 2022-06-19 00:31:58.384360
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
@ -10,7 +10,7 @@ import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '9e8ce58c0a26'
|
revision = '4ec79dd5b191'
|
||||||
down_revision = None
|
down_revision = None
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
|
@ -23,6 +23,7 @@ def upgrade() -> None:
|
||||||
sa.Column('name', sa.Text(), nullable=False),
|
sa.Column('name', sa.Text(), nullable=False),
|
||||||
sa.Column('code', sa.Text(), nullable=False),
|
sa.Column('code', sa.Text(), nullable=False),
|
||||||
sa.Column('year', sa.Integer(), nullable=False),
|
sa.Column('year', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('log_announcements', sa.Boolean(), nullable=False),
|
||||||
sa.PrimaryKeyConstraint('course_id'),
|
sa.PrimaryKeyConstraint('course_id'),
|
||||||
sa.UniqueConstraint('code'),
|
sa.UniqueConstraint('code'),
|
||||||
sa.UniqueConstraint('name')
|
sa.UniqueConstraint('name')
|
||||||
|
@ -30,6 +31,7 @@ def upgrade() -> None:
|
||||||
op.create_table('ufora_announcements',
|
op.create_table('ufora_announcements',
|
||||||
sa.Column('announcement_id', sa.Integer(), nullable=False),
|
sa.Column('announcement_id', sa.Integer(), nullable=False),
|
||||||
sa.Column('course_id', sa.Integer(), nullable=True),
|
sa.Column('course_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('publication_date', sa.DateTime(timezone=True), nullable=True),
|
||||||
sa.ForeignKeyConstraint(['course_id'], ['ufora_courses.course_id'], ),
|
sa.ForeignKeyConstraint(['course_id'], ['ufora_courses.course_id'], ),
|
||||||
sa.PrimaryKeyConstraint('announcement_id')
|
sa.PrimaryKeyConstraint('announcement_id')
|
||||||
)
|
)
|
|
@ -1,32 +0,0 @@
|
||||||
"""Add option to disable announcement fetching for courses
|
|
||||||
|
|
||||||
Revision ID: d3cd92cb9efe
|
|
||||||
Revises: 9e8ce58c0a26
|
|
||||||
Create Date: 2022-06-18 00:36:00.484627
|
|
||||||
|
|
||||||
"""
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision = "d3cd92cb9efe"
|
|
||||||
down_revision = "9e8ce58c0a26"
|
|
||||||
branch_labels = None
|
|
||||||
depends_on = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
with op.batch_alter_table("ufora_courses", schema=None) as batch_op:
|
|
||||||
batch_op.add_column(sa.Column("log_announcements", sa.Boolean(), nullable=False, server_default=sa.false()))
|
|
||||||
|
|
||||||
# ### end Alembic commands ###
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
|
||||||
with op.batch_alter_table("ufora_courses", schema=None) as batch_op:
|
|
||||||
batch_op.drop_column("log_announcements")
|
|
||||||
|
|
||||||
# ### end Alembic commands ###
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
@ -10,8 +12,13 @@ async def get_courses_with_announcements(session: AsyncSession) -> list[UforaCou
|
||||||
return (await session.execute(query)).scalars().all()
|
return (await session.execute(query)).scalars().all()
|
||||||
|
|
||||||
|
|
||||||
async def create_new_announcement(session: AsyncSession, announcement_id: int, course: UforaCourse):
|
async def create_new_announcement(
|
||||||
|
session: AsyncSession, announcement_id: int, course: UforaCourse, publication_date: datetime
|
||||||
|
) -> UforaAnnouncement:
|
||||||
"""Add a new announcement to the database"""
|
"""Add a new announcement to the database"""
|
||||||
new_announcement = UforaAnnouncement(announcement_id=announcement_id, course=course)
|
new_announcement = UforaAnnouncement(
|
||||||
|
announcement_id=announcement_id, course=course, publication_date=publication_date
|
||||||
|
)
|
||||||
session.add(new_announcement)
|
session.add(new_announcement)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
return new_announcement
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from sqlalchemy import Column, Integer, Text, ForeignKey, Boolean
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import Column, Integer, Text, ForeignKey, Boolean, DateTime
|
||||||
from sqlalchemy.orm import declarative_base, relationship
|
from sqlalchemy.orm import declarative_base, relationship
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
@ -44,5 +46,6 @@ class UforaAnnouncement(Base):
|
||||||
|
|
||||||
announcement_id = Column(Integer, primary_key=True)
|
announcement_id = Column(Integer, primary_key=True)
|
||||||
course_id = Column(Integer, ForeignKey("ufora_courses.course_id"))
|
course_id = Column(Integer, ForeignKey("ufora_courses.course_id"))
|
||||||
|
publication_date: datetime = Column(DateTime(timezone=True))
|
||||||
|
|
||||||
course: UforaCourse = relationship("UforaCourse", back_populates="announcements", uselist=False, lazy="selectin")
|
course: UforaCourse = relationship("UforaCourse", back_populates="announcements", uselist=False, lazy="selectin")
|
||||||
|
|
|
@ -26,12 +26,14 @@ class UforaNotification:
|
||||||
_view_url: str = field(init=False)
|
_view_url: str = field(init=False)
|
||||||
_title: str = field(init=False)
|
_title: str = field(init=False)
|
||||||
_description: str = field(init=False)
|
_description: str = field(init=False)
|
||||||
|
published_dt: datetime = field(init=False)
|
||||||
_published: str = field(init=False)
|
_published: str = field(init=False)
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
self._view_url = self._create_url()
|
self._view_url = self._create_url()
|
||||||
self._title = self._clean_content(self.content["title"])
|
self._title = self._clean_content(self.content["title"])
|
||||||
self._description = self._get_description()
|
self._description = self._get_description()
|
||||||
|
self.published_dt = self._published_datetime()
|
||||||
self._published = self._get_published()
|
self._published = self._get_published()
|
||||||
|
|
||||||
def to_embed(self) -> discord.Embed:
|
def to_embed(self) -> discord.Embed:
|
||||||
|
@ -69,7 +71,8 @@ class UforaNotification:
|
||||||
text = text.replace("*", "\\*")
|
text = text.replace("*", "\\*")
|
||||||
return md(text)
|
return md(text)
|
||||||
|
|
||||||
def _get_published(self) -> str:
|
def _published_datetime(self) -> datetime:
|
||||||
|
"""Get a datetime instance of the publication date"""
|
||||||
# Datetime is unable to parse the timezone because it's useless
|
# Datetime is unable to parse the timezone because it's useless
|
||||||
# We will hereby cut it out and pray the timezone will always be UTC+0
|
# We will hereby cut it out and pray the timezone will always be UTC+0
|
||||||
published = self.content["published"].rsplit(" ", 1)[0]
|
published = self.content["published"].rsplit(" ", 1)[0]
|
||||||
|
@ -81,6 +84,10 @@ class UforaNotification:
|
||||||
if offset is not None:
|
if offset is not None:
|
||||||
dt += offset
|
dt += offset
|
||||||
|
|
||||||
|
return dt
|
||||||
|
|
||||||
|
def _get_published(self) -> str:
|
||||||
|
"""Get a formatted string that represents when this announcement was published"""
|
||||||
# TODO
|
# TODO
|
||||||
return "Placeholder :) TODO make the functions to format this"
|
return "Placeholder :) TODO make the functions to format this"
|
||||||
|
|
||||||
|
@ -137,9 +144,10 @@ async def fetch_ufora_announcements(session: AsyncSession) -> list[UforaNotifica
|
||||||
|
|
||||||
# Create a new notification
|
# Create a new notification
|
||||||
notification_id, course_id = parsed
|
notification_id, course_id = parsed
|
||||||
notifications.append(UforaNotification(item, course, notification_id, course_id))
|
notification = UforaNotification(item, course, notification_id, course_id)
|
||||||
|
notifications.append(notification)
|
||||||
|
|
||||||
# Create new db entry
|
# Create new db entry
|
||||||
await crud.create_new_announcement(session, notification_id, course)
|
await crud.create_new_announcement(session, notification_id, course, notification.published_dt)
|
||||||
|
|
||||||
return notifications
|
return notifications
|
||||||
|
|
Loading…
Reference in New Issue