当前位置: 首页>>代码示例>>Python>>正文


Python routers.SimpleRouter方法代码示例

本文整理汇总了Python中rest_framework.routers.SimpleRouter方法的典型用法代码示例。如果您正苦于以下问题:Python routers.SimpleRouter方法的具体用法?Python routers.SimpleRouter怎么用?Python routers.SimpleRouter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rest_framework.routers的用法示例。


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

示例1: drf_router

# 需要导入模块: from rest_framework import routers [as 别名]
# 或者: from rest_framework.routers import SimpleRouter [as 别名]
def drf_router():
    """
    DRF Router as a lazy object because it needs to import User model which
    can't be done until after django.setup()
    """
    from django.contrib.auth.models import User
    from rest_framework import routers
    from rest_framework import serializers
    from rest_framework import viewsets

    class UserSerializer(serializers.Serializer):
        id = serializers.IntegerField(label="ID", read_only=True)
        username = serializers.CharField(max_length=200)

    class UserViewSet(viewsets.ModelViewSet):
        queryset = User.objects.all()
        serializer_class = UserSerializer

    router = routers.SimpleRouter()
    router.register(r"users", UserViewSet)
    return router 
开发者ID:scoutapp,项目名称:scout_apm_python,代码行数:23,代码来源:django_app.py

示例2: setUp

# 需要导入模块: from rest_framework import routers [as 别名]
# 或者: from rest_framework.routers import SimpleRouter [as 别名]
def setUp(self):
        RedefineViewSet.as_view = viewset_as_view_decorator(RedefineViewSet.as_view)
        router = routers.SimpleRouter()
        router.register(r'd-view$', RedefineViewSet, base_name='test_base_name')
        self.url_patterns = router.urls

        url_parser = UrlParser()
        self.apis = url_parser.get_apis(url_patterns=self.url_patterns)
        self.api_introspector = ApiIntrospector(self.apis)
        self.api_introspector.inspect() 
开发者ID:Arello-Mobile,项目名称:py2swagger,代码行数:12,代码来源:test_api_introspector.py

示例3: get_viewset_permissions

# 需要导入模块: from rest_framework import routers [as 别名]
# 或者: from rest_framework.routers import SimpleRouter [as 别名]
def get_viewset_permissions(viewset, model, user, obj, queryset):
    """
    Return a list of actions a user can perform on a viewset. We use the SimpleRouter to extract
    routes from viewsets. Possible actions per viewset is cached in memory. The next thing we do
    is to check permissions on all actions using the AbakusPermission backend.
    """

    router = SimpleRouter()
    viewset_cls = viewset.__class__

    def get_permissions(viewset_cls):
        routes = router.get_routes(viewset_cls)
        actions = []
        for route in routes:
            actions += route.mapping.values()
        return [action_to_permission(action) for action in actions]

    permissions = (
        permission_cache[viewset_cls]
        if viewset_cls in permission_cache
        else permission_cache.setdefault(viewset_cls, get_permissions(viewset_cls))
    )

    handler = permission_handler(viewset, model)

    return handler.permissions_grant(permissions, user, obj, queryset) 
开发者ID:webkom,项目名称:lego,代码行数:28,代码来源:views.py

示例4: setUp

# 需要导入模块: from rest_framework import routers [as 别名]
# 或者: from rest_framework.routers import SimpleRouter [as 别名]
def setUp(self):
        MockPersonIndex().reindex()
        MockPetIndex().reindex()
        self.router = SimpleRouter()

        class FacetSerializer(HaystackFacetSerializer):

            class Meta:
                fields = ["firstname", "lastname", "created"]

        class ViewSet1(FacetMixin, HaystackViewSet):
            index_models = [MockPerson]
            serializer_class = Serializer
            facet_serializer_class = FacetSerializer

        class ViewSet2(MoreLikeThisMixin, HaystackViewSet):
            index_models = [MockPerson]
            serializer_class = Serializer

        class ViewSet3(HaystackViewSet):
            index_models = [MockPerson, MockPet]
            serializer_class = Serializer

        self.view1 = ViewSet1
        self.view2 = ViewSet2
        self.view3 = ViewSet3 
开发者ID:rhblind,项目名称:drf-haystack,代码行数:28,代码来源:test_viewsets.py

示例5: add_table

# 需要导入模块: from rest_framework import routers [as 别名]
# 或者: from rest_framework.routers import SimpleRouter [as 别名]
def add_table(table, create_index=False):
    if create_index:
        mapp = Mapping()
        data_mapping = mapp.generate_data_mapping(table)
        record_mapping = mapp.generate_record_data_mapping(table)
        delete_mapping = mapp.generate_deleted_data_mapping(table)
        add_index(table.name, data_mapping)
        add_index(table.name + ".", record_mapping)
        add_index(table.name + "..", delete_mapping)

    data.initialize.add_serializer(table)
    viewset = data.initialize.add_viewset(table)
    router = SimpleRouter(trailing_slash=False)
    router.register(table.name, viewset, base_name=table.name)
    urls = router.urls
    data.urls.urlpatterns.extend(urls)
    data_url_map[table.name] = urls

    viewset = record_data.initialize.add_viewset(table)
    router = SimpleRouter(trailing_slash=False)
    router.register(table.name, viewset, base_name=table.name)
    urls = router.urls
    record_data.urls.urlpatterns.extend(urls)
    record_data_url_map[table.name] = urls

    viewset = deleted_data.initialize.add_viewset(table)
    router = SimpleRouter(trailing_slash=False)
    router.register(table.name, viewset, base_name=table.name)
    urls = router.urls
    deleted_data.urls.urlpatterns.extend(urls)
    deleted_data_url_map[table.name] = urls

    # 添加权限
    permission, is_new = models.Permission.objects.get_or_create(name=f"{table.name}.read")
    permission.alias = f"{table.name}读权限"
    permission.save()
    permission, is_new = models.Permission.objects.get_or_create(name=f"{table.name}.write")
    permission.alias = f"{table.name}写权限"
    permission.save()


# 删除表和api 
开发者ID:open-cmdb,项目名称:cmdb,代码行数:44,代码来源:initialize.py


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