diff --git a/server/users/management/commands/move_new_raw_to_field.py b/server/users/management/commands/move_new_raw_to_field.py index e69de29b..4b18dc40 100644 --- a/server/users/management/commands/move_new_raw_to_field.py +++ b/server/users/management/commands/move_new_raw_to_field.py @@ -0,0 +1,26 @@ +from django.core.management.base import BaseCommand + +from users.models import License + + +class Command(BaseCommand): + help = """ + Move raw data from new API to own field on License model + """ + + 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.filter(new_api_raw=''): + if license.is_old_api(): + continue + + license.new_api_raw = license.raw + license.raw = '' + + print(f'Moving raw to new API field for licenseID {license.id}') + + if not options['dry_run']: + license.save() diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index bf5f1587..bec75ddc 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -1,5 +1,4 @@ -import json -from datetime import datetime, date, timedelta +from datetime import date, timedelta from django.utils.timezone import now from django.core.management.base import BaseCommand diff --git a/server/users/models.py b/server/users/models.py index dbe4e7cf..50a748ba 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -336,13 +336,27 @@ class License(models.Model): def get_hep_isbn(self) -> str: hep_data = self._read_as_json(self.raw) - if 'sku' in hep_data: # Magento key + if self._is_old_api(hep_data): return hep_data['sku'] - elif 'isbn' in hep_data: # new shop key + elif self._is_new_api(hep_data): return hep_data['isbn'] else: return '' + def is_new_api(self) -> bool: + hep_data = self._read_as_json(self.raw) + return self._is_new_api(hep_data) + + def is_old_api(self) -> bool: + hep_data = self._read_as_json(self.raw) + return self._is_old_api(hep_data) + + def _is_new_api(self, hep_data: dict) -> bool: + return 'isbn' in hep_data + + def _is_old_api(self, hep_data: dict) -> bool: + return 'sku' in hep_data + @staticmethod def is_product_active(expiry_date, isbn): now = timezone.now()