def test_get_object_or_404(self): user = self.create_user('test', 'test') # test with model as first arg self.assertRaises(NotFound, get_object_or_404, User, User.username=='not-here') self.assertEqual(user, get_object_or_404(User, User.username=='test')) # test with query as first arg active = User.select().where(User.active==True) inactive = User.select().where(User.active==False) self.assertRaises(NotFound, get_object_or_404, active, User.username=='not-here') self.assertRaises(NotFound, get_object_or_404, inactive, User.username=='test') self.assertEqual(user, get_object_or_404(active, User.username=='test'))
def api_detail(self, pk, method=None): obj = get_object_or_404(self.get_query(), self.pk==pk) method = method or request.method if not getattr(self, 'check_%s' % method.lower())(obj): return self.response_forbidden() if method == 'GET': return self.object_detail(obj) elif method in ('PUT', 'POST'): return self.edit(obj) elif method == 'DELETE': return self.delete(obj)