• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python api.canonical_resource_for函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中related_resource.api.urls.api.canonical_resource_for函数的典型用法代码示例。如果您正苦于以下问题:Python canonical_resource_for函数的具体用法?Python canonical_resource_for怎么用?Python canonical_resource_for使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了canonical_resource_for函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_correct_setup

    def test_correct_setup(self):
        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "GET"

        # Verify the explicit 'through' relationships has been created correctly
        resource = api.canonical_resource_for("taggabletag")
        resp = resource.wrap_view("dispatch_detail")(request, pk=self.taggabletag_1.pk)
        data = json.loads(resp.content)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(data["tag"], "/v1/tag/1/")
        self.assertEqual(data["taggable"], "/v1/taggable/1/")

        resource = api.canonical_resource_for("taggable")
        resp = resource.wrap_view("dispatch_detail")(request, pk=self.taggable_1.pk)
        data = json.loads(resp.content)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(data["name"], "exam")

        resource = api.canonical_resource_for("tag")
        resp = resource.wrap_view("dispatch_detail")(request, pk=self.tag_1.pk)
        data = json.loads(resp.content)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(data["name"], "important")

        # and check whether the extradata is present
        self.assertEqual(data["extradata"]["name"], u"additional")
开发者ID:10to8,项目名称:django-tastypie,代码行数:27,代码来源:tests.py


示例2: test_read_tag_content_object_full

 def test_read_tag_content_object_full(self):
     request = MockRequest()
     request.GET = {'format': 'json'}
     request.method = 'GET'
     
     # set the content_object field to full mode
     resource = api.canonical_resource_for('generictag')
     resource.fields['content_object'].full = True
     
     # check for self.tag_1 and self.category_1
     resp = resource.wrap_view('dispatch_detail')(request, pk=self.tag_1.pk)
     self.assertEqual(resp.status_code, 200)
     data = json.loads(resp.content)
     self.assertEqual(data['content_object'], 
         CategoryResource().full_dehydrate(
                 CategoryResource().build_bundle(obj=self.category_1, 
                     request=request)).data)
     
     # now for self.tag_2 and self.taggable_1
     resp = resource.wrap_view('dispatch_detail')(request, pk=self.tag_2.pk)
     self.assertEqual(resp.status_code, 200)
     data = json.loads(resp.content)
     self.assertEqual(data['content_object'], 
         TaggableResource().full_dehydrate(
                 TaggableResource().build_bundle(obj=self.taggable_1, 
                     request=request)).data)
开发者ID:dhatch,项目名称:django-tastypie,代码行数:26,代码来源:tests.py


示例3: test_related_resource_authorization

    def test_related_resource_authorization(self):
        resource = api.canonical_resource_for("notes")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.set_body(
            '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": null}'
        )

        resp = resource.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(User.objects.get(id=self.user.id).username, "testy_mctesterson")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.set_body(
            '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-2", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": {"id": %s, "username": "foobar"}}'
            % self.user.id
        )

        resp = resource.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(User.objects.get(id=self.user.id).username, "foobar")
开发者ID:KidneyKorsen,项目名称:django-tastypie,代码行数:25,代码来源:tests.py


示例4: test_m2m_put_prefetch

    def test_m2m_put_prefetch(self):
        resource = api.canonical_resource_for("forum")
        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        forum = Forum.objects.create()
        user_data_1 = {"username": "valid but unique", "email": "[email protected]", "password": "junk"}
        user_data_2 = {
            "username": "valid and very unique",
            "email": "[email protected]",
            "password": "junk",
        }
        user_data_3 = {"username": "valid again", "email": "[email protected]", "password": "junk"}

        forum_data = {"members": [user_data_1, user_data_2], "moderators": [user_data_3]}
        request.set_body(json.dumps(forum_data))

        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": forum.pk, "resource_name": resource._meta.resource_name, "api_name": resource._meta.api_name},
        )

        response = resource.put_detail(request)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content.decode("utf-8"))

        # Check that the query does what it's supposed to and only the return value is wrong
        self.assertEqual(User.objects.count(), 3)

        self.assertEqual(len(data["members"]), 2)
        self.assertEqual(len(data["moderators"]), 1)
开发者ID:strets123,项目名称:django-tastypie-tweaks,代码行数:31,代码来源:tests.py


