diff --git a/fiesta/apps/sections/forms/plugin_configuration.py b/fiesta/apps/sections/forms/plugin_configuration.py index 50183865..0892388b 100644 --- a/fiesta/apps/sections/forms/plugin_configuration.py +++ b/fiesta/apps/sections/forms/plugin_configuration.py @@ -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]: @@ -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() diff --git a/fiesta/apps/sections/forms/plugin_state.py b/fiesta/apps/sections/forms/plugin_state.py index 2b825c27..cb9a044c 100644 --- a/fiesta/apps/sections/forms/plugin_state.py +++ b/fiesta/apps/sections/forms/plugin_state.py @@ -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): @@ -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() diff --git a/fiesta/apps/sections/services/sections_plugins_validator.py b/fiesta/apps/sections/services/sections_plugins_validator.py index 3b2340ab..217403d4 100644 --- a/fiesta/apps/sections/services/sections_plugins_validator.py +++ b/fiesta/apps/sections/services/sections_plugins_validator.py @@ -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 @@ -14,7 +15,7 @@ @dataclasses.dataclass(frozen=True) -class SectionsPluginsValidator: +class SectionPluginsValidator: """Defines relations between plugin configurations and validates them.""" section: Section @@ -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) @@ -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( @@ -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,