當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。