-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validator form #760
Merged
Merged
Validator form #760
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,10 +337,20 @@ Change the format of all date in Squest UI. Based on Python [strftime](https://s | |
|
||
### FIELD_VALIDATOR_PATH | ||
|
||
!!!warning | ||
|
||
FIELD_VALIDATOR_PATH is now deprecated. Please use [SURVEY_VALIDATOR_PATH](#survey_validator_path) instead. | ||
|
||
**Default:** `plugins/field_validators` | ||
|
||
Path to form field validation modules. | ||
|
||
### SURVEY_VALIDATOR_PATH | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add deprecation warning on the other field |
||
|
||
**Default:** `plugins/survey_validators` | ||
|
||
Path to SurveyValidator modules. | ||
|
||
## Redis | ||
|
||
### REDIS_CACHE_USER | ||
|
4 changes: 4 additions & 0 deletions
4
docs/manual/advanced/validators.md → docs/manual/advanced/field_validators.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Survey validators | ||
|
||
Survey validators are Python modules that can be added as plugin. It allows users to implement their own validation | ||
logic on a day1 or day2 operation against the full survey. | ||
|
||
## Creating survey validator | ||
|
||
Create a Python file in **SURVEY_VALIDATOR_PATH** (default is `plugins/survey_validators`). | ||
Create Python class that inherit from SurveyValidator with a method `validate_survey`. | ||
|
||
```python | ||
# plugins/survey_validators/MySurveyValidator.py | ||
from service_catalog.forms import SurveyValidator | ||
|
||
class MyCustomValidatorFoo(SurveyValidator): | ||
def validate_survey(self): | ||
# Implement your own logic here | ||
pass | ||
``` | ||
|
||
## SurveyValidator attributes | ||
|
||
### survey | ||
|
||
This is a dict containing survey + request_comment. Keys are variable name. | ||
type: dict | ||
|
||
```bash | ||
>>> print(self.survey) | ||
{ | ||
"request_comment": "commentary sent by user" | ||
"ram_gb": 8, | ||
"vcpu": 4 | ||
} | ||
``` | ||
|
||
### user | ||
|
||
User requesting operation. | ||
type: django.contrib.auth.models.User | ||
|
||
### operation | ||
|
||
Operation requested. | ||
type: service_catalog.models.Operation | ||
|
||
### instance | ||
|
||
Instance targeted. | ||
type: service_catalog.models.Instance | ||
|
||
!!!note | ||
For day 1 operation `self.instance` is a FakeInstance object that contains only **name** and **quota_scope** without `save` method. | ||
The real Instance object is created after validation. | ||
|
||
## SurveyValidator method | ||
|
||
### validate_survey(self) | ||
|
||
Redefine it to implement your own logic. | ||
|
||
### fail(self, message, field="\_\_all\_\_") | ||
|
||
Raise an exception and display message on UI/API. | ||
|
||
## Set validator to a form field | ||
|
||
In Squest, edit an Operation to set validators. Multiples validators can be added, validators are executed in alphabetical order by script name and class name. | ||
|
||
## Example | ||
|
||
This validator will always fail if: | ||
|
||
- ram and cpu are both equal 1 | ||
- It's not the weekend yet | ||
|
||
```python | ||
from service_catalog.forms import SurveyValidator | ||
import datetime | ||
|
||
class ValidatorForVM(SurveyValidator): | ||
def validate_survey(self): | ||
if self.survey.get("ram") == 1 and self.survey.get("vcpu") == 1: | ||
self.fail("Forbidden: you cannot use ram=1 and cpu=1") | ||
|
||
weekday = datetime.datetime.today().weekday() | ||
if weekday < 5: | ||
self.fail("Sorry it's not the weekend yet") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and the line that correspond to this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done