Skip to content

Commit

Permalink
Merge pull request #52 from romank0/db-column-to-model-fields-transla…
Browse files Browse the repository at this point in the history
…tion-fix

Fixes DB columns to model fields translation in deserialization
  • Loading branch information
PaulGilmartin authored Jun 5, 2023
2 parents 946e2d8 + 8d5af8e commit 1c5af30
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
26 changes: 10 additions & 16 deletions pgpubsub/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,24 @@ def _build_model_serializer_data(cls, payload: Dict, state: str):
fields = {
field.name: field for field in model_cls._meta.fields
}
db_fields = {
field.db_column: field for field in model_cls._meta.fields
db_columns = {
field.column: field for field in model_cls._meta.fields
}

original_state = payload[state]
new_state = {}
model_data = []
if payload[state] is not None:
for field in list(original_state):
for db_field in list(original_state):
# Triggers serialize the notification payload with
# respect to how the model fields look as columns
# in the database. We therefore need to take
# care to map xxx_id named columns to the corresponding
# xxx model field and also to account for model fields
# with alternative database column names as declared
# by the db_column attribute.
value = original_state.pop(field)
if field.endswith('_id'):
field = field.rsplit('_id')[0]
if field in fields:
new_state[field] = value
elif field in db_fields:
field = db_fields[field].name
new_state[field] = value
# in the database. We therefore need to translate
# to model fields and skip outdated fields
value = original_state.pop(db_field)
if db_field in db_columns:
model_field = db_columns[db_field].name
new_state[model_field] = value

pk = model_cls._meta.pk
serialized = {
'fields': new_state,
Expand Down
18 changes: 18 additions & 0 deletions pgpubsub/tests/migrations/0010_media_store_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-05-30 10:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tests', '0009_auto_20230522_0720'),
]

operations = [
migrations.AddField(
model_name='media',
name='store_id',
field=models.TextField(null=True),
),
]
1 change: 1 addition & 0 deletions pgpubsub/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Media(models.Model):
name = models.TextField()
content_type = models.TextField(null=True)
size = models.BigIntegerField(null=True)
store_id = models.TextField(null=True)


class Author(models.Model):
Expand Down
3 changes: 3 additions & 0 deletions pgpubsub/tests/test_trigger_deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def test_deserialize_edit_payload():
name='avatar.jpg',
content_type='image/png',
size=15000,
store_id="some-value",
)
assert 1 == Notification.objects.all().count()
insert_notification = Notification.from_channel(
Expand All @@ -99,6 +100,7 @@ def test_deserialize_edit_payload():
assert media.pk == deserialized['new'].pk
assert media.key == deserialized['new'].key
assert media.size == deserialized['new'].size
assert media.store_id == deserialized['new'].store_id

media.name = 'avatar_2.jpg'
media.save()
Expand All @@ -114,6 +116,7 @@ def test_deserialize_edit_payload():
assert deserialized['new'].pk == media.pk
assert deserialized['new'].key == media.key
assert deserialized['new'].size == media.size
assert media.store_id == deserialized['new'].store_id


@pytest.mark.django_db(transaction=True)
Expand Down

0 comments on commit 1c5af30

Please sign in to comment.