diff --git a/lidiabrowser/lidia/models.py b/lidiabrowser/lidia/models.py index 8d82619..1b7c1a7 100644 --- a/lidiabrowser/lidia/models.py +++ b/lidiabrowser/lidia/models.py @@ -189,3 +189,15 @@ class Meta: def __str__(self): lidiaterm = self.lidiaterm.term if self.lidiaterm else None return f"{self.articleterm}/{lidiaterm}" + + +def delete_all() -> None: + """Delete all objects in lidia app.""" + + Publication.objects.all().delete() + Language.objects.all().delete() + BaseAnnotation.objects.all().delete() + ArticleTerm.objects.all().delete() + LidiaTerm.objects.all().delete() + Category.objects.all().delete() + TermGroup.objects.all().delete() diff --git a/lidiabrowser/sync/management/commands/populate.py b/lidiabrowser/sync/management/commands/populate.py index 2679e5b..52f9500 100644 --- a/lidiabrowser/sync/management/commands/populate.py +++ b/lidiabrowser/sync/management/commands/populate.py @@ -1,10 +1,20 @@ -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandParser from sync.populate import populate +from lidia.models import delete_all class Command(BaseCommand): help = "Parse raw sync content and populate LIDIA models" + def add_arguments(self, parser: CommandParser) -> None: + parser.add_argument( + "--refresh", + action="store_true", + help="First remove annotations from database to solve any sync problems" + ) + def handle(self, *args, **options): + if options["refresh"]: + delete_all() populate() diff --git a/lidiabrowser/sync/management/commands/sync.py b/lidiabrowser/sync/management/commands/sync.py index 79af966..fb2d1df 100644 --- a/lidiabrowser/sync/management/commands/sync.py +++ b/lidiabrowser/sync/management/commands/sync.py @@ -1,10 +1,20 @@ -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandParser from sync.zoterosync import sync +from sync.models import delete_all class Command(BaseCommand): help = "Synchronize with data on the Zotero server" + def add_arguments(self, parser: CommandParser) -> None: + parser.add_argument( + "--refresh", + action="store_true", + help="First remove sync information from database to solve any sync problems" + ) + def handle(self, *args, **options): + if options["refresh"]: + delete_all() sync() diff --git a/lidiabrowser/sync/models.py b/lidiabrowser/sync/models.py index ded08b0..323dcc2 100644 --- a/lidiabrowser/sync/models.py +++ b/lidiabrowser/sync/models.py @@ -32,3 +32,10 @@ class Sync(models.Model): def __str__(self): return f"Library #{self.library_id}" + + +def delete_all() -> None: + """Delete all objects in sync app.""" + Publication.objects.all().delete() + Annotation.objects.all().delete() + Sync.objects.all().delete()