Add dddp.models.get_object(model, meteor_id) method, make get_meteor_id(obj) return None where obj is None.

This commit is contained in:
Tyson Clugg 2015-04-23 10:02:13 +10:00
parent 756a65474d
commit befa57b8a3

View file

@ -12,6 +12,8 @@ from dddp import meteor_random_id
@transaction.atomic
def get_meteor_id(obj):
"""Return an Alea ID for the given object."""
if obj is None:
return None
# Django model._meta is now public API -> pylint: disable=W0212
meta = obj._meta
obj_pk = str(obj.pk)
@ -42,6 +44,14 @@ def get_object_id(model, meteor_id):
).values_list('object_id', flat=True).get()
@transaction.atomic
def get_object(model, meteor_id):
"""Return an object for the given meteor_id."""
return model.objects.get(
pk=get_object_id(model, meteor_id),
)
class AleaIdField(models.CharField):
"""CharField that generates its own values using Alea PRNG before INSERT."""