# -*- coding: utf-8 -*- from __future__ import annotations import typing from crispy_forms.bootstrap import FormActions from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, Submit from django import forms from django.utils.translation import gettext_lazy as _ from .layout import CancelButton class Form(forms.Form): HORIZONTAL = True INCLUDE_CANCEL = True def get_layout_fields(self) -> list[str]: return [] def get_submit_button(self) -> Submit: return Submit('submit', _('Save'), css_class='btn btn-primary') def get_extra_actions(self) -> typing.Any: result = [] if self.INCLUDE_CANCEL is True: result.append(CancelButton(_('Cancel'))) return result def get_form_actions_template(self) -> str: if self.HORIZONTAL is True: return 'ui/ui/forms/formactions-horizontal.html' return 'ui/ui/forms/formactions.html' def get_form_helper_form_class(self) -> str: if self.HORIZONTAL is True: return 'form-horizontal' return '' def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.form_class = self.get_form_helper_form_class() self.helper.label_class = 'col-md-3 col-form-label' self.helper.field_class = 'col-md-9' self.helper.attrs = { 'id': self.__class__.__name__, 'novalidate': '', } self.helper.layout = Layout( *self.get_layout_fields(), FormActions( self.get_submit_button(), *self.get_extra_actions(), template=self.get_form_actions_template(), ), )