1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
Validators
==========
Django Menu Generator uses validators to allow the displaying of menu items.
A validator is a function that receives the request as arg and returns a boolean indicating if the check has passed
for Django Menu Generator the validators must always be a list containing at least one callable or python path to a callable.
If there is more than one validator, all of them will be evaluated using the AND connector.
Built-in validators
-------------------
Django Menu Generator has the following built-in validators:
- is_superuser:
A validator to check if the authenticated user is a superuser
Usage:
.. code:: python
"validators": ['menu_generator.validators.is_superuser']
- is_staff:
A validator to check if the authenticated user is member of the staff
Usage:
.. code:: python
"validators": ['menu_generator.validators.is_staff']
- is_authenticated:
A validator to check if user is authenticated
Usage:
.. code:: python
"validators": ['menu_generator.validators.is_authenticated']
- is_anonymous:
A validator to check if the user is not authenticated
Usage:
.. code:: python
"validators": ['menu_generator.validators.is_anonymous']
- user_has_permission:
A validator to check if the user has the given permission
Usage:
.. code:: python
"validators": [
('menu_generator.validators.user_has_permission', 'app_label.permission_codename')
]
- More than one validator:
You can pass more than one validator to evaluate using the AND connector
.. code:: python
"validators": [
'menu_generator.validators.is_staff',
('menu_generator.validators.user_has_permission', 'some_app.some_permission')
...
]
Custom validators
-----------------
You can build your own validators and use them with Django Menu Generator
Let's build a validator that checks if the user have more than one pet (dummy example) assuming the user has a
many to many relation called pets
Assuming we build the function inside ``your_project/app1`` on a ``menu_validators.py`` we have:
.. code:: python
# Remember you always must to past the request as first parameter
def has_more_than_one_pet(request):
return request.user.pets.count() > 0
So we can use it as a validator
.. code:: python
"validators": ['your_project.app1.menu_validators.has_more_than_one_pet']
Now let's build a validator that checks if the user's pet belongs to a specific type to illustrate the validators with
parameters.
Assuming we build the function inside the same path and the user have a foreign key called pet
.. code:: python
def has_a_pet_of_type(request, type):
return request.user.pet.type == type
So we use the validator like this:
.. code:: python
"validators": [
('your_project.app1.menu_validators.has_a_pet_of_type', 'DOG')
]
As you can see, we use tuples to pass parameters to the validators, where the first position is the validator and the rest are
the function parameters
|