forked from allegro/ralph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom fields in GUI and API
- Loading branch information
Showing
49 changed files
with
1,481 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Custom fields | ||
|
||
## How to attach custom fields to your model? | ||
|
||
Mix `WithCustomFieldsMixin` class to your model definition (import it from `ralph.lib.custom_fields.models`) | ||
|
||
## Admin integration | ||
|
||
To use custom fields in Django Admin for your model, mix `CustomFieldValueAdminMaxin` class to your model admin (import it from `ralph.lib.custom_fields.admin`) | ||
|
||
## Django Rest Framework integration | ||
|
||
To use custom fields in Django Rest Framework, mix `WithCustomFieldsSerializerMixin` class to your API serializer (import it from `ralph.lib.custom_fields.api`) | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,121 @@ | ||
# Custom fields | ||
|
||
Ralph's custom fields features: | ||
|
||
* it could be attached to any model | ||
* field could have limitation on value type (ex. have to be int, string, url, bool) | ||
* field could have limitation on possible choices (like html's select field) | ||
|
||
## Defining your own custom fields | ||
|
||
To define your own custom fields, go to `http://<YOUR-RALPH-URL>/custom_fields/customfield/` or select it in menu under `Settings > Custom fields`. | ||
|
||
Possible options: | ||
|
||
* `name` - name of your custom field | ||
* `attribute name` - this is slugged name of the custom field. It's used as a key in API. | ||
* `type` - custom field type. Possible choices here are: | ||
* `string` | ||
* `integer` | ||
* `date` | ||
* `url` | ||
* `choice list` | ||
* `choices` - fill it if you've chosen `choices` type. This is list of possible choices for your custom field. Separate choices by `|`, ex. `abc|def|ghi`. | ||
* `default value` - if you fill it, this value will be used as a default for your custom field, | ||
|
||
Example: | ||
|
||
![custom-field-definition](/img/custom-field-add.png "Example of custom field definition") | ||
|
||
|
||
## Attaching custom fields to objects | ||
|
||
You could attach custom fields to any object type (if it was enabled by developers to do it for particular type). | ||
|
||
Ralph's custom fields works pretty much the same as any other fields here. First type (part of) the name of the custom field into the `Key` field. | ||
|
||
|
||
![custom-field-autocomplete](/img/custom-field-autocomplete.png "Custom fields autocompletion") | ||
|
||
Then select custom field of your choice in the autocomplete list. Notice that (for some types) value field might change it's type, for example to select list. Type or select desirable value and save the changes! | ||
|
||
![custom-field-select-value](/img/custom-field-select-value.png "Custom fields - value selection") | ||
|
||
> For any object, there could be at most one value for each custom field attached to it (in other words, you cannot have the same custom field attached to single object multiple times). | ||
## API | ||
|
||
You could change custom fields through Ralph API as simple as using it's GUI! | ||
|
||
## Reading custom fields | ||
|
||
Custom fields are attached in read-only form to any API (applicable) resource as a key-value dictionary. | ||
|
||
> The key in this dictionary is `attribute_name` defined on custom field. As pointed above, it's slugged name of the custom field. | ||
Example: | ||
``` | ||
{ | ||
... | ||
"custom_fields": { | ||
"monitoring": "zabbix", | ||
"docker_version": "1.11" | ||
}, | ||
... | ||
} | ||
``` | ||
|
||
## Filtering | ||
|
||
You could easily filter objects by value of custom field of your choice. Preprend `attribute_name` by `customfield__` in URL of list of objects to select only matching to custom field of your choice, for example: `http://<YOUR-RALPH-URL>/api/data-center-assets/?customfield__docker_version=1.10`. | ||
|
||
|
||
## Changing custom fields | ||
|
||
To preview custom fields in REST-friendly way, go to `http://<YOUR-RALPH-URL>/api/<YOUR-RESOURCE-URL>/customfields/`, for example `http://<YOUR-RALPH-URL>/api/assetmodels/1234/customfields/`. Here you have custom fields attached to this particular object (in this case to model with id `1234`). | ||
|
||
Example: | ||
``` | ||
{ | ||
"count": 2, | ||
"next": null, | ||
"previous": null, | ||
"results": [ | ||
{ | ||
"id": 1, | ||
"custom_field": { | ||
"name": "docker version", | ||
"attribute_name": "docker_version", | ||
"type": "string", | ||
"default_value": "1.10" | ||
}, | ||
"value": "1.11", | ||
"url": "http://<YOUR-RALPH-URL>/api/assetmodels/1234/customfields/1/" | ||
}, | ||
{ | ||
"id": 29, | ||
"custom_field": { | ||
"name": "monitoring", | ||
"attribute_name": "monitoring", | ||
"type": "choice list", | ||
"default_value": "zabbix" | ||
}, | ||
"value": "zabbix", | ||
"url": "http://<YOUR-RALPH-URL>/api/assetmodels/1234/customfields/29/" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
|
||
You could attach here new custom field value for this object (make POST request on custom fields list) or update any existing custom field value (make PUT or PATCH request on selected custom field value, ex. `http://<YOUR-RALPH-URL>/api/assetmodels/1234/customfields/29/`). For example you could make POST to `http://<YOUR-RALPH-URL>/api/assetmodels/1234/customfields/` request with following data to attach new custom field to Asset Model with ID `1234`: | ||
``` | ||
{ | ||
"value": "http://ralph.allegrogroup.com/manual.pdf", | ||
"custom_field": "manual_url" | ||
} | ||
``` | ||
|
||
> You could use custom field ID or attribute name to point it in API. | ||
> Notice that every action here will happen in context of particular object - every custom field will be attached to resource pointed by current url (ex. `/assetmodels/1234`). |
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ Werkzeug | |
pudb | ||
ipython | ||
ipdb | ||
isort==4.2.2 | ||
isort==4.2.5 |
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
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
Oops, something went wrong.