def find_one(cls, query, select_for_update=False): try: if select_for_update: return cls.objects.filter(to_django_query(query, model_cls=cls)).select_for_update().get() return cls.objects.get(to_django_query(query, model_cls=cls)) except cls.DoesNotExist: raise modularodm.exceptions.NoResultsFound() except cls.MultipleObjectsReturned as e: raise modularodm.exceptions.MultipleResultsFound(*e.args)
def find_one(cls, query): try: return cls.objects.get(to_django_query(query, model_cls=cls)) except cls.DoesNotExist: raise modularodm.exceptions.NoResultsFound() except cls.MultipleObjectsReturned as e: raise modularodm.exceptions.MultipleResultsFound(*e.args)
def test_handles_and_queries(self): q = Q('foo', 'eq', 42) & Q('bar', 'eq', 24) django_q = to_django_query(q) assert type(django_q) is DjangoQ assert django_q.connector == 'AND' assert len(django_q.children) == 2 assert django_q.children == [('foo__exact', 42), ('bar__exact', 24)]
def get_object_or_error(model_cls, query_or_pk, display_name=None): obj = query = None if isinstance(query_or_pk, basestring): # they passed a 5-char guid as a string if issubclass(model_cls, GuidMixin): # if it's a subclass of GuidMixin we know it's primary_identifier_name query = {'guids___id': query_or_pk} else: if hasattr(model_cls, 'primary_identifier_name'): # primary_identifier_name gives us the natural key for the model query = {model_cls.primary_identifier_name: query_or_pk} else: # fall back to modmcompatiblity's load method since we don't know their PIN obj = model_cls.load(query_or_pk) else: # they passed a query if hasattr(model_cls, 'primary_identifier_name'): query = to_django_query(query_or_pk, model_cls=model_cls) else: # fall back to modmcompatibility's find_one obj = model_cls.find_one(query_or_pk) if not obj: if not query: # if we don't have a query or an object throw 404 raise NotFound try: # TODO This could be added onto with eager on the queryset and the embedded fields of the api if isinstance(query, dict): obj = model_cls.objects.get(**query) else: obj = model_cls.objects.get(query) except ObjectDoesNotExist: raise NotFound # For objects that have been disabled (is_active is False), return a 410. # The User model is an exception because we still want to allow # users who are unconfirmed or unregistered, but not users who have been # disabled. if model_cls is User and obj.is_disabled: raise Gone(detail='The requested user is no longer available.', meta={ 'full_name': obj.fullname, 'family_name': obj.family_name, 'given_name': obj.given_name, 'middle_names': obj.middle_names, 'profile_image': obj.profile_image_url() }) elif model_cls is not User and not getattr( obj, 'is_active', True) or getattr(obj, 'is_deleted', False): if display_name is None: raise Gone else: raise Gone( detail='The requested {name} is no longer available.'.format( name=display_name)) return obj
def get_object_or_error(model_cls, query_or_pk, display_name=None): obj = query = None if isinstance(query_or_pk, basestring): # they passed a 5-char guid as a string if issubclass(model_cls, GuidMixin): # if it's a subclass of GuidMixin we know it's primary_identifier_name query = {'guids___id': query_or_pk} else: if hasattr(model_cls, 'primary_identifier_name'): # primary_identifier_name gives us the natural key for the model query = {model_cls.primary_identifier_name: query_or_pk} else: # fall back to modmcompatiblity's load method since we don't know their PIN obj = model_cls.load(query_or_pk) else: # they passed a query if hasattr(model_cls, 'primary_identifier_name'): query = to_django_query(query_or_pk, model_cls=model_cls) else: # fall back to modmcompatibility's find_one obj = model_cls.find_one(query_or_pk) if not obj: if not query: # if we don't have a query or an object throw 404 raise NotFound try: # TODO This could be added onto with eager on the queryset and the embedded fields of the api if isinstance(query, dict): obj = model_cls.objects.get(**query) else: obj = model_cls.objects.get(query) except ObjectDoesNotExist: raise NotFound # For objects that have been disabled (is_active is False), return a 410. # The User model is an exception because we still want to allow # users who are unconfirmed or unregistered, but not users who have been # disabled. if model_cls is User and obj.is_disabled: raise UserGone(user=obj) elif model_cls is not User and not getattr(obj, 'is_active', True) or getattr(obj, 'is_deleted', False): if display_name is None: raise Gone else: raise Gone(detail='The requested {name} is no longer available.'.format(name=display_name)) return obj
def find(cls, query=None): if not query: return cls.objects.all() else: return cls.objects.filter(to_django_query(query, model_cls=cls))
def test_returns_a_django_q(self): q = Q('foo', 'eq', 42) django_q = to_django_query(q) assert type(django_q) is DjangoQ