示例5: test_related_resource_fk_lookup

    def test_related_resource_fk_lookup(self):
        resource = api.canonical_resource_for('tag')
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.raw_post_data = '{"extradata": {"non_existent_field": "foobar"}, "taggabletags": [ ]}'
        response = resource.post_list(request)

        self.assertEqual(response.status_code, 201)
开发者ID:saebyn,项目名称:django-tastypie,代码行数:9,代码来源:tests.py


示例6: test_cannot_access_user_resource

    def test_cannot_access_user_resource(self):
        resource = api.canonical_resource_for('users')
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.raw_post_data = '{"username": "foobar"}'
        resp = resource.wrap_view('dispatch_detail')(request, pk=self.user.pk)

        self.assertEqual(resp.status_code, 405)
        self.assertEqual(User.objects.get(id=self.user.id).username, self.user.username)
开发者ID:dhatch,项目名称:django-tastypie,代码行数:10,代码来源:tests.py


示例7: test_related_resource_partial_update

 def test_related_resource_partial_update(self):
     note = Note.objects.create(author=self.user, content="Note Content", title="Note Title", slug="note-title")
     resource = api.canonical_resource_for('notes')
     request = MockRequest()
     request.GET = {'format': 'json'}
     request.method = 'PUT'
     request.raw_post_data = '{"content": "The note has been changed"}'
     resp = resource.put_detail(request, pk=note.pk)
     self.assertEqual(resp.status_code, 201)
     self.assertEqual(Note.objects.get(id=note.id).content, "The note has been changed")
开发者ID:clelland,项目名称:django-tastypie,代码行数:10,代码来源:tests.py


示例8: test_related_resource_authorization

    def test_related_resource_authorization(self):
        resource = api.canonical_resource_for('notes')
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.raw_post_data = '{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00", "author": {"id": %s, "username": "foobar"}}' % self.user.id

        resp = resource.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(User.objects.get(id=self.user.id).username, 'foobar')
开发者ID:EnTeQuAk,项目名称:django-tastypie,代码行数:10,代码来源:tests.py


示例9: test_cannot_access_user_resource

    def test_cannot_access_user_resource(self):
        resource = api.canonical_resource_for("users")
        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.body = '{"username": "foobar"}'
        resp = resource.wrap_view("dispatch_detail")(request, pk=self.user.pk)

        self.assertEqual(resp.status_code, 405)
        self.assertEqual(User.objects.get(id=self.user.id).username, self.user.username)
开发者ID:10to8,项目名称:django-tastypie,代码行数:10,代码来源:tests.py


示例10: test_no_save_m2m_related

    def test_no_save_m2m_related(self):
        """
        When saving an object with a M2M field, don't save that related object's related objects.
        """
        cg1 = ContactGroup.objects.create(name='The Inebriati')
        cg2 = ContactGroup.objects.create(name='The Stone Cutters')

        c1 = Contact.objects.create(name='foo')
        c2 = Contact.objects.create(name='bar')
        c2.groups.add(cg1, cg2)
        c3 = Contact.objects.create(name='baz')
        c3.groups.add(cg1)

        self.assertEqual(list(c1.groups.all()), [])
        self.assertEqual(list(c2.groups.all()), [cg1, cg2])
        self.assertEqual(list(c3.groups.all()), [cg1])

        data = {
            'name': c1.name,
            'groups': [reverse('api_dispatch_detail', kwargs={'api_name': 'v1', 'resource_name': 'contactgroup', 'pk': cg1.pk})],
        }

        resource = api.canonical_resource_for('contact')
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request._load_post_and_files = lambda *args, **kwargs: None
        request.set_body(json.dumps(data))

        num_queries = 9 if old_saving_algorithm else 8

        with self.assertNumQueries(num_queries):
            response = resource.wrap_view('dispatch_detail')(request, pk=c1.pk)

        self.assertEqual(response.status_code, 204, response.content)

        new_contacts = Contact.objects.all()
        new_c1 = new_contacts[0]
        new_c2 = new_contacts[1]
        new_c3 = new_contacts[2]

        self.assertEqual(new_c1.name, c1.name)

        self.assertEqual(new_c1.id, c1.id)
        self.assertEqual(list(new_c1.groups.all()), [cg1])
        self.assertEqual(new_c2.id, c2.id)
        self.assertEqual(list(new_c2.groups.all()), [cg1, cg2])
        self.assertEqual(new_c3.id, c3.id)
        self.assertEqual(list(new_c3.groups.all()), [cg1])

        new_cg1 = ContactGroup.objects.get(id=cg1.id)
        new_cg2 = ContactGroup.objects.get(id=cg2.id)

        self.assertEqual(list(new_cg1.members.all()), [new_c1, new_c2, new_c3])
        self.assertEqual(list(new_cg2.members.all()), [new_c2])
开发者ID:SeanHayes,项目名称:django-tastypie,代码行数:55,代码来源:tests.py


示例11: test_no_save_m2m_unchanged

    def test_no_save_m2m_unchanged(self):
        """
        Posting a new detail with a related m2m object shouldn't
        save the m2m object unless the m2m object is provided inline.
        """

        def _save_fails_test(sender, **kwargs):
            self.fail("Should not have saved Label")

        pre_save.connect(_save_fails_test, sender=Label)
        l1 = Label.objects.get(name="coffee")
        resource = api.canonical_resource_for("post")
        label_resource = api.canonical_resource_for("label")

        request = MockRequest()

        body = json.dumps({"name": "test post", "label": [label_resource.get_resource_uri(l1)]})

        request.set_body(body)

        resource.post_list(request)  # _save_fails_test will explode if Label is saved
开发者ID:KidneyKorsen,项目名称:django-tastypie,代码行数:21,代码来源:tests.py


示例12: test_apifielderror_missing_not_null_field

    def test_apifielderror_missing_not_null_field(self):
        """
        Posting a new detail with no related objects
        should require one query to save the object
        """
        resource = api.canonical_resource_for('product')

        request = MockRequest()
        body = json.dumps({})
        request.set_body(body)

        with self.assertRaises(ApiFieldError):
            resource.post_list(request)
开发者ID:beedesk,项目名称:django-tastypie,代码行数:13,代码来源:tests.py


示例13: test_one_query_for_post_list

    def test_one_query_for_post_list(self):
        """
        Posting a new detail with no related objects
        should require one query to save the object
        """
        resource = api.canonical_resource_for("category")

        request = MockRequest()
        body = json.dumps({"name": "Foo", "parent": None})
        request.set_body(body)

        with self.assertNumQueries(1):
            resp = resource.post_list(request)
开发者ID:KidneyKorsen,项目名称:django-tastypie,代码行数:13,代码来源:tests.py


示例14: test_post_by_data_requires_content_type

 def test_post_by_data_requires_content_type(self):
     """Make sure 400 (BadRequest) is the response if an attempt is made to post with data
     for the GenericForeignKey without providing a content_type
     """
     
     request = MockRequest()
     request.GET = {'format': 'json'}
     request.method = 'POST'
     request.raw_post_data = '{"name": "Photoshop", "content_object": %s}' % '{"name": "Design"}'
     
     resource = api.canonical_resource_for('generictag')
     resp = resource.wrap_view('dispatch_list')(request)
     self.assertTrue(resp.status_code, 400)
开发者ID:dhatch,项目名称:django-tastypie,代码行数:13,代码来源:tests.py


示例15: test_put_null

    def test_put_null(self):
        resource = api.canonical_resource_for("category")
        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.body = '{"parent": null, "name": "Son"}'

        # Before the PUT, there should be a parent.
        self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).parent.pk, self.parent_cat_1.pk)

        # After the PUT, the parent should be ``None``.
        resp = resource.put_detail(request, pk=self.child_cat_1.pk)
        self.assertEqual(resp.status_code, 204)
        self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).name, "Son")
        self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).parent, None)
开发者ID:10to8,项目名称:django-tastypie,代码行数:15,代码来源:tests.py


示例16: test_put_null

    def test_put_null(self):
        resource = api.canonical_resource_for('category')
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.raw_post_data = '{"parent": null, "name": "Son"}'

        # Before the PUT, there should be a parent.
        self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).parent.pk, self.parent_cat_1.pk)

        # After the PUT, the parent should be ``None``.
        resp = resource.put_detail(request, pk=self.child_cat_1.pk)
        self.assertEqual(resp.status_code, 204)
        self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).name, 'Son')
        self.assertEqual(Category.objects.get(pk=self.child_cat_1.pk).parent, None)
