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