Refactor archive_feed command to confirm deletion of entries before proceeding
All checks were successful
Deploy to Server / deploy (push) Successful in 12s

This commit is contained in:
Joakim Hellsén 2026-03-27 04:29:15 +01:00
commit f9cac0974d
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk

View file

@ -56,11 +56,15 @@ class Command(BaseCommand):
if count == 0: if count == 0:
msg = f"No entries found for feed: {url}" msg = f"No entries found for feed: {url}"
self.stdout.write(self.style.WARNING(msg)) self.stdout.write(self.style.WARNING(msg))
else: else:
proceed: bool = True
if not force: if not force:
return self.confirm_and_list_entries(url, entries_qs, count) proceed: bool = self.confirm_and_list_entries(
url=url,
entries_qs=entries_qs,
count=count,
)
if proceed:
entries_qs.delete() entries_qs.delete()
msg = f"Deleted {count} entries for feed: {url}" msg = f"Deleted {count} entries for feed: {url}"
self.stdout.write(self.style.SUCCESS(msg)) self.stdout.write(self.style.SUCCESS(msg))
@ -73,15 +77,23 @@ class Command(BaseCommand):
else: else:
msg: str = "\tFeed is up to date, but no new entries were archived." msg: str = "\tFeed is up to date, but no new entries were archived."
self.stdout.write(self.style.WARNING(msg)) self.stdout.write(self.style.WARNING(msg))
return None
def confirm_and_list_entries( def confirm_and_list_entries(
self, self,
url: str, url: str,
entries_qs: QuerySet[Entry, Entry], entries_qs: QuerySet[Entry, Entry],
count: int, count: int,
) -> None: ) -> bool:
"""Confirm with the user before deleting entries and list some of them.""" """Confirm with the user before deleting entries and list some of them.
Args:
url (str): The URL of the feed.
entries_qs (QuerySet[Entry, Entry]): The queryset of entries to be deleted.
count (int): The total number of entries to be deleted.
Returns:
True if user confirms, False otherwise.
"""
msg: str = f"The following {count} entries will be removed for feed: {url}" msg: str = f"The following {count} entries will be removed for feed: {url}"
self.stdout.write(self.style.WARNING(msg)) self.stdout.write(self.style.WARNING(msg))
@ -94,7 +106,8 @@ class Command(BaseCommand):
confirm: str = input("Are you sure you want to proceed? (yes/no): ") confirm: str = input("Are you sure you want to proceed? (yes/no): ")
if confirm.lower() != "yes": if confirm.lower() != "yes":
self.stdout.write(self.style.ERROR("Operation cancelled.")) self.stdout.write(self.style.ERROR("Operation cancelled."))
return return False
return True
def get_entry_title(entry: Entry) -> str | None: def get_entry_title(entry: Entry) -> str | None: