mirror of
https://github.com/ae-utbm/sith.git
synced 2026-11-05 08:49:32 +00:00
refactor update_spam_database
This commit is contained in:
1 parent
7dddb36254
commit
4f989d82bb
1 file changed
+13
-4
@@ -38,14 +38,21 @@ class Command(BaseCommand):
|
|||||||
return domains
|
return domains
|
||||||
|
|
||||||
def _update_domains(self, domains: set[str]):
|
def _update_domains(self, domains: set[str]):
|
||||||
|
existing = set(ToxicDomain.objects.values_list("domain", flat=True))
|
||||||
|
if existing == domains:
|
||||||
|
self.stdout.write("Domain database was up to date")
|
||||||
|
return
|
||||||
|
if to_remove := (existing - domains):
|
||||||
# Cleanup database
|
# Cleanup database
|
||||||
ToxicDomain.objects.filter(is_externally_managed=True).delete()
|
ToxicDomain.objects.filter(
|
||||||
|
domain__in=to_remove, is_externally_managed=True
|
||||||
|
).delete()
|
||||||
|
if to_create := (domains - existing):
|
||||||
# Create database
|
# Create database
|
||||||
ToxicDomain.objects.bulk_create(
|
ToxicDomain.objects.bulk_create(
|
||||||
[
|
[
|
||||||
ToxicDomain(domain=domain, is_externally_managed=True)
|
ToxicDomain(domain=domain, is_externally_managed=True)
|
||||||
for domain in domains
|
for domain in to_create
|
||||||
],
|
],
|
||||||
ignore_conflicts=True,
|
ignore_conflicts=True,
|
||||||
)
|
)
|
||||||
@@ -53,7 +60,9 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
if not self._should_update(force=options["force"]):
|
if not self._should_update(force=options["force"]):
|
||||||
self.stdout.write("Domain database is up to date")
|
self.stdout.write(
|
||||||
|
"No update required. Use --force if you want to do it anyway."
|
||||||
|
)
|
||||||
return
|
return
|
||||||
self.stdout.write("Updating domain database")
|
self.stdout.write("Updating domain database")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user