From e74f224de45f9123521f01d70e0d4f22cef98266 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 13:29:28 +0200 Subject: [PATCH] Add command to fix ibans --- .../commands/reset_license_duration.py | 2 +- .../users/management/commands/sync_ibans.py | 26 +++++++++++++++++++ server/users/models.py | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 server/users/management/commands/sync_ibans.py diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index 795f5593..0716f683 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -24,7 +24,7 @@ class Command(BaseCommand): today = now().date() for license in License.objects.filter(expire_date__gte=today): - isbn = license.get_isbn() + isbn = license.get_hep_isbn() if isbn not in YEARLY_ISBNS: continue diff --git a/server/users/management/commands/sync_ibans.py b/server/users/management/commands/sync_ibans.py new file mode 100644 index 00000000..7857eb5f --- /dev/null +++ b/server/users/management/commands/sync_ibans.py @@ -0,0 +1,26 @@ +from django.core.management.base import BaseCommand + +from users.models import License + + +class Command(BaseCommand): + help = """ + Overwrites the License model's IBAN with the IBAN from the hep-JSON + """ + + def add_arguments(self, parser): + parser.add_argument('--dry-run', action='store_true', dest='dry_run', default=False, help='Make dry-run') + + def handle(self, *args, **options): + + for license in License.objects.all(): + isbn = license.get_hep_isbn() + + if isbn == license.isbn: + continue + + print(f'Setting ISBN for licenseID {license.id} from {license.isbn} to {isbn}') + + if not options['dry_run']: + license.isbn = isbn + license.save() diff --git a/server/users/models.py b/server/users/models.py index 794999d5..849d232d 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -330,7 +330,7 @@ class License(models.Model): return License.NO_DATE - def get_isbn(self) -> str: + def get_hep_isbn(self) -> str: hep_data = self._read_as_json(self.raw) if 'sku' in hep_data: # Magento key return hep_data['sku']