Add migration for raw field

This commit is contained in:
Christian Cueni 2021-09-22 10:00:09 +02:00
parent 4be9858730
commit a34db38a5e
3 changed files with 43 additions and 4 deletions

View File

@ -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()

View File

@ -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

View File

@ -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()