snapshot: preserve configurable onboarding conditional logic

This commit is contained in:
Md Bayazid Bostame
2026-03-27 12:54:47 +01:00
parent eb0fb811e4
commit 2e5e941d41
9 changed files with 431 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
from collections import OrderedDict
from django.utils.translation import get_language
from .models import FormFieldConfig, FormSectionConfig
from .models import FormConditionalRuleConfig, FormFieldConfig, FormSectionConfig
DEFAULT_FIELD_ORDER = {
@@ -132,6 +132,38 @@ OFFBOARDING_DEFAULT_PAGE = {
'notes': 'abschluss',
}
DEFAULT_CONDITIONAL_RULES = {
'onboarding': {
'business-card-box': {
'clauses': [{'field': 'order_business_cards', 'operator': 'checked', 'value': True}],
},
'employment-end-box': {
'clauses': [{'field': 'employment_type', 'operator': 'equals', 'value': 'befristet'}],
},
'group-mailboxes-box': {
'clauses': [{'field': 'group_mailboxes_required_choice', 'operator': 'equals', 'value': 'ja'}],
},
'extra-hardware-box': {
'clauses': [{'field': 'additional_hardware_needed_choice', 'operator': 'equals', 'value': 'ja'}],
},
'extra-software-box': {
'clauses': [{'field': 'additional_software_needed_choice', 'operator': 'equals', 'value': 'ja'}],
},
'extra-access-box': {
'clauses': [{'field': 'additional_access_needed_choice', 'operator': 'equals', 'value': 'ja'}],
},
'successor-box': {
'clauses': [{'field': 'successor_required_choice', 'operator': 'equals', 'value': 'ja'}],
},
'phone-box': {
'clauses': [
{'field': 'successor_required_choice', 'operator': 'equals', 'value': 'ja'},
{'field': 'inherit_phone_number_choice', 'operator': 'not_equals', 'value': 'ja'},
],
},
},
}
def get_section_order(form_type: str) -> list[str]:
if form_type == 'onboarding':
@@ -157,6 +189,10 @@ def get_default_page_map(form_type: str) -> dict[str, str]:
return {}
def get_default_conditional_rules(form_type: str) -> dict[str, dict]:
return DEFAULT_CONDITIONAL_RULES.get(form_type, {})
def _default_sort(form_type: str, field_name: str) -> int:
ordered = DEFAULT_FIELD_ORDER.get(form_type, [])
if field_name in ordered:
@@ -215,6 +251,35 @@ def ensure_form_section_configs(form_type: str) -> dict[str, FormSectionConfig]:
return existing
def ensure_form_conditional_rule_configs(form_type: str) -> dict[str, FormConditionalRuleConfig]:
defaults = get_default_conditional_rules(form_type)
if not defaults:
return {}
existing = {
cfg.target_key: cfg
for cfg in FormConditionalRuleConfig.objects.filter(form_type=form_type)
}
missing = [key for key in defaults.keys() if key not in existing]
if missing:
FormConditionalRuleConfig.objects.bulk_create(
[
FormConditionalRuleConfig(
form_type=form_type,
target_key=key,
clauses=defaults[key].get('clauses', []),
is_active=True,
)
for key in missing
],
ignore_conflicts=True,
)
existing = {
cfg.target_key: cfg
for cfg in FormConditionalRuleConfig.objects.filter(form_type=form_type)
}
return existing
def apply_form_field_config(form_type: str, form) -> None:
field_names = list(form.fields.keys())
configs = _ensure_configs(form_type, field_names)