Skip to content

How to make a custom condition? #120

@lanshark

Description

@lanshark

Hello All,

Trying to create a custom condition. Here is the code (which I have put in conditions.py in my django project, and imported.

@conditions.register('is_tester')
def is_tester_condition(request=None, **kwargs):
    """
    Django-Flags condition that requires an authcode passed in request, and
    determines if that authcode belongs to a tester, or is in a tester group.

    Returns True if yes, False otherwise.
    """

    logger.debug(f"is_tester_condition: kwargs: {kwargs}")
    authcode = request.GET.get("authCode", "").lower()
    if request is None:
        raise conditions.RequiredForCondition(
            "request is required for condition 'is_tester'"
        )
    if not authcode:
        raise conditions.RequiredForCondition(
            "authCode is required for condition 'is_tester'"
        )
    return True

I have another section of code that calls

for flag in get_flags():
    if flag_enabled (flag, request=request):
         do whatever

when this code runs, I get:

TypeError: is_tester_condition() got multiple values for argument 'request'

If I remove the request=None param from the condition definition, I get that the function takes 0 arguments, but is given 1.

Do you have an examples of custom conditions anywhere? Any ideas why it thinks request is being sent twice?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions