-
Notifications
You must be signed in to change notification settings - Fork 175
Add the ability to create an arbitrary schema for rendering elsewhere #403
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
Open
mutantsan
wants to merge
6
commits into
ckan:master
Choose a base branch
from
mutantsan:add-arbitrary-schemas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5615c49
add the ability to create an arbitrary scheme for rendering elsewhere
mutantsan 6654fe6
fix: return licenses to package_form template
mutantsan 49ebdc2
fix: fix tests
mutantsan 54bf426
doc: update readme
mutantsan 507efcd
Fix: do not bump version, fix render_fields.html formatting
mutantsan 6de69bd
Merge remote-tracking branch 'origin/master' into add-arbitrary-schemas
mutantsan 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 hidden or 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 hidden or 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,27 @@ | ||
scheming_version: 2 | ||
schema_id: ckanext_notifier | ||
about: An example of a config schema for a fictional extension | ||
|
||
fields: | ||
- field_name: ckanext.ckanext_notifier.enable_notifications | ||
label: Enable notifications | ||
validators: default(true) boolean_validator | ||
preset: select | ||
required: true | ||
choices: | ||
- value: true | ||
label: Enable | ||
- value: false | ||
label: Disable | ||
|
||
- field_name: ckanext.ckanext_notifier.notify_to_email | ||
label: Notification email | ||
validators: unicode_safe email_validator | ||
required: true | ||
help_text: Specify the email address to which the notification will be sent | ||
|
||
- field_name: ckanext.ckanext_notifier.frequency | ||
label: Notification frequency in seconds | ||
validators: default(3600) int_validator | ||
required: true | ||
input_type: number |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
ckanext/scheming/templates/scheming/snippets/render_fields.html
This file contains hidden or 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,30 @@ | ||
{# | ||
fields - a list of scheming field dictionaries | ||
data - form data fields | ||
errors - A dict of errors for the fields | ||
entity_type - entity type | ||
object_type - object type | ||
set_fields_defaults - flag to set the default field values | ||
{#} | ||
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. this is cute but doing comments like this could lead to confusion about where the comment starts and ends 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. Fixed |
||
|
||
{% for field in fields if field.form_snippet is not none %} | ||
{% if field.field_name not in data and set_fields_defaults %} | ||
{# Set the field default value before rendering but only if | ||
it doesn't already exist in data which would mean the form | ||
has been submitted. #} | ||
{% if field.default_jinja2 %} | ||
{% do data.__setitem__(field.field_name, h.scheming_render_from_string(field.default_jinja2)) %} | ||
{% elif field.default %} | ||
{% do data.__setitem__(field.field_name, field.default) %} | ||
{% endif %} | ||
{% endif %} | ||
|
||
{% snippet 'scheming/snippets/form_field.html', | ||
field=field, | ||
data=data, | ||
errors=errors, | ||
licenses=licenses, | ||
entity_type=entity_type, | ||
object_type=object_type | ||
%} | ||
{% endfor %} |
This file contains hidden or 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,47 @@ | ||
import pytest | ||
from flask import render_template | ||
from bs4 import BeautifulSoup | ||
|
||
import ckan.plugins.toolkit as tk | ||
|
||
|
||
class TestArbitrarySchema: | ||
def test_arbitrary_schema_structure(self): | ||
schema = tk.h.scheming_get_arbitrary_schema("ckanext_notifier") | ||
|
||
assert schema["scheming_version"] | ||
assert schema["schema_id"] == "ckanext_notifier" | ||
assert schema["about"] | ||
assert isinstance(schema["fields"], list) | ||
|
||
@pytest.mark.usefixtures("with_request_context") | ||
def test_render_arbitrary_schema(self, app): | ||
schema = tk.h.scheming_get_arbitrary_schema("ckanext_notifier") | ||
|
||
result = render_template( | ||
"scheming/snippets/render_fields.html", | ||
fields=schema["fields"], | ||
data={}, | ||
errors={}, | ||
) | ||
|
||
soup = BeautifulSoup(result) | ||
|
||
assert len(soup.select("div.form-group")) == 3 | ||
|
||
|
||
@pytest.mark.usefixtures("with_plugins") | ||
class TestGetArbitrarySchemaHelper: | ||
def test_get_all_arbitrary_schemas(self): | ||
assert tk.h.scheming_arbitrary_schemas() | ||
|
||
@pytest.mark.ckan_config("scheming.arbitrary_schemas", "") | ||
def test_get_all_arbitrary_schemas_if_none(self): | ||
assert not tk.h.scheming_arbitrary_schemas() | ||
|
||
def test_get_specific_schema(self): | ||
assert tk.h.scheming_get_arbitrary_schema("ckanext_notifier") | ||
|
||
@pytest.mark.ckan_config("scheming.arbitrary_schemas", "") | ||
def test_get_specific_schema_if_none(self): | ||
assert not tk.h.scheming_get_arbitrary_schema("ckanext_notifier") |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.