def test_is_like_uuid(self): bad_subjects = ["123", 123, u"fred", 12.9, [1, 2, 3], {"1": "2"}] for subject in bad_subjects: self.assertFalse(helpers.is_like_uuid(subject)) good_subjects = [mocks.UUID1, mocks.UUID2] for subject in good_subjects: self.assertTrue(helpers.is_like_uuid(subject)) # Ensure passing an actual UUID object returns True subject = uuid.uuid4() self.assertTrue(helpers.is_like_uuid(subject))
def get_by_slug_or_key(cls, ctx_or_req, slug_or_key, with_relations=None): """ Returns an object of the supplied type that has a key with the supplied string or a slug matching the supplied string. :param ctx_or_req: Either a `falcon.request.Request` object or a `procession.context.Context` object. :param slug_or_key: A string that may be a slug or lookup key of the object. :param with_relations: Optional list of object classes or class strings representing the child relation objects to include when retrieving the parent record. :raises `procession.exc.NotFound` if no such object found in backend storage. """ if helpers.is_like_uuid(slug_or_key): return cls.get_by_key(ctx_or_req, slug_or_key, with_relations=with_relations) else: ctx = cls._find_ctx(ctx_or_req) filters = { 'slug': slug_or_key } search_spec = search.SearchSpec(ctx, filters=filters, relations=with_relations) data = ctx.store.get_one(cls, search_spec) # TODO(jaypipes): Implement ACLs here. return cls.from_dict(data, is_new=False)
def get_by_slug_or_key(self, obj_type, slug_or_key, search_spec): """ Returns a single object of the supplied type that has a key with the supplied string or a slug matching the supplied string. :param obj_type: A `procession.objects.Object` class. :param slug_or_key: A string that may be a slug or lookup key of the object. :param search_spec: `procession.search.SearchSpec` to use which specifies things like related objects to pull. :raises `procession.exc.NotFound` if no such object found in backend storage. """ if helpers.is_like_uuid(slug_or_key): key_name = obj_type.key_field_name() else: key_name = obj_type.slug_field()[0] values = { key_name: slug_or_key } search_spec.filter_by(**values) return self.get_one(obj_type, search_spec)