开发者ID:dhatch,项目名称:django-tastypie,代码行数:15,代码来源:tests.py


示例17: test_depth_limits

    def test_depth_limits(self):
        self.grandchild_cat = Category.objects.create(parent=self.child_cat_1, name='Grandson')
        self.great_grandchild_cat = Category.objects.create(parent=self.grandchild_cat, name='Great-Grandson')
        
        resource = api.canonical_resource_for('depth-category')
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'GET'
        resp = resource.wrap_view('dispatch_detail')(request, pk=self.great_grandchild_cat.pk)

        self.assertEqual(resp.status_code, 200)
        data = json.loads(resp.content)
        self.assertEqual(data['name'], 'Great-Grandson')
        self.assertEqual(data['parent']['name'], 'Grandson')
        self.assertEqual(data['parent']['parent']['name'], 'Son')
        self.assertEqual(data['parent']['parent']['parent'], '/v1/depth-category/1/')
开发者ID:sbnoemi,项目名称:django-tastypie,代码行数:16,代码来源:tests.py


示例18: test_two_queries_for_post_list

    def test_two_queries_for_post_list(self):
        """
        Posting a new detail with one related object, referenced via its
        ``resource_uri`` should require two queries: one to save the
        object, and one to lookup the related object.
        """
        parent = Category.objects.create(name="Bar")
        resource = api.canonical_resource_for("category")

        request = MockRequest()
        body = json.dumps({"name": "Foo", "parent": resource.get_resource_uri(parent)})

        request.set_body(body)

        with self.assertNumQueries(2):
            resp = resource.post_list(request)
开发者ID:KidneyKorsen,项目名称:django-tastypie,代码行数:16,代码来源:tests.py


示例19: test_ok_not_null_field_included

    def test_ok_not_null_field_included(self):
        """
        Posting a new detail with no related objects
        should require one query to save the object
        """
        company = Company.objects.create()

        resource = api.canonical_resource_for('product')

        request = MockRequest()
        body = json.dumps({
            'producer': {'pk': company.pk},
        })
        request.set_body(body)

        resp = resource.post_list(request)

        self.assertEqual(resp.status_code, 201)
开发者ID:beedesk,项目名称:django-tastypie,代码行数:18,代码来源:tests.py


示例20: test_post_by_uri

 def test_post_by_uri(self):
     """Create a new GenericTag item using POST request. 
     Point content_object to a category by it's uri"""
     new_category = Category.objects.create(name="Design")
     self.assertEqual(new_category.name, "Design")
     
     request = MockRequest()
     request.GET = {'format': 'json'}
     request.method = 'POST'
     request.raw_post_data = '{"name": "Photoshop", "content_object": "%s"}' % CategoryResource().get_resource_uri(new_category)
     
     resource = api.canonical_resource_for('generictag')
     
     resp = resource.wrap_view('dispatch_list')(request)
     self.assertEqual(resp.status_code, 201)
     
     # get newly created object via headers.locaion
     self.assertTrue(resp.has_header('location'))
     location = resp['location']
     
     resp = self.client.get(location, data={"format": "json"})
     self.assertEqual(resp.status_code, 200)
     data = json.loads(resp.content)
     self.assertEqual(data['name'], 'Photoshop')
     self.assertEqual(data['content_object'], 
             CategoryResource().get_resource_uri(new_category))
     
     # now try doing this with a TaggableObject instead
     
     new_taggable = Taggable.objects.create(name="Design Post")
     
     request.raw_post_data = '{"name": "UX", "content_object": "%s"}' % TaggableResource().get_resource_uri(new_taggable)
     resp = resource.wrap_view('dispatch_list')(request)
     self.assertEqual(resp.status_code, 201)
     
     self.assertTrue(resp.has_header('location'))
     location = resp['location']
     
     resp = self.client.get(location, data={"format" : "json"})
     self.assertEqual(resp.status_code, 200)
     data = json.loads(resp.content)
     self.assertEqual(data['name'], 'UX')
     self.assertEqual(data['content_object'],
         TaggableResource().get_resource_uri(new_taggable))
开发者ID:dhatch,项目名称:django-tastypie,代码行数:44,代码来源:tests.py



注:本文中的related_resource.api.urls.api.canonical_resource_for函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python models.S_C_Card类代码示例发布时间:2022-05-26
下一篇:
Python resources.PersonResource类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap