English 中文(简体)
django-waffle在Waffle_status endpoint上没有显示适当的国旗地位。
原标题:django-waffle does not show proper flag status on waffle_status endpoint
  • 时间:2023-12-21 16:27:16
  •  标签:
  • django

我建立了这一旗帜:

“waffle>

I m 试图利用waffle_status终端点获得国旗地位,但不管用户是否持平,我还是得到同样的答复:

{
    "flags": {
        "test_flag": {
            "is_active": false,
            "last_modified": "2023-12-21T16:03:38.922Z"
        }
    },
    "switches": {},
    "samples": {}
}

难道我必须把这一旗帜贴上使用者们的旗帜,而后者是一只id子?

我利用4,django-waffle 4.1.0做了这项工作,我已就环境问题举行过这次集会。 y

.
.
.
INSTALLED_APPS = [
     django.contrib.admin ,
     rest_framework ,
     rest_framework.authtoken ,
     django.contrib.auth ,
     waffle ,
     django.contrib.contenttypes ,
     django.contrib.sessions ,
     django.contrib.messages ,
     django.contrib.staticfiles ,
     exammple 
]
.
.
.

MIDDLEWARE = [
     django.middleware.security.SecurityMiddleware ,
     django.contrib.sessions.middleware.SessionMiddleware ,
     django.middleware.common.CommonMiddleware ,
     django.middleware.csrf.CsrfViewMiddleware ,
     django.contrib.auth.middleware.AuthenticationMiddleware ,
     waffle.middleware.WaffleMiddleware ,
     django.contrib.messages.middleware.MessageMiddleware ,
     django.middleware.clickjacking.XFrameOptionsMiddleware ,
]
.
.
.
REST_FRAMEWORK = {
     DEFAULT_PERMISSION_CLASSES : [
         rest_framework.permissions.IsAuthenticated ,
    ]
}
.
.
.

以及项目设计。 file

urlpatterns = [
    path( admin/ , admin.site.urls),
    path( api/v1/token/ , TokenObtainPairView.as_view(), name= token_obtain_pair ),
    path( api/v1/token/refresh/ , TokenRefreshView.as_view(), name= token_refresh ),
    path(
         api/v1/ , include(
            [
                path(  , include(example_urls))
            ]
        )
    ),
    path( waffle/ , include( waffle.urls )),
]

这是否是一种 b/mis塞的特征? 或者说,在某个地方,我没有听说过什么?

P.S. 这样做的目的是,我必须向前线飞行员提供这一旗帜,以展示或隐藏某些视国旗身份而定的直观部件。

我曾尝试将集延戈行政及环境。 False

www.un.org/Depts/DGACM/index_spanish.htm 我确实 clo了“django-waffle”项目的背书,我写道:

 def test_waffle_json_user_flag(self):
        user = get_user_model().objects.create(username= someone_else )
        flag = waffle.get_waffle_flag_model().objects.create(name= myflag )
        flag.users.add(user)
        self.client.force_login(user=user)
        response = self.client.get(reverse( waffle_status ))
        content = response.json()
        self.assertTrue(content[ flags ][ myflag ][ is_active ])

        another_user = get_user_model().objects.create(username= another_one )
        self.client.force_login(user=another_user)
        another_response = self.client.get(reverse( waffle_status ))
        another_content = another_response.json()
        self.assertFalse(another_content[ flags ][ myflag ][ is_active ])

它成功通过!

然后,我拥有该项目上的django服务器,我确实树立了旗帜,对用户具有约束力。 我确实使用邮政员进行测试,我的问题与上文所述相同。

在<代码>waffle_status上作一些辩论 关于Waffle-django一揽子计划的意见,我认为,所提出的申请没有用户,我指的是,如果我把申请打入<代码>request.user。 i see AnonymousUser so, what Im Disappearance here?

问题回答

www.un.org/Depts/DGACM/index_spanish.htm 简短回答:你再次缺失 DEFAULT_AUTHENTICATION_CLASSES,我不知道你如何使用象征性的处理方法,但我要说,有人正在使用会议认证,这样就能够做到:

REST_FRAMEWORK = {
    ...
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.SessionAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    ...

}

www.un.org/Depts/DGACM/index_french.htm

You re almost there! Generally speaking there are two aspects to take into account when handling users:

  • Authentication: verify if the user is who the user claims to be, so you can log the user in safely and give him a token/session
  • Authorization: specifying access/rights that the user has

此时,你只重新核对授权部分的编号为DEFAULT_PERmission_CLASSES,但你没有重新界定任何授权类别,因此DRF赢得了用户的笔记(因此用户将永远是。 在某些情况下,例如,公共终点一般不需要用户数据,因此,验证其开会/编码格不入只是间接费用。

我刚刚碰到这一点。 这是因为DRF公司在中层设备中进行认证。 http://www.django-rest-framework.org/api-guide/authentication/#:%7E:text=Authentication%20always%20runs%20at%20the,contrib.auth%20包装%27s%20User%20class” rel=“nofollow noreferer” docs :

Authentication always runs at the very start of the view, before the permission and throttling checks occur, and before any other code is allowed to proceed.

为了解决这一问题,您需要填写waffle_json。 一种看法是(或如果你更喜欢的话,你可以做中值工作),即对用户进行选择性认证,然后根据修改后的要求,将<代码>waffle_json查询。

Example:

from django.core.exceptions import PermissionDenied
from rest_framework.views import APIView
from rest_framework.exceptions import AuthenticationFailed
from knox.auth import TokenAuthentication
from waffle.views import waffle_json



class WaffleFeatureFlagsView(APIView):
    authentication_classes = []
    permission_classes = []

    def get(self, request, *args, **kwargs):
        """Returns the list of feature flags and their values."""

        token = request.META.get("HTTP_AUTHORIZATION")

        if token:
            token = token.split("Token ")[1].encode("utf-8")
            auth = TokenAuthentication()

            try:
                # I am using knox for token auth, but you can 
                # replace this part with the DRF token auth instead
                user, token = auth.authenticate_credentials(token)
            except AuthenticationFailed:
                raise PermissionDenied

            request.user = user

        return waffle_json(request)

希望帮助!





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签