示例#1
0
    def test_get_filter_set_for_model_before_init(self):
        # TestClass shouldn't be represented in filter_set yet.
        self.assertTrue(str(TestClass) not in CRUDManager.filter_set.keys())

        # Call get_filter_set_for_model, and verify that this initializes filter_set for this model
        model_filter_set = CRUDManager.get_filter_set_for_model(TestClass)
        self.verify_model_filter_set_structure(model_filter_set)
def print_filters(request):
    """
    Include all filters available for this user and role (role is set in session vars)
    """
    redirect_field_name = "next"
    redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, ""))

    path = resolve(redirect_to)

    role = request.session["crud-role"]

    filters = CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)["filter"][role]
    valid_filters = [filter_name for filter_name, filter_value in filters.items() if filter_value is not None]

    return_string = ""
    for filter_str in valid_filters:
        if filter_str == "__default":
            display_str = "(No Filter)"
        else:
            display_str = filter_str.title()
        return_string += (
            '<input style="width: 20%" type="radio" name="filter"  value="' + filter_str + '">' + display_str + "<br>"
        )

    if return_string == "":
        return_string = '(No filters on this view. Click "Continue.")'
    else:
        return_string += "<br> "
    return return_string
示例#3
0
    def test_get_filter_set_for_model_before_init(self):
        # TestClass shouldn't be represented in filter_set yet.
        self.assertTrue(str(TestClass) not in CRUDManager.filter_set.keys())

        # Call get_filter_set_for_model, and verify that this initializes filter_set for this model
        model_filter_set = CRUDManager.get_filter_set_for_model(TestClass)
        self.verify_model_filter_set_structure(model_filter_set)
def print_filters(request):
    """
    Include all filters available for this user and role (role is set in session vars)
    """
    redirect_field_name = "next"
    redirect_to = request.POST.get(redirect_field_name,
                                   request.GET.get(redirect_field_name, ''))

    path = resolve(redirect_to)

    role = request.session['crud-role']

    filters = CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)['filter'][role]
    valid_filters = [filter_name for filter_name, filter_value in filters.items() if filter_value is not None]

    return_string = ''
    for filter_str in valid_filters:
        if filter_str == "__default":
            display_str = "(No Filter)"
        else:
            display_str = filter_str.title()
        return_string += '<input style="width: 20%" type="radio" name="filter"  value="' + filter_str + '">' + display_str + '<br>'

    if return_string == '':
        return_string = '(No filters on this view. Click "Continue.")'
    else:
        return_string += "<br> "
    return mark_safe(return_string)
def print_available_options(request):
    """
    Print the filter set for this model.
    """
    path = resolve(request.path)
    allowed_methods = CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)["allowed_methods"]

    htmlLines = []
    all_roles_for_user = ""
    for role in CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)["filter"].keys():
        # Decide if we can show this info to this user.
        if role == "anonymous":
            can_view_role = True
        elif role == "authenticated":
            can_view_role = request.user.is_authenticated()
        elif role == "admin":
            can_view_role = request.user.is_superuser
        else:
            can_view_role = CRUDManager.auth_function(role, request.user, request)

        if can_view_role:
            all_roles_for_user += str(allowed_methods[role].values()).replace("dict_values", "")

            if role not in ["authenticated", "anonymous"]:
                header_string = ' -H "CRUD-Role: ' + role + '"'
            else:
                header_string = ""
            if role != "anonymous" and TokenAuthentication in path.func.cls.authentication_classes:
                auth_string = ' -H "Authorization: Token ' + request.user.auth_token.key + '"'
            elif role != "anonymous" and BasicAuthentication in path.func.cls.authentication_classes:
                auth_string = ' -H "Authorization: Basic (base64 encoding of "username:password")"'
            else:
                auth_string = ""

            # Make sure this role can perform at least one operation on this view:
            if not all(value is None for value in allowed_methods[role].values()):
                filters = CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)["filter"][role]
                htmlLines.append("<br><b><u> Role " + role + "</b></u>")

                for filter_str in filters.keys():
                    filter_func = filters[filter_str]
                    methods = allowed_methods[role][filter_str]
                    if methods is None:
                        methods = "(none)"

                    if filter_str == "__default":
                        filters_string = ""
                        filter_str = "no filter: "
                    else:
                        filters_string = ' -H "CRUD-Filters: ' + filter_str + '"'
                        filter_str = "filter '{filter_str}': ".format(filter_str=filter_str)
                    htmlLines.append("Operations " + methods + " with " + filter_str)
                    if filter_func is not None and methods != "C":
                        doc_string = filter_func.__doc__
                        if doc_string is None:
                            doc_string = "(No doc string available for this function)"
                        doc_string = doc_string.replace("\n", "")
                        # Remove all duplicate whitespace
                        doc_string = " ".join(doc_string.split())
                        htmlLines.append("     Uses objects from function '<i>" + doc_string + "</i>'")
                    try:
                        full_url = request.META["werkzeug.request"].base_url
                        htmlLines.append(
                            "     Example using CURL and GET: <i>'curl -X GET "
                            + full_url
                            + header_string
                            + filters_string
                            + auth_string
                            + "</i>'"
                        )
                    except KeyError:
                        htmlLines.append("     (Could not create example CURL commands)")

    # This sucks, but if any of the users have 'C' as an allowed method (gross),
    # grab the code from our create function (ummm), and use regular expressions
    # (what?!) to find the required_params and optional_params dictionaries (WHY?!).
    #
    # Note: this doesn't work when there's a method decorator like rate_limit.
    #
    # TODO: Extract the required_params and optional_params design pattern up one
    # level, so that (among other things) we can do away with this.
    if "C" in all_roles_for_user:
        source_string = inspect.getsource(path.func.cls.create)

        required_expression = re.compile("required_params[\s]*=[\s]*(([\[\{]){1}[^\]]*[\]\}]{1})")
        required_iterator = required_expression.finditer(source_string)
        htmlLines.append("<br><b><u>Required 'Create' POST Parameters:</u></b>")
        required_string = "(none)"
        for match in required_iterator:
            required_string = match.groups()[0]
            required_string = " ".join(required_string.split())
        htmlLines.append(required_string)

        optional_expression = re.compile("optional_params[\s]*=[\s]*(([\[\{]){1}[^\]]*[\]\}]{1})")
        optional_iterator = optional_expression.finditer(source_string)
        htmlLines.append("<br><b><u>Optional 'Create' POST Parameters:</u></b>")
        optional_string = "(none)"
        for match in optional_iterator:
            optional_string = match.groups()[0]
            optional_string = " ".join(optional_string.split())
        htmlLines.append(optional_string)

    htmlText = "\n".join(htmlLines)
    return htmlText
