snapshot: preserve dynamic builder and section ordering work
This commit is contained in:
@@ -3,7 +3,7 @@ from django import forms
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import get_language
|
||||
|
||||
from .models import FormConditionalRuleConfig, FormCustomFieldConfig, FormFieldConfig, FormSectionConfig
|
||||
from .models import FormConditionalRuleConfig, FormCustomFieldConfig, FormCustomSectionConfig, FormFieldConfig, FormSectionConfig
|
||||
|
||||
|
||||
DEFAULT_FIELD_ORDER = {
|
||||
@@ -72,6 +72,10 @@ OFFBOARDING_PAGE_LABELS = {
|
||||
'austritt': '2. Austritt',
|
||||
'abschluss': '3. Abschluss',
|
||||
}
|
||||
CORE_SECTION_LABELS = {
|
||||
'onboarding': ONBOARDING_PAGE_LABELS,
|
||||
'offboarding': OFFBOARDING_PAGE_LABELS,
|
||||
}
|
||||
|
||||
LOCKED_FIELD_RULES = {
|
||||
'onboarding': {'full_name', 'work_email', 'contract_start', 'agreement_confirm'},
|
||||
@@ -157,12 +161,6 @@ DEFAULT_CONDITIONAL_RULES = {
|
||||
'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'},
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -170,19 +168,11 @@ CUSTOM_FIELD_PREFIX = 'custom__'
|
||||
|
||||
|
||||
def get_section_order(form_type: str) -> list[str]:
|
||||
if form_type == 'onboarding':
|
||||
return ONBOARDING_PAGE_ORDER
|
||||
if form_type == 'offboarding':
|
||||
return OFFBOARDING_PAGE_ORDER
|
||||
return []
|
||||
return [item['key'] for item in get_section_definitions(form_type)]
|
||||
|
||||
|
||||
def get_section_labels(form_type: str) -> dict[str, str]:
|
||||
if form_type == 'onboarding':
|
||||
return ONBOARDING_PAGE_LABELS
|
||||
if form_type == 'offboarding':
|
||||
return OFFBOARDING_PAGE_LABELS
|
||||
return {}
|
||||
return {item['key']: item['title'] for item in get_section_definitions(form_type)}
|
||||
|
||||
|
||||
def get_default_page_map(form_type: str) -> dict[str, str]:
|
||||
@@ -193,6 +183,42 @@ def get_default_page_map(form_type: str) -> dict[str, str]:
|
||||
return {}
|
||||
|
||||
|
||||
def get_custom_section_configs(form_type: str, include_inactive: bool = False) -> list[FormCustomSectionConfig]:
|
||||
qs = FormCustomSectionConfig.objects.filter(form_type=form_type)
|
||||
if not include_inactive:
|
||||
qs = qs.filter(is_active=True)
|
||||
return list(qs.order_by('sort_order', 'section_key'))
|
||||
|
||||
|
||||
def get_section_definitions(form_type: str, include_inactive_custom: bool = False) -> list[dict[str, object]]:
|
||||
definitions: list[dict[str, object]] = []
|
||||
section_configs = ensure_form_section_configs(form_type)
|
||||
for cfg in sorted(section_configs.values(), key=lambda item: (item.sort_order, item.section_key)):
|
||||
label_map = CORE_SECTION_LABELS.get(form_type, {})
|
||||
definitions.append(
|
||||
{
|
||||
'key': cfg.section_key,
|
||||
'title': label_map.get(cfg.section_key, cfg.section_key),
|
||||
'locked': cfg.section_key in LOCKED_SECTION_RULES.get(form_type, set()),
|
||||
'is_custom': False,
|
||||
'sort_order': cfg.sort_order,
|
||||
}
|
||||
)
|
||||
for cfg in get_custom_section_configs(form_type, include_inactive=include_inactive_custom):
|
||||
definitions.append(
|
||||
{
|
||||
'key': cfg.section_key,
|
||||
'title': cfg.translated_title(get_language()),
|
||||
'locked': False,
|
||||
'is_custom': True,
|
||||
'is_active': cfg.is_active,
|
||||
'sort_order': cfg.sort_order,
|
||||
}
|
||||
)
|
||||
definitions.sort(key=lambda item: (item.get('sort_order', 9999), item['key']))
|
||||
return definitions
|
||||
|
||||
|
||||
def get_default_conditional_rules(form_type: str) -> dict[str, dict]:
|
||||
return DEFAULT_CONDITIONAL_RULES.get(form_type, {})
|
||||
|
||||
@@ -323,7 +349,7 @@ def ensure_form_field_configs(form_type: str, field_names: list[str]) -> dict[st
|
||||
|
||||
|
||||
def ensure_form_section_configs(form_type: str) -> dict[str, FormSectionConfig]:
|
||||
section_order = get_section_order(form_type)
|
||||
section_order = list(CORE_SECTION_LABELS.get(form_type, {}).keys())
|
||||
if not section_order:
|
||||
return {}
|
||||
existing = {
|
||||
@@ -333,7 +359,15 @@ def ensure_form_section_configs(form_type: str) -> dict[str, FormSectionConfig]:
|
||||
missing = [key for key in section_order if key not in existing]
|
||||
if missing:
|
||||
FormSectionConfig.objects.bulk_create(
|
||||
[FormSectionConfig(form_type=form_type, section_key=key, is_visible=True) for key in missing],
|
||||
[
|
||||
FormSectionConfig(
|
||||
form_type=form_type,
|
||||
section_key=key,
|
||||
sort_order=section_order.index(key),
|
||||
is_visible=True,
|
||||
)
|
||||
for key in missing
|
||||
],
|
||||
ignore_conflicts=True,
|
||||
)
|
||||
existing = {
|
||||
|
||||
Reference in New Issue
Block a user