-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Refactor Django
orcabus_id
(#784)
- Loading branch information
1 parent
955689d
commit 80fd526
Showing
76 changed files
with
510 additions
and
577 deletions.
There are no files selected for viewing
73 changes: 40 additions & 33 deletions
73
lib/workload/stateless/stacks/metadata-manager/app/fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,55 @@ | ||
import hashlib | ||
|
||
import ulid | ||
from django.core.validators import RegexValidator | ||
from django.db import models | ||
|
||
ULID_REGEX_STR = r"[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}" | ||
ulid_validator = RegexValidator(regex=ULID_REGEX_STR, | ||
message='ULID is expected to be 26 characters long', | ||
code='invalid_orcabus_id') | ||
|
||
|
||
def get_ulid() -> str: | ||
return ulid.new().str | ||
|
||
class HashField(models.CharField): | ||
description = ( | ||
"HashField is related to some base fields (other columns) in a model and" | ||
"stores its hashed value for better indexing performance." | ||
) | ||
|
||
def __init__(self, base_fields, *args, **kwargs): | ||
""" | ||
:param base_fields: name of fields storing the value to be hashed | ||
""" | ||
self.base_fields = base_fields | ||
kwargs["max_length"] = 64 | ||
super(HashField, self).__init__(*args, **kwargs) | ||
class UlidField(models.CharField): | ||
description = "An OrcaBus internal ID (ULID)" | ||
|
||
def __init__(self, *args, **kwargs): | ||
kwargs['max_length'] = 26 # ULID length | ||
kwargs['validators'] = [ulid_validator] | ||
kwargs['default'] = get_ulid | ||
super().__init__(*args, **kwargs) | ||
|
||
def deconstruct(self): | ||
name, path, args, kwargs = super().deconstruct() | ||
del kwargs["max_length"] | ||
if self.base_fields is not None: | ||
kwargs["base_fields"] = self.base_fields | ||
del kwargs['validators'] | ||
del kwargs['default'] | ||
return name, path, args, kwargs | ||
|
||
def pre_save(self, instance, add): | ||
self.calculate_hash(instance) | ||
return super(HashField, self).pre_save(instance, add) | ||
|
||
def calculate_hash(self, instance): | ||
sha256 = hashlib.sha256() | ||
for field in self.base_fields: | ||
value = getattr(instance, field) | ||
sha256.update(value.encode("utf-8")) | ||
setattr(instance, self.attname, sha256.hexdigest()) | ||
class OrcaBusIdField(UlidField): | ||
description = "An OrcaBus internal ID (based on ULID)" | ||
|
||
def __init__(self, prefix='', *args, **kwargs): | ||
self.prefix = prefix | ||
super().__init__(*args, **kwargs) | ||
|
||
@property | ||
def non_db_attrs(self): | ||
return super().non_db_attrs + ("prefix",) | ||
|
||
class HashFieldHelper(object): | ||
def __init__(self): | ||
self.__sha256 = hashlib.sha256() | ||
def from_db_value(self, value, expression, connection): | ||
if value and self.prefix != '': | ||
return f"{self.prefix}.{value}" | ||
else: | ||
return value | ||
|
||
def add(self, value): | ||
self.__sha256.update(value.encode("utf-8")) | ||
return self | ||
def to_python(self, value): | ||
# This will be called when the function | ||
return self.get_prep_value(value) | ||
|
||
def calculate_hash(self): | ||
return self.__sha256.hexdigest() | ||
def get_prep_value(self, value): | ||
# We just want the last 26 characters which is the ULID (ignoring any prefix) when dealing with the database | ||
return value[-26:] |
74 changes: 74 additions & 0 deletions
74
...tateless/stacks/metadata-manager/app/migrations/0003_alter_contact_orcabus_id_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Generated by Django 5.1.4 on 2024-12-17 01:44 | ||
|
||
import app.fields | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('app', '0002_remove_historicalcontact_history_user_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='contact', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='historicalcontact', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(db_index=True), | ||
), | ||
migrations.AlterField( | ||
model_name='historicalindividual', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(db_index=True), | ||
), | ||
migrations.AlterField( | ||
model_name='historicallibrary', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(db_index=True), | ||
), | ||
migrations.AlterField( | ||
model_name='historicalproject', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(db_index=True), | ||
), | ||
migrations.AlterField( | ||
model_name='historicalsample', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(db_index=True), | ||
), | ||
migrations.AlterField( | ||
model_name='historicalsubject', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(db_index=True), | ||
), | ||
migrations.AlterField( | ||
model_name='individual', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='library', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='project', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='sample', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
migrations.AlterField( | ||
model_name='subject', | ||
name='orcabus_id', | ||
field=app.fields.OrcaBusIdField(primary_key=True, serialize=False), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/workload/stateless/stacks/metadata-manager/app/models/contact.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
lib/workload/stateless/stacks/metadata-manager/app/serializers/base.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.