Skip to content

Commit

Permalink
fix(plugins): better validation messages for configuration validator
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 18, 2023
1 parent 7f1f74e commit 355d0a5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
4 changes: 2 additions & 2 deletions fiesta/apps/sections/forms/plugin_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from apps.fiestaforms.forms import BaseModelForm
from apps.plugins.models import BasePluginConfiguration
from apps.sections.models import Section
from apps.sections.services.sections_plugins_validator import SectionsPluginsValidator
from apps.sections.services.sections_plugins_validator import SectionPluginsValidator


def get_plugin_configuration_form(configuration: BasePluginConfiguration, for_section: Section) -> type[BaseModelForm]:
Expand All @@ -17,7 +17,7 @@ def _post_clean(self):
super()._post_clean()

try:
SectionsPluginsValidator.for_changed_conf(
SectionPluginsValidator.for_changed_conf(
section=for_section,
conf=self.instance,
).check_validity()
Expand Down
4 changes: 2 additions & 2 deletions fiesta/apps/sections/forms/plugin_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from apps.plugins.models import Plugin
from apps.plugins.plugin import BasePluginAppConfig
from apps.plugins.utils import all_plugins_mapped_to_label
from apps.sections.services.sections_plugins_validator import SectionsPluginsValidator
from apps.sections.services.sections_plugins_validator import SectionPluginsValidator


class ChangePluginStateForm(BaseModelForm):
Expand All @@ -35,7 +35,7 @@ def _post_clean(self):
super()._post_clean()

try:
SectionsPluginsValidator.for_changed_plugin(
SectionPluginsValidator.for_changed_plugin(
section=self.instance.section,
plugin=self.instance,
).check_validity()
Expand Down
60 changes: 47 additions & 13 deletions fiesta/apps/sections/services/sections_plugins_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.translation import gettext_lazy as _

from apps.buddy_system.apps import BuddySystemConfig
from apps.pickup_system.apps import PickupSystemConfig
from apps.plugins.models import BasePluginConfiguration, Plugin
from apps.plugins.plugin import BasePluginAppConfig
from apps.plugins.utils import all_plugins_mapped_to_class
Expand All @@ -14,7 +15,7 @@


@dataclasses.dataclass(frozen=True)
class SectionsPluginsValidator:
class SectionPluginsValidator:
"""Defines relations between plugin configurations and validates them."""

section: Section
Expand All @@ -28,21 +29,54 @@ def check_validity(self):
self._check_for_plugin(p)

def _check_for_plugin(self, plugin: Plugin):
match plugin.app_config:
case BuddySystemConfig() | SectionsConfig():
if not self.has_enabled_plugin(BuddySystemConfig):
return
sections_conf: SectionsConfiguration = self.get_configuration(SectionsConfig)

sections_conf: SectionsConfiguration = self.get_configuration(SectionsConfig)
# TODO: would be better to refactor to some kind of matrix:
# FIELD_DEPENDENCIES = {
# BuddySystemConfig: (SectionsConfig, SectionsConfiguration.required_faculty, lambda v: v),
# }

if not sections_conf.required_faculty:
raise ValidationError(
match plugin.app_config:
case (BuddySystemConfig() | PickupSystemConfig()):
self._check_field_dependency(
plugin=plugin,
field_value=sections_conf.required_faculty,
err=ValidationError(
_(
"With enabled Buddy system plugin, you need to require faculty "
"in Section plugin configuration."
)
"With enabled {plugin} plugin, you need to have enabled "
"faculty requirement in the ESN Section plugin configuration."
).format(plugin=plugin.app_config.verbose_name),
),
)
case SectionsConfig():
for cfg in (BuddySystemConfig, PickupSystemConfig):
app = all_plugins_mapped_to_class().get(cfg)

self._check_field_dependency(
plugin=self.plugins.get(app.label),
field_value=sections_conf.required_faculty,
err=ValidationError(
_(
"With enabled {plugin} plugin, you need to have enabled "
"faculty requirement in the ESN Section plugin configuration."
).format(plugin=app.verbose_name),
),
)

def _check_field_dependency(
self,
plugin: Plugin,
field_value: bool,
err: ValidationError,
):
if plugin.state == Plugin.State.DISABLED:
return

if field_value:
return

raise err

def has_enabled_plugin(self, app: type[BasePluginAppConfig]):
"""Checks if plugin is enabled."""
app_obj = all_plugins_mapped_to_class().get(app)
Expand All @@ -56,7 +90,7 @@ def get_configuration(self, app: type[BasePluginAppConfig]) -> BasePluginConfigu
return self.configurations.get(app_obj.label)

@classmethod
def for_changed_conf(cls, section: Section, conf: BasePluginConfiguration) -> SectionsPluginsValidator:
def for_changed_conf(cls, section: Section, conf: BasePluginConfiguration) -> SectionPluginsValidator:
"""Creates validator for standard state, but a configuration has been changed."""
plugin = conf.plugins.get(section=section)
return cls(
Expand All @@ -66,7 +100,7 @@ def for_changed_conf(cls, section: Section, conf: BasePluginConfiguration) -> Se
)

@classmethod
def for_changed_plugin(cls, section: Section, plugin: Plugin) -> SectionsPluginsValidator:
def for_changed_plugin(cls, section: Section, plugin: Plugin) -> SectionPluginsValidator:
"""Creates validator for standard state, but a plugin has been changed."""
return cls(
section=section,
Expand Down

0 comments on commit 355d0a5

Please sign in to comment.