Python rest_framework.routers.SimpleRouter() Examples

The following are 5 code examples of rest_framework.routers.SimpleRouter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.routers , or try the search function .
Example #1
Source File: django_app.py    From scout_apm_python with MIT License 6 votes vote down vote up
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 
Example #2
Source File: test_api_introspector.py    From py2swagger with MIT License 5 votes vote down vote up
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() 
Example #3
Source File: views.py    From lego with MIT License 5 votes vote down vote up
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) 
Example #4
Source File: test_viewsets.py    From drf-haystack with MIT License 5 votes vote down vote up
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 
Example #5
Source File: initialize.py    From cmdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
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