Skip to content

Commit c03abc6

Browse files
committed
Switch from wrapt library to decorator so that components which utilize the db_model decorator can be stored in backend cache.
1 parent 1ac657b commit c03abc6

File tree

4 files changed

+38
-39
lines changed

4 files changed

+38
-39
lines changed

django_unicorn/decorators.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from django.conf import settings
55

6-
import wrapt
6+
from decorator import decorator
77

88

9-
@wrapt.decorator()
10-
def db_model(wrapped, instance, args, kwargs):
9+
@decorator
10+
def db_model(func, *args, **kwargs):
1111
"""
1212
Converts a JSON representation of a Django model into an actual model.
1313
@@ -20,50 +20,48 @@ def delete(self, model):
2020
`component.delete({ 'name': 'modelName', pk: 1})` -> `component.delete(modelInstance)`
2121
"""
2222

23+
instance = args[0]
24+
model_dictionary = args[1]
25+
2326
if hasattr(instance, "Meta") and hasattr(instance.Meta, "db_models"):
24-
db_model_name = args[0].get("name")
27+
db_model_name = model_dictionary.get("name")
2528
assert db_model_name, "Missing db model name"
26-
db_model_pk = args[0].get("pk")
29+
db_model_pk = model_dictionary.get("pk")
2730
assert db_model_pk, "Missing db model pk"
2831
db_model_found = False
2932

3033
for db_model in instance.Meta.db_models:
3134
if db_model.name == db_model_name:
3235
model = db_model.model_class.objects.get(pk=db_model_pk)
33-
args = (model,) + args[1:]
36+
args = (instance, model,) + args[2:]
3437
db_model_found = True
3538
break
3639

3740
assert db_model_found, f"No db_model found that matches '{db_model_name}'"
3841
else:
3942
raise AssertionError("No db_models defined")
4043

41-
result = wrapped(*args, **kwargs)
44+
result = func(*args, **kwargs)
4245

4346
return result
4447

4548

46-
def _timed_enabled():
47-
return settings.DEBUG
48-
49-
50-
@wrapt.decorator(enabled=_timed_enabled)
51-
def timed(wrapped, instance, args, kwargs):
49+
@decorator
50+
def timed(func, *args, **kwargs):
5251
"""
5352
Decorator that prints out the timing of a function.
5453
5554
Slightly altered version of https://gist.github.com/bradmontgomery/bd6288f09a24c06746bbe54afe4b8a82.
5655
"""
56+
if not settings.DEBUG:
57+
return func(*args, **kwargs)
58+
5759
logger = logging.getLogger("profile")
5860
start = time.time()
59-
result = wrapped(*args, **kwargs)
61+
result = func(*args, **kwargs)
6062
end = time.time()
6163

62-
function_name = wrapped.__name__
63-
64-
if instance:
65-
function_name = f"{instance}.{function_name}"
66-
64+
function_name = func.__name__
6765
arguments = ""
6866

6967
if args:

poetry.lock

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ django = ">=2.2"
1515
beautifulsoup4 = ">=4.8.0"
1616
orjson = "^3.2.1"
1717
shortuuid = "^1.0.1"
18-
wrapt = "^1.12.1"
1918
cachetools = "^4.1.1"
2019
dataclasses = {version = "^0.8.0", python = "<3.7"}
20+
decorator = "^4.4.2"
2121

2222
[tool.poetry.dev-dependencies]
2323
pytest = "^6.2"

tests/decorators/test_db_model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ def test_happy_path(component):
3838
@pytest.mark.django_db
3939
def test_missing_name(component):
4040
with pytest.raises(AssertionError):
41-
component.get_pk({"pk": 1})
41+
component.get_pk({"pk": 1}, 1)
4242

4343

4444
@pytest.mark.django_db
4545
def test_missing_pk(component):
4646
with pytest.raises(AssertionError):
47-
component.get_pk({"name": "flavor"})
47+
component.get_pk({"name": "flavor"}, 1)
4848

4949

5050
@pytest.mark.django_db
5151
def test_model_not_found(component):
5252
with pytest.raises(Flavor.DoesNotExist):
53-
component.get_pk({"name": "flavor", "pk": -99})
53+
component.get_pk({"name": "flavor", "pk": -99}, 1)
5454

5555

5656
@pytest.mark.django_db
@@ -83,9 +83,9 @@ class Meta:
8383

8484

8585
@pytest.mark.django_db
86-
def test_component_db_model_not_pickleable(component):
86+
def test_component_db_model_is_pickleable(component):
8787
"""
88-
This is not ideal and should hopefully be fixable by providing a `__reduce_ex__` method.
88+
Using the `wrapt` library would raise an exception, but the `decorator` library
89+
is pickleable, so this should be a-ok
8990
"""
90-
with pytest.raises(UnicornCacheError):
91-
get_cacheable_component(component)
91+
get_cacheable_component(component)

0 commit comments

Comments
 (0)