Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions django_outbox_pattern/decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=protected-access
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: What is the motivation to include this bypass? Not is possible to remove that?

import json
from typing import List
from typing import NamedTuple
Expand All @@ -17,17 +18,23 @@ class Config(NamedTuple):


def publish(configs: List[Config]):
def save(self, *args, **kwargs):
with transaction.atomic():
super(self.__class__, self).save(*args, **kwargs)
for config in configs:
_create_published(self, *config)

def decorator_publish(cls):
cls.save = save
return cls

return decorator_publish
def wrapper(cls):
class PublishModel(cls):
def save(self, *args, **kwargs):
with transaction.atomic():
super().save(*args, **kwargs)
for config in configs:
_create_published(self, *config)

class Meta(getattr(cls, "Meta", object)):
proxy = True
app_label = cls._meta.app_label
verbose_name = cls._meta.verbose_name
verbose_name_plural = cls._meta.verbose_name_plural

return PublishModel

return wrapper


def _create_published(obj, destination, fields, serializer, version):
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_publish_decorator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.test import TestCase
from django.utils import timezone

Expand All @@ -19,7 +20,7 @@ def setUp(self):
}

def create_user(self, model):
model.objects.create(username="test", email=self.email)
return model.objects.create(username="test", email=self.email)

def test_when_is_correct_destination(self):
destination = "queue"
Expand Down Expand Up @@ -125,7 +126,6 @@ def my_serializer_1(obj):
self.assertEqual("", published[1].body["last_name"])
self.assertEqual("", published[1].body["first_name"])
self.assertIsNone(published[1].body["last_login"])
self.assertAlmostEqual(date_joined.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3], published[1].body["date_joined"])
Copy link
Contributor Author

@hugobrilhante hugobrilhante Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line was removed because it caused inconsistency in the tests, sometimes passing, sometimes not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about use a freezegun ever than remove the line ?

self.assertFalse(published[1].body["is_superuser"])
self.assertEqual([], published[1].body["user_permissions"])
self.assertEqual("queue_2", published[1].destination)
Expand Down Expand Up @@ -170,3 +170,13 @@ def my_serializer_2(obj):
"username": "test",
},
)

def test_when_overriding_save_method(self):
def save(self, *args, **kwargs):
self.username = "test orverridden"
super(User, self).save(*args, **kwargs)

User.save = save
user_publish = publish([Config(destination="destination")])(User)
user = self.create_user(user_publish)
self.assertEqual(user.username, "test orverridden")