def print_available_options(request):
    """
    Print the filter set for this model.
    """
    path = resolve(request.path)
    allowed_methods = CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)['allowed_methods']

    htmlLines = []
    all_roles_for_user = ''
    for role in CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)['filter'].keys():
        # Decide if we can show this info to this user.
        if role == 'anonymous':
            can_view_role = True
        elif role == 'authenticated':
            can_view_role = request.user.is_authenticated()
        elif role == "admin":
            can_view_role = request.user.is_superuser
        else:
            can_view_role = CRUDManager.auth_function(role, request.user, request)

        if can_view_role:
            all_roles_for_user += str(allowed_methods[role].values()).replace("dict_values", "")

            if role not in ['authenticated', 'anonymous']:
                header_string = ' -H "CRUD-Role: ' + role + '"'
            else:
                header_string = ''
            if role != 'anonymous' and TokenAuthentication in path.func.cls.authentication_classes:
                auth_string = ' -H "Authorization: Token ' + request.user.auth_token.key + '"'
            elif role != 'anonymous' and BasicAuthentication in path.func.cls.authentication_classes:
                auth_string = ' -H "Authorization: Basic (base64 encoding of "username:password")"'
            else:
                auth_string = ''

            # Make sure this role can perform at least one operation on this view:
            if not all(value is None for value in allowed_methods[role].values()):
                filters = CRUDManager.get_filter_set_for_model(path.func.cls.crud_model)['filter'][role]
                htmlLines.append("<br><b><u> Role " + role + "</b></u>")

                for filter_str in filters.keys():
                    filter_func = filters[filter_str]
                    methods = allowed_methods[role][filter_str]
                    if methods is None:
                        methods = "(none)"

                    if filter_str == '__default':
                        filters_string = ''
                        filter_str = "no filter: "
                    else:
                        filters_string = ' -H "CRUD-Filters: ' + filter_str + '"'
                        filter_str = "filter '{filter_str}': ".format(filter_str=filter_str)
                    htmlLines.append("Operations " + methods + " with " + filter_str)
                    if filter_func is not None and methods != 'C':
                        doc_string = filter_func.__doc__
                        if doc_string is None:
                            doc_string = "(No doc string available for this function)"
                        doc_string = doc_string.replace("\n", "")
                        # Remove all duplicate whitespace
                        doc_string = ' '.join(doc_string.split())
                        htmlLines.append("     Uses objects from function '<i>" + doc_string + "</i>'")
                    try:
                        full_url = request.META['werkzeug.request'].base_url
                        htmlLines.append("     Example using CURL and GET: <i>\'curl -X GET " + full_url + header_string + filters_string + auth_string + '</i>\'')
                    except KeyError:
                        htmlLines.append("     (Could not create example CURL commands)")

    # This sucks, but if any of the users have 'C' as an allowed method (gross),
    # grab the code from our create function (ummm), and use regular expressions
    # (what?!) to find the required_params and optional_params dictionaries (WHY?!).
    #
    # Note: this doesn't work when there's a method decorator like rate_limit.
    #
    # TODO: Extract the required_params and optional_params design pattern up one
    # level, so that (among other things) we can do away with this.
    if 'C' in all_roles_for_user:
        source_string = inspect.getsource(path.func.cls.create)

        required_expression = re.compile('required_params[\s]*=[\s]*(([\[\{]){1}[^\]]*[\]\}]{1})')
        required_iterator = required_expression.finditer(source_string)
        htmlLines.append("<br><b><u>Required 'Create' POST Parameters:</u></b>")
        required_string = "(none)"
        for match in required_iterator:
            required_string = match.groups()[0]
            required_string = ' '.join(required_string.split())
        htmlLines.append(required_string)

        optional_expression = re.compile('optional_params[\s]*=[\s]*(([\[\{]){1}[^\]]*[\]\}]{1})')
        optional_iterator = optional_expression.finditer(source_string)
        htmlLines.append("<br><b><u>Optional 'Create' POST Parameters:</u></b>")
        optional_string = "(none)"
        for match in optional_iterator:
            optional_string = match.groups()[0]
            optional_string = ' '.join(optional_string.split())
        htmlLines.append(optional_string)

    htmlText = '\n'.join(htmlLines)
    return mark_safe(htmlText)