Skip to content

Commit

Permalink
✨ [#1068] Add formio.formatters module
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jan 4, 2022
1 parent 3568373 commit 984f245
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/openforms/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
"openforms.config",
"openforms.emails",
"openforms.formio",
"openforms.formio.formatters.apps.FormIOFormattersApp",
"openforms.forms",
"openforms.multidomain",
"openforms.products",
Expand Down
12 changes: 12 additions & 0 deletions src/openforms/formio/formatters/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _


class FormIOFormattersApp(AppConfig):
name = "openforms.formio.formatters"
label = "formio_formatters"
verbose_name = _("FormIO formatters")

def ready(self):
# register the plugin
from . import default # noqa
1 change: 1 addition & 0 deletions src/openforms/formio/formatters/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO implement: iban, bsn, postcode, licenseplate, npFamilyMembers, cosign, map
114 changes: 114 additions & 0 deletions src/openforms/formio/formatters/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from typing import Any, Dict

from openforms.plugins.plugin import AbstractBasePlugin

from .registry import register


class FormioFormatter(AbstractBasePlugin):
def __call__(self, component: dict, value: Any) -> str:
return self.format(component, value)

def format(self, component: dict, value: Any) -> str:
raise NotImplementedError("%r must implement the 'format' method" % type(self))


@register("default")
class DefaultFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
if isinstance(value, list):
return value
return str(value)


@register("textfield")
class TextFieldFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
return str(value)


@register("email")
class EmailFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
return str(value)


@register("date")
class DateFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("time")
class TimeFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("phoneNumber")
class PhoneNumberFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("file")
class FileFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("textarea")
class TextAreaFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("number")
class NumberFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("password")
class PasswordFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("checkbox")
class CheckboxFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("selectboxes")
class SelectBoxesFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
selected_labels = [
entry["label"] for entry in component["values"] if value.get(entry["value"])
]
return ", ".join(selected_labels)


@register("select")
class SelectFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("currency")
class CurrencyFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


@register("radio")
class RadioFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError


# @register("signature")
class SignatureFormatter(FormioFormatter):
def format(self, component: Dict, value: str) -> str:
raise NotImplementedError
14 changes: 14 additions & 0 deletions src/openforms/formio/formatters/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from openforms.plugins.registry import BaseRegistry


class Registry(BaseRegistry):
"""
A registry for the FormIO formatters.
"""

pass


# Sentinel to provide the default registry. You an easily instantiate another
# :class:`Registry` object to use as dependency injection in tests.
register = Registry()

0 comments on commit 984f245

Please sign in to comment.