-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from dabapps/producer-refactor
Introduce the concept of "producers"
- Loading branch information
Showing
9 changed files
with
647 additions
and
525 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django_readers.utils import map_or_apply, none_safe_attrgetter | ||
from operator import attrgetter, methodcaller | ||
|
||
|
||
def attr(name, *, transform_value=None, transform_value_if_none=False): | ||
def producer(instance): | ||
value = none_safe_attrgetter(name)(instance) | ||
if transform_value and (value is not None or transform_value_if_none): | ||
value = transform_value(value) | ||
return value | ||
|
||
return producer | ||
|
||
|
||
method = methodcaller | ||
|
||
|
||
def relationship(name, related_projector): | ||
""" | ||
Given an attribute name and a projector, return a producer which plucks | ||
the attribute off the instance, figures out whether it represents a single | ||
object or an iterable/queryset of objects, and applies the given projector | ||
to the related object or objects. | ||
""" | ||
|
||
def producer(instance): | ||
try: | ||
related = none_safe_attrgetter(name)(instance) | ||
except ObjectDoesNotExist: | ||
return None | ||
return map_or_apply(related, related_projector) | ||
|
||
return producer | ||
|
||
|
||
def pk_list(name): | ||
""" | ||
Given an attribute name (which should be a relationship field), return a | ||
producer which returns a list of the PK of each item in the relationship (or | ||
just a single PK if this is a to-one field, but this is an inefficient way of | ||
doing it). | ||
""" | ||
return relationship(name, attrgetter("pk")) |
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
Oops, something went wrong.