Skip to content
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

Feature/custom schedule validators #361

Merged
merged 10 commits into from
Jul 31, 2017
Merged

Conversation

drnlm
Copy link
Member

@drnlm drnlm commented Jul 27, 2017

To help address #358 , this refactors to schedule validation code to use explicit lists of validation functions.

This should make it much simpler to add custom validators to extend the behaviour of the schedule.

Copy link
Member

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some small comments, but looks cool otherwise!

return a list of invalid items or an empty list if the validator finds
no error.

Use ``register_schedule_item_validator`` and ``register_slot_vaildator``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/vail/vali/

Wafer runs validation on the slots and the schedule items. This behaviour
can be extended by providing custom validators.

Each validator is called with a list of all the items and is expected to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explain that item validators get a list of items and slot validators get a list of slots?


# Helpers for people extending the tests
def register_slot_validator(function, err_type, msg):
global SLOT_VALIDATORS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we're calling .append(...) we don't actually need the globals here but I guess it's not a bad idea to make it clear that these are globals. Happy either way, just checking intent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer the explicit globals declaration for clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.

for validator, err_type, _msg in SCHEDULE_ITEM_VALIDATORS:
failed_items = validator(all_items)
if failed_items:
errors[err_type] = failed_items
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried this can override errors if err_type is not unique (and the registration interface doesn't prevent that). Maybe we could do:

errors = defaultdict(list)
...
errors[err_type].extend(failed_items)

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - I'll make that change.

for validator, err_type, _msg in SLOT_VALIDATORS:
failed_slots = validator(all_slots)
if failed_slots:
errors[err_type] = failed_slots
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question about uniqueness of err_type here.

@@ -8,6 +8,7 @@
{% if errors %}
<div name="errors">
<h2>{% trans "Errors in the slots" %}</h2>
{% block displayerrors %}
{% if errors.overlaps %}
<h3>{% trans "OVerlapping slots" %}</h3>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/OVer/Over/

Use ``register_schedule_item_validator`` and ``register_slot_vaildator``
to add the validators to the list.

To display the errors in the admin form, you will also need to extend the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily for this PR, but it would be cool if the default displayerrors block at least reported all errors so that people didn't need to override this block just to see their custom errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be, but there's some custom rendering behaviour for clashes which makes that a bit tricky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing we could keep the special casing but still display everything else, but let's leave that for a later PR.

@drnlm drnlm merged commit 4243af6 into master Jul 31, 2017
@drnlm drnlm deleted the feature/custom_schedule_validators branch July 31, 2017 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants