Skip to main content

Classes to manually run form validation for clinicedc/edc projects

Project description

pypi actions codecov downloads

edc-form-runners

Classes to manually run modelform validation for clinicedc/edc projects.

Rerun modelform validation

You can use the FormRunner to rerun modelform validation on all instances for a model.

You could do this:

runner = FormRunner(modelform)
runner.run()

If modelform validation does not validate, the validation message is captures in model Issue.

You could also run for every model in your EDC deployment by getting the ModelForm class from the admin registry and running FormRunner:

from django.apps import apps as django_apps
from edc_form_runners.form_runners import (
    FormRunner,
    FormRunnerError,
    get_modelform_cls,
    )

for app_config in django_apps.get_app_configs():
    if app_config.name.startswith("edc_"):
        continue
    for model_cls in app_config.get_models():
        if model_cls == Appointment:
            continue
        print(model_cls._meta.label_lower)
        try:
            modelform = get_modelform_cls(model_cls._meta.label_lower)
        except FormRunnerError as e:
            print(e)
        else:
            print(modelform)
            try:
                runner = FormRunner(modelform)
            except AttributeError as e:
                print(f"{e}. See {model_cls._meta.label_lower}.")
            else:
                try:
                    runner.run()
                except (AttributeError, FieldError) as e:
                    print(f"{e}. See {model_cls._meta.label_lower}.")

You could also create a custom FormRunner for your model to add extra fields and ignore others.

For example:

class AppointmentFormRunner(FormRunner):
    def __init__(self, modelform_cls: ModelForm = None, **kwargs):
        modelform_cls = modelform_cls or AppointmentForm
        extra_fieldnames = ["appt_datetime"]
        ignore_fieldnames = ["appt_close_datetime"]
        super().__init__(
            modelform_cls=modelform_cls,
            extra_formfields=extra_fieldnames,
            ignore_formfields=ignore_fieldnames,
            **kwargs,
        )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edc-form-runners-0.1.0.tar.gz (32.0 kB view hashes)

Uploaded Source

Built Distribution

edc_form_runners-0.1.0-py3-none-any.whl (30.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page