From dbd5f92fbdf701075e1742bf4189be0649ffadcf Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 09:37:25 +0200 Subject: [PATCH 1/7] Save correct license for user --- .../commands/reset_license_duration.py | 17 +++++++++++++++++ server/users/managers.py | 7 ++++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 server/users/management/commands/reset_license_duration.py diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py new file mode 100644 index 00000000..61f21402 --- /dev/null +++ b/server/users/management/commands/reset_license_duration.py @@ -0,0 +1,17 @@ +from django.utils.timezone import now +from django.core.management.base import BaseCommand + +from users.models import License + + +class Command(BaseCommand): + help = 'Reset licenses that have a duration that is longer than the standard duration' + + def add_arguments(self, parser): + parser.add_argument('--dry-run', action='store_true', dest='save', default=False, help='Make dry-run') + + def handle(self, *args, **options): + today = now().date() + + for license in License.objects.filter(expire_date__gte=today): + print(license) diff --git a/server/users/managers.py b/server/users/managers.py index 2a545850..b0a6578b 100644 --- a/server/users/managers.py +++ b/server/users/managers.py @@ -147,14 +147,15 @@ class LicenseManager(models.Manager): else: user_role = Role.objects.get_default_student_role() - new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id) + new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id, isbn) new_license.licensee.license_expiry_date = new_license.expire_date new_license.licensee.save() return new_license - def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id): - return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id) + def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id, isbn): + return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id, + isbn=isbn) def get_active_license_for_user(self, user): licenses = self.filter(licensee=user, expire_date__gte=timezone.now()).order_by('-expire_date') From adba163c77b0f9c799d3c9d1e8597d2563cd5dc5 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 10:53:25 +0200 Subject: [PATCH 2/7] WIP: Add command for license duration reset --- server/users/licenses.py | 4 +- server/users/management/commands/__init__.py | 0 .../commands/reset_license_duration.py | 37 ++++++++++++++++++- server/users/models.py | 24 ++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 server/users/management/commands/__init__.py diff --git a/server/users/licenses.py b/server/users/licenses.py index 57447d7c..4ec25f5c 100644 --- a/server/users/licenses.py +++ b/server/users/licenses.py @@ -9,7 +9,7 @@ MYSKILLBOX_LICENSES = { }, "978-3-0355-1860-3": { 'edition': STUDENT_KEY, - 'duration': 455, + 'duration': 365, 'name': 'Student 1 year' }, "978-3-0355-1862-7": { @@ -24,7 +24,7 @@ MYSKILLBOX_LICENSES = { }, "978-3-0355-1823-8": { 'edition': TEACHER_KEY, - 'duration': 455, + 'duration': 365, 'name': 'Teacher 1 year' } } diff --git a/server/users/management/commands/__init__.py b/server/users/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index 61f21402..9b66f3d5 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -1,11 +1,21 @@ +import json +from datetime import datetime, date + from django.utils.timezone import now from django.core.management.base import BaseCommand +from users.licenses import MYSKILLBOX_LICENSES from users.models import License +YEARLY_ISBNS = ["978-3-0355-1860-3", "978-3-0355-1823-8"] +LONGER_DURATION = 455 + class Command(BaseCommand): - help = 'Reset licenses that have a duration that is longer than the standard duration' + help = """ + Reset licenses that have a duration of one year + Uses the duration of MYSKILLBOX_LICENSES as reference. + """ def add_arguments(self, parser): parser.add_argument('--dry-run', action='store_true', dest='save', default=False, help='Make dry-run') @@ -14,4 +24,27 @@ class Command(BaseCommand): today = now().date() for license in License.objects.filter(expire_date__gte=today): - print(license) + isbn = license.get_isbn() + if isbn not in YEARLY_ISBNS: + continue + + defined_duration = MYSKILLBOX_LICENSES[isbn]['duration'] # use isbn from hep due to our bug + start_date = license.get_hep_start_date() + duration_in_days = (license.expire_date - start_date).days + if duration_in_days == defined_duration: + continue + + user_join_delta = (license.expire_date - license.licensee.date_joined.date()).days + + if self._is_change_candidate(user_join_delta, duration_in_days, start_date): + print(license.id, duration_in_days, defined_duration, license.expire_date, start_date, user_join_delta) + + def _is_change_candidate(self, user_join_delta: int, duration_in_days: int, start_date: date) -> bool: + """ + user_join_delta == LONGER_DURATION: user joined the same number of days as the old duration was + duration_in_days == LONGER_DURATION: delta from start date and expiry date is the same as the old duration + start_date == NO_DATE: license does not have a start date -> was created after moving to new api as + such it the longer duration was set as duration + """ + return user_join_delta == LONGER_DURATION or duration_in_days == LONGER_DURATION \ + or start_date == License.NO_DATE diff --git a/server/users/models.py b/server/users/models.py index a6877966..794999d5 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -1,3 +1,4 @@ +import json import random import re import string @@ -308,6 +309,8 @@ class License(models.Model): objects = LicenseManager() + NO_DATE = date(2011, 10, 7) # used to tag licenses without start date + def is_teacher_license(self): return self.for_role.key == RoleManager.TEACHER_KEY @@ -315,6 +318,27 @@ class License(models.Model): date = make_aware(datetime(self.expire_date.year, self.expire_date.month, self.expire_date.day)) return License.is_product_active(date, self.isbn) + def _read_as_json(self, raw_string: str) -> dict: + return json.loads(raw_string.replace("'", '"').replace('True', 'true').replace('False', 'false') + .replace('None', 'null')) + + def get_hep_start_date(self) -> date: + hep_data = self._read_as_json(self.raw) + if 'updated_at' in hep_data: # old key from Magento. Format: 2020-06-22 18:45:13 + date_string = hep_data['created_at'] + return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').date() + + return License.NO_DATE + + def get_isbn(self) -> str: + hep_data = self._read_as_json(self.raw) + if 'sku' in hep_data: # Magento key + return hep_data['sku'] + elif 'isbn' in hep_data: # new shop key + return hep_data['isbn'] + else: + return '' + @staticmethod def is_product_active(expiry_date, isbn): now = timezone.now() From fbc441d1eaa4c9fd00e961acefe0b84a1b0cda5c Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 13:22:44 +0200 Subject: [PATCH 3/7] Save new expiry_dates to models --- .../commands/reset_license_duration.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index 9b66f3d5..795f5593 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -1,5 +1,5 @@ import json -from datetime import datetime, date +from datetime import datetime, date, timedelta from django.utils.timezone import now from django.core.management.base import BaseCommand @@ -18,7 +18,7 @@ class Command(BaseCommand): """ def add_arguments(self, parser): - parser.add_argument('--dry-run', action='store_true', dest='save', default=False, help='Make dry-run') + parser.add_argument('--dry-run', action='store_true', dest='dry_run', default=False, help='Make dry-run') def handle(self, *args, **options): today = now().date() @@ -28,16 +28,16 @@ class Command(BaseCommand): if isbn not in YEARLY_ISBNS: continue - defined_duration = MYSKILLBOX_LICENSES[isbn]['duration'] # use isbn from hep due to our bug + standard_duration = MYSKILLBOX_LICENSES[isbn]['duration'] # use isbn from hep due to our bug start_date = license.get_hep_start_date() duration_in_days = (license.expire_date - start_date).days - if duration_in_days == defined_duration: + if duration_in_days == standard_duration: continue user_join_delta = (license.expire_date - license.licensee.date_joined.date()).days if self._is_change_candidate(user_join_delta, duration_in_days, start_date): - print(license.id, duration_in_days, defined_duration, license.expire_date, start_date, user_join_delta) + self._update_expiry_date(license, standard_duration, options['dry_run']) def _is_change_candidate(self, user_join_delta: int, duration_in_days: int, start_date: date) -> bool: """ @@ -48,3 +48,21 @@ class Command(BaseCommand): """ return user_join_delta == LONGER_DURATION or duration_in_days == LONGER_DURATION \ or start_date == License.NO_DATE + + def _update_expiry_date(self, license: License, standard_duration: int, dry_run: bool): + new_expiry_date = license.expire_date - timedelta(LONGER_DURATION - standard_duration) + user_expire_date = license.licensee.license_expiry_date + + if user_expire_date == license.expire_date: + print(f'Resetting user expire_date for userID {license.licensee.id} from' + f' {license.licensee.license_expiry_date} to {new_expiry_date}') + + if not dry_run: + license.licensee.license_expiry_date = new_expiry_date + license.licensee.save() + + print(f'Resetting expiry date for license ID {license.id} ({license.expire_date}) to {new_expiry_date}') + + if not dry_run: + license.expire_date = new_expiry_date + license.save() From e74f224de45f9123521f01d70e0d4f22cef98266 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 13:29:28 +0200 Subject: [PATCH 4/7] 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'] From 28cdc3f4f8e955933a10048d6f84baa2ce5358dd Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Thu, 9 Sep 2021 13:51:05 +0200 Subject: [PATCH 5/7] Add created_field to license --- .../migrations/0028_license_created_at.py | 25 +++++++++++++++++++ server/users/models.py | 6 +++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 server/users/migrations/0028_license_created_at.py diff --git a/server/users/migrations/0028_license_created_at.py b/server/users/migrations/0028_license_created_at.py new file mode 100644 index 00000000..41e2bb22 --- /dev/null +++ b/server/users/migrations/0028_license_created_at.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2.24 on 2021-09-09 11:39 + +from datetime import datetime +from django.db import migrations, models +from django.utils.timezone import make_aware + +from users.models import NO_DATE + +MIN_DATE = datetime.combine(NO_DATE, datetime.min.time()) + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0027_auto_20210414_2116'), + ] + + operations = [ + migrations.AddField( + model_name='license', + name='created_at', + field=models.DateTimeField(auto_now_add=True, default=make_aware(MIN_DATE)), + preserve_default=False, + ), + ] diff --git a/server/users/models.py b/server/users/models.py index 849d232d..8475a501 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -2,7 +2,7 @@ import json import random import re import string -from datetime import date, datetime, timedelta +from datetime import date, datetime, timedelta, MINYEAR from typing import Union from django.contrib.auth import get_user_model @@ -18,6 +18,7 @@ from users.licenses import MYSKILLBOX_LICENSES from users.managers import LicenseManager, RoleManager, UserManager, UserRoleManager DEFAULT_SCHOOL_ID = 1 +NO_DATE = date(MINYEAR, 1, 1) # date to tag licenses without tag class User(AbstractUser): @@ -306,10 +307,11 @@ class License(models.Model): raw = models.TextField(default='') isbn = models.CharField(max_length=50, blank=False, null=False, default=list(MYSKILLBOX_LICENSES.keys())[0]) # student license + created_at = models.DateTimeField(auto_now_add=True) objects = LicenseManager() - NO_DATE = date(2011, 10, 7) # used to tag licenses without start date + NO_DATE = NO_DATE def is_teacher_license(self): return self.for_role.key == RoleManager.TEACHER_KEY From 4be98587303f84beaca73eecd42ee8826e9e1f81 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Wed, 22 Sep 2021 09:27:19 +0200 Subject: [PATCH 6/7] Add created_at fields to license --- server/users/admin.py | 2 +- .../commands/move_new_raw_to_field.py | 0 .../commands/reset_license_duration.py | 4 +-- server/users/managers.py | 9 ++++--- .../migrations/0028_auto_20210922_0550.py | 25 +++++++++++++++++++ ...eated_at.py => 0029_auto_20210922_0550.py} | 12 +++------ server/users/models.py | 10 +++++--- 7 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 server/users/management/commands/move_new_raw_to_field.py create mode 100644 server/users/migrations/0028_auto_20210922_0550.py rename server/users/migrations/{0028_license_created_at.py => 0029_auto_20210922_0550.py} (54%) diff --git a/server/users/admin.py b/server/users/admin.py index 60efb7f1..00fa9251 100644 --- a/server/users/admin.py +++ b/server/users/admin.py @@ -77,7 +77,7 @@ class UserSettingAdmin(admin.ModelAdmin): @admin.register(License) class LicenseAdmin(admin.ModelAdmin): - list_display = ('licensee', 'for_role', 'expire_date', 'isbn') + list_display = ('licensee', 'for_role', 'expire_date', 'isbn', 'hep_created_at') list_filter = ('licensee', 'expire_date') raw_id_fields = ('licensee',) diff --git a/server/users/management/commands/move_new_raw_to_field.py b/server/users/management/commands/move_new_raw_to_field.py new file mode 100644 index 00000000..e69de29b diff --git a/server/users/management/commands/reset_license_duration.py b/server/users/management/commands/reset_license_duration.py index 0716f683..bf5f1587 100644 --- a/server/users/management/commands/reset_license_duration.py +++ b/server/users/management/commands/reset_license_duration.py @@ -5,7 +5,7 @@ from django.utils.timezone import now from django.core.management.base import BaseCommand from users.licenses import MYSKILLBOX_LICENSES -from users.models import License +from users.models import License, NO_DATE YEARLY_ISBNS = ["978-3-0355-1860-3", "978-3-0355-1823-8"] LONGER_DURATION = 455 @@ -47,7 +47,7 @@ class Command(BaseCommand): such it the longer duration was set as duration """ return user_join_delta == LONGER_DURATION or duration_in_days == LONGER_DURATION \ - or start_date == License.NO_DATE + or start_date == NO_DATE def _update_expiry_date(self, license: License, standard_duration: int, dry_run: bool): new_expiry_date = license.expire_date - timedelta(LONGER_DURATION - standard_duration) diff --git a/server/users/managers.py b/server/users/managers.py index b0a6578b..a4308a23 100644 --- a/server/users/managers.py +++ b/server/users/managers.py @@ -147,15 +147,16 @@ class LicenseManager(models.Manager): else: user_role = Role.objects.get_default_student_role() - new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id, isbn) + new_license = self._create_license_for_role(licensee, expiry_date, raw, user_role, order_id, isbn, + activation_date) new_license.licensee.license_expiry_date = new_license.expire_date new_license.licensee.save() return new_license - def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id, isbn): - return self.create(licensee=licensee, expire_date=expiry_date, raw=raw, for_role=role, order_id=order_id, - isbn=isbn) + def _create_license_for_role(self, licensee, expiry_date, raw, role, order_id, isbn, activation_date): + return self.create(licensee=licensee, expire_date=expiry_date, new_api_raw=raw, for_role=role, order_id=order_id, + isbn=isbn, hep_created_at=activation_date) def get_active_license_for_user(self, user): licenses = self.filter(licensee=user, expire_date__gte=timezone.now()).order_by('-expire_date') diff --git a/server/users/migrations/0028_auto_20210922_0550.py b/server/users/migrations/0028_auto_20210922_0550.py new file mode 100644 index 00000000..657c7743 --- /dev/null +++ b/server/users/migrations/0028_auto_20210922_0550.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2.24 on 2021-09-22 05:50 + +import datetime +from django.db import migrations, models +from django.utils.timezone import utc + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0027_auto_20210414_2116'), + ] + + operations = [ + migrations.AddField( + model_name='license', + name='hep_created_at', + field=models.DateTimeField(default=datetime.datetime(1, 1, 1, 0, 0, tzinfo=utc)), + ), + migrations.AddField( + model_name='license', + name='new_api_raw', + field=models.TextField(default=''), + ), + ] diff --git a/server/users/migrations/0028_license_created_at.py b/server/users/migrations/0029_auto_20210922_0550.py similarity index 54% rename from server/users/migrations/0028_license_created_at.py rename to server/users/migrations/0029_auto_20210922_0550.py index 41e2bb22..433e5c75 100644 --- a/server/users/migrations/0028_license_created_at.py +++ b/server/users/migrations/0029_auto_20210922_0550.py @@ -1,25 +1,21 @@ -# Generated by Django 2.2.24 on 2021-09-09 11:39 +# Generated by Django 2.2.24 on 2021-09-22 05:50 -from datetime import datetime from django.db import migrations, models -from django.utils.timezone import make_aware -from users.models import NO_DATE - -MIN_DATE = datetime.combine(NO_DATE, datetime.min.time()) +from users.models import AWARE_NO_DATETIME class Migration(migrations.Migration): dependencies = [ - ('users', '0027_auto_20210414_2116'), + ('users', '0028_auto_20210922_0550'), ] operations = [ migrations.AddField( model_name='license', name='created_at', - field=models.DateTimeField(auto_now_add=True, default=make_aware(MIN_DATE)), + field=models.DateTimeField(auto_now_add=True, default=AWARE_NO_DATETIME), preserve_default=False, ), ] diff --git a/server/users/models.py b/server/users/models.py index 8475a501..dbe4e7cf 100644 --- a/server/users/models.py +++ b/server/users/models.py @@ -18,7 +18,9 @@ from users.licenses import MYSKILLBOX_LICENSES from users.managers import LicenseManager, RoleManager, UserManager, UserRoleManager DEFAULT_SCHOOL_ID = 1 -NO_DATE = date(MINYEAR, 1, 1) # date to tag licenses without tag +NO_DATE = date(MINYEAR, 1, 1) # date to tag licenses without date +NO_DATETIME = datetime.combine(NO_DATE, datetime.min.time()) +AWARE_NO_DATETIME = make_aware(NO_DATETIME) class User(AbstractUser): @@ -308,11 +310,11 @@ class License(models.Model): isbn = models.CharField(max_length=50, blank=False, null=False, default=list(MYSKILLBOX_LICENSES.keys())[0]) # student license created_at = models.DateTimeField(auto_now_add=True) + hep_created_at = models.DateTimeField(default=AWARE_NO_DATETIME) + new_api_raw = models.TextField(default='') objects = LicenseManager() - NO_DATE = NO_DATE - def is_teacher_license(self): return self.for_role.key == RoleManager.TEACHER_KEY @@ -330,7 +332,7 @@ class License(models.Model): date_string = hep_data['created_at'] return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S').date() - return License.NO_DATE + return NO_DATE def get_hep_isbn(self) -> str: hep_data = self._read_as_json(self.raw) From a34db38a5e6dd0063e0bc88d8b2c559526b0b0a8 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Wed, 22 Sep 2021 10:00:09 +0200 Subject: [PATCH 7/7] Add migration for raw field --- .../commands/move_new_raw_to_field.py | 26 +++++++++++++++++++ .../commands/reset_license_duration.py | 3 +-- server/users/models.py | 18 +++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) 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()