2
0

0024_auto_20240513_1143.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Generated by Django 5.0.6 on 2024-05-13 11:43
  2. from django.db import migrations
  3. from datetime import datetime
  4. from abid_utils.abid import abid_from_values, DEFAULT_ABID_URI_SALT
  5. def calculate_abid(self):
  6. """
  7. Return a freshly derived ABID (assembled from attrs defined in ABIDModel.abid_*_src).
  8. """
  9. prefix = self.abid_prefix
  10. ts = eval(self.abid_ts_src)
  11. uri = eval(self.abid_uri_src)
  12. subtype = eval(self.abid_subtype_src)
  13. rand = eval(self.abid_rand_src)
  14. if (not prefix) or prefix == 'obj_':
  15. suggested_abid = self.__class__.__name__[:3].lower()
  16. raise Exception(f'{self.__class__.__name__}.abid_prefix must be defined to calculate ABIDs (suggested: {suggested_abid})')
  17. if not ts:
  18. ts = datetime.utcfromtimestamp(0)
  19. print(f'[!] WARNING: Generating ABID with ts=0000000000 placeholder because {self.__class__.__name__}.abid_ts_src={self.abid_ts_src} is unset!', ts.isoformat())
  20. if not uri:
  21. uri = str(self)
  22. print(f'[!] WARNING: Generating ABID with uri=str(self) placeholder because {self.__class__.__name__}.abid_uri_src={self.abid_uri_src} is unset!', uri)
  23. if not subtype:
  24. subtype = self.__class__.__name__
  25. print(f'[!] WARNING: Generating ABID with subtype={subtype} placeholder because {self.__class__.__name__}.abid_subtype_src={self.abid_subtype_src} is unset!', subtype)
  26. if not rand:
  27. rand = getattr(self, 'uuid', None) or getattr(self, 'id', None) or getattr(self, 'pk')
  28. print(f'[!] WARNING: Generating ABID with rand=self.id placeholder because {self.__class__.__name__}.abid_rand_src={self.abid_rand_src} is unset!', rand)
  29. abid = abid_from_values(
  30. prefix=prefix,
  31. ts=ts,
  32. uri=uri,
  33. subtype=subtype,
  34. rand=rand,
  35. salt=DEFAULT_ABID_URI_SALT,
  36. )
  37. assert abid.ulid and abid.uuid and abid.typeid, f'Failed to calculate {prefix}_ABID for {self.__class__.__name__}'
  38. return abid
  39. def copy_snapshot_uuids(apps, schema_editor):
  40. print(' Copying snapshot.id -> snapshot.uuid...')
  41. Snapshot = apps.get_model("core", "Snapshot")
  42. for snapshot in Snapshot.objects.all():
  43. snapshot.uuid = snapshot.id
  44. snapshot.save(update_fields=["uuid"])
  45. def generate_snapshot_abids(apps, schema_editor):
  46. print(' Generating snapshot.abid values...')
  47. Snapshot = apps.get_model("core", "Snapshot")
  48. for snapshot in Snapshot.objects.all():
  49. snapshot.abid_prefix = 'snp_'
  50. snapshot.abid_ts_src = 'self.added'
  51. snapshot.abid_uri_src = 'self.url'
  52. snapshot.abid_subtype_src = '"01"'
  53. snapshot.abid_rand_src = 'self.uuid'
  54. snapshot.abid = calculate_abid(snapshot)
  55. snapshot.uuid = snapshot.abid.uuid
  56. snapshot.save(update_fields=["abid", "uuid"])
  57. def generate_archiveresult_abids(apps, schema_editor):
  58. print(' Generating ArchiveResult.abid values... (may take an hour or longer for large collections...)')
  59. ArchiveResult = apps.get_model("core", "ArchiveResult")
  60. Snapshot = apps.get_model("core", "Snapshot")
  61. for result in ArchiveResult.objects.all():
  62. result.abid_prefix = 'res_'
  63. result.snapshot = Snapshot.objects.get(pk=result.snapshot_id)
  64. result.snapshot_added = result.snapshot.added
  65. result.snapshot_url = result.snapshot.url
  66. result.abid_ts_src = 'self.snapshot_added'
  67. result.abid_uri_src = 'self.snapshot_url'
  68. result.abid_subtype_src = 'self.extractor'
  69. result.abid_rand_src = 'self.id'
  70. result.abid = calculate_abid(result)
  71. result.uuid = result.abid.uuid
  72. result.save(update_fields=["abid", "uuid"])
  73. class Migration(migrations.Migration):
  74. dependencies = [
  75. ('core', '0023_alter_archiveresult_options_archiveresult_abid_and_more'),
  76. ]
  77. operations = [
  78. migrations.RunPython(copy_snapshot_uuids, reverse_code=migrations.RunPython.noop),
  79. migrations.RunPython(generate_snapshot_abids, reverse_code=migrations.RunPython.noop),
  80. migrations.RunPython(generate_archiveresult_abids, reverse_code=migrations.RunPython.noop),
  81. ]