code simplification

moved format code into properties on the dataclasses.

Added pagesize as a requirement to the formatdata func.
This commit is contained in:
Corban-Lee Jones 2024-02-19 09:06:21 +00:00
parent 60525599f2
commit de1f16549c
3 changed files with 41 additions and 14 deletions

View File

@ -227,17 +227,13 @@ class FeedCog(commands.Cog):
await inter.response.defer()
page = 1
pagesize = 10
async def formatdata(index: int, item: dict) -> tuple[str, str]:
item = SubscriptionChannel.from_dict(item)
next_emoji = self.bot.get_emoji(1204542366602502265)
key = f"{index}. {item.subscription.name} {next_emoji} <#{item.id}>"
value = f"[RSS]({item.subscription.rss_url}) · [API]({API.CHANNEL_ENDPOINT}{item.uuid}/)"
return key, value
key = f"{index}. {item.subscription.name} {next_emoji} {item.mention}"
return key, item.hyperlinks_string
async def getdata(page: int) -> dict:
async def getdata(page: int, pagesize: int) -> dict:
async with aiohttp.ClientSession() as session:
api = API(self.bot.api_token, session)
return await api.get_subscription_channels(
@ -245,7 +241,15 @@ class FeedCog(commands.Cog):
)
embed = Followup(f"Links in {inter.guild.name}").info()._embed
pagination = PaginationView(self.bot, inter, embed, getdata, formatdata, pagesize, page)
pagination = PaginationView(
self.bot,
inter=inter,
embed=embed,
getdata=getdata,
formatdata=formatdata,
pagesize=10,
initpage=1
)
await pagination.send()
@subscription_group.command(name="add")
@ -324,16 +328,13 @@ class FeedCog(commands.Cog):
await inter.response.defer()
page = 1
pagesize = 10
def formatdata(index, item):
item = Subscription.from_dict(item)
key = f"{index}. {item.name}"
value = f"[RSS]({item.rss_url}) · [API]({API.SUBSCRIPTION_ENDPOINT}{item.uuid}/)"
return key, value
async def getdata(page):
async def getdata(page: int, pagesize: int):
async with aiohttp.ClientSession() as session:
api = API(self.bot.api_token, session)
return await api.get_subscriptions(
@ -341,7 +342,15 @@ class FeedCog(commands.Cog):
)
embed = Followup(f"Subscriptions in {inter.guild.name}").info()._embed
pagination = PaginationView(self.bot, inter, embed, getdata, formatdata, pagesize, page)
pagination = PaginationView(
self.bot,
inter=inter,
embed=embed,
getdata=getdata,
formatdata=formatdata,
pagesize=10,
initpage=1
)
await pagination.send()

View File

@ -285,6 +285,24 @@ class SubscriptionChannel(DjangoDataModel):
item["creation_datetime"] = datetime.strptime(item["creation_datetime"], DATETIME_FORMAT)
return item
@property
def mention(self) -> str:
"""
Returns the `id` as a string in the discord mention format.
"""
return f"<#{self.id}>"
@property
def hyperlinks_string(self) -> str:
""""""
api_hyperlink = f"[API]({API.CHANNEL_ENDPOINT}{self.uuid}/)"
rss_hyperlink = f"[RSS]({self.subscription.rss_url})"
value = f"{rss_hyperlink} · {api_hyperlink}"
return value
@dataclass
class TrackedContent(DjangoDataModel):

View File

@ -194,7 +194,7 @@ class PaginationView(View):
embed = self.embed.copy()
try:
data, total_results = await self.getdata(self.index)
data, total_results = await self.getdata(self.index, self.pagesize)
except aiohttp.ClientResponseError as exc:
log.error(exc)
await (