From 5ca510228aebe8b1fb1324a1265792c59d029d5d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org>
Date: Thu, 27 Apr 2023 09:51:24 +0200
Subject: [PATCH] add sync management command

---
 Makefile                                           |  4 ++++
 .../management/commands/import_old_contracts.py    | 14 +++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 7c3c25a..3dd614c 100644
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,7 @@ help:
 	@echo "Application:"
 	@echo "  run            Run the application on port ${PORT}"
 	@echo "  shell          Access the Django shell"
+	@echo "  sync           Sync with the old contract registry"
 	@echo ""
 	@echo "Database:"
 	@echo "  migrations     Generate migrations"
@@ -47,6 +48,9 @@ run: venv
 shell: venv
 	${VENV}/bin/python manage.py shell --settings=${SETTINGS}
 
+sync:
+	${VENV}/bin/python manage.py import_old_contracts https://github.com/pirati-web/smlouvy.pirati.cz.git gh-pages --settings=${SETTINGS} --delete -v 3
+
 migrations: venv
 	${VENV}/bin/python manage.py makemigrations --settings=${SETTINGS}
 
diff --git a/contracts/management/commands/import_old_contracts.py b/contracts/management/commands/import_old_contracts.py
index e2758e1..36a3b9e 100644
--- a/contracts/management/commands/import_old_contracts.py
+++ b/contracts/management/commands/import_old_contracts.py
@@ -2,6 +2,7 @@ import io
 import os
 import re
 import string
+import shutil
 from datetime import date, datetime
 
 import yaml
@@ -57,6 +58,11 @@ class Command(BaseCommand):
             type=str,
             help="Directory to store the cloned repository in",
         )
+        parser.add_argument(
+            "--delete",
+            action="store_true",
+            help="Delete the old temporary storage directory, if it exists.",
+        )
         parser.add_argument(
             "--existing",
             action="store_true",
@@ -1339,7 +1345,7 @@ class Command(BaseCommand):
             (options["directory"] if options["directory"] is not None else "git"),
         )
 
-        if os.path.exists(git_dir):
+        if os.path.exists(git_dir) and not options["delete"]:
             if not options["existing"]:
                 if self.verbosity >= 1:
                     self.stderr.write(
@@ -1356,6 +1362,12 @@ class Command(BaseCommand):
                 if self.verbosity >= 2:
                     self.stdout.write("Using existing git storage directory.")
         else:
+            if options["delete"] and os.path.exists(git_dir):
+                if self.verbosity >= 2:
+                    self.stdout.write("Deleting old git storage directory.")
+
+                shutil.rmtree(git_dir)
+
             if self.verbosity >= 2:
                 self.stdout.write("Cloning repository.")
 
-- 
GitLab