-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chore: Refactor Django orcabus_id
#784
Open
williamputraintan
wants to merge
4
commits into
main
Choose a base branch
from
chore/refactor-orcabus-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+509
−577
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One caveat I found here is the strip/annotation will not work if it uses
iexact
( on the custom filtering we had that based on model). Currently, changing it to filter for only exact match.