Update crispy FormHelper code.

This commit is contained in:
Casper V. Kristensen 2020-09-24 21:07:13 +02:00
parent 05871a7ddf
commit b118881a13
Signed by: caspervk
GPG key ID: 289CA03790535054
3 changed files with 50 additions and 53 deletions

View file

@ -6,10 +6,8 @@ from django import forms
class BankImportForm(forms.Form):
import_string = forms.CharField(widget=forms.Textarea)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Field("import_string"),
Submit("submit", "Import", css_class="btn btn-block btn-primary")
)
helper = FormHelper()
helper.layout = Layout(
Field("import_string"),
Submit("submit", "Import", css_class="btn btn-block btn-primary"),
)

View file

@ -31,7 +31,7 @@ class RaidResponseForm(ModelForm):
type="submit",
name="status",
value=RaidResponse.Status.SIGNED_UP,
css_class=f"btn-block {'btn-success' if not is_signed_up else 'btn-primary'}"
css_class=f"btn btn-block {'btn-success' if not is_signed_up else 'btn-primary'}"
),
css_class="form-group col-md-3"
),
@ -41,7 +41,7 @@ class RaidResponseForm(ModelForm):
type="submit",
name="status",
value=RaidResponse.Status.BACKUP,
css_class="btn-warning btn-block",
css_class="btn btn-block btn-warning",
disabled=self.instance.pk and self.instance.status == RaidResponse.Status.BACKUP
),
css_class="form-group col-md-2"
@ -52,7 +52,7 @@ class RaidResponseForm(ModelForm):
type="submit",
name="status",
value=RaidResponse.Status.SIGNED_OFF,
css_class="btn-danger btn-block",
css_class="btn btn-block btn-danger",
disabled=self.instance.pk and self.instance.status == RaidResponse.Status.SIGNED_OFF
),
css_class="form-group col-md-2"
@ -76,30 +76,31 @@ class GuestRaidResponseForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["status"].initial = RaidResponse.Status.SIGNED_UP
self.helper = FormHelper()
self.helper.layout = Layout(
Row(
Column("guest_name", css_class="col-12 col-md-3"),
Column("guest_klass", css_class="col-12 col-md-2"),
Column("role", css_class="col-12 col-md-2"),
Column("status", css_class="col-12 col-md-3"),
Column(
HTML("<label>&nbsp;</label>"), # offsets the button consistent with the other input elements
StrictButton(
"Add",
type="submit",
css_class=f"btn-block btn-primary"
),
css_class="form-group col-md-2"
helper = FormHelper()
helper.layout = Layout(
Row(
Column("guest_name", css_class="col-12 col-md-3"),
Column("guest_klass", css_class="col-12 col-md-2"),
Column("role", css_class="col-12 col-md-2"),
Column("status", css_class="col-12 col-md-3"),
Column(
HTML("<label>&nbsp;</label>"), # offsets the button consistent with the other input elements
Submit(
"submit",
"Add",
css_class="btn btn-block btn-primary"
),
css_class="form-row"
)
css_class="form-group col-md-2"
),
css_class="form-row"
)
self.helper.render_hidden_fields = True # Include MultiFormMixin's hidden form_id_field_prefix
self.helper.form_show_labels = True
self.helper.form_tag = False
)
helper.render_hidden_fields = True # Include MultiFormMixin's hidden form_id_field_prefix
helper.form_tag = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["status"].initial = RaidResponse.Status.SIGNED_UP
class RaidResponseChangeForm(ModelForm):
@ -118,8 +119,8 @@ class RaidForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper = FormHelper()
if self.instance.pk is None:
submit_button = Submit("submit", "Create", css_class="btn btn-success float-right")
else:
@ -134,16 +135,16 @@ class RaidForm(ModelForm):
),
submit_button
)
self.helper.render_hidden_fields = True
self.helper.render_hidden_fields = True # Include MultiFormMixin's hidden form_id_field_prefix
class RaidCommentForm(ModelForm):
helper = FormHelper()
helper.layout = Layout(
Field("body", label="lol"),
Submit("submit", "Submit"),
Field("body"),
Submit("submit", "Submit", css_class="btn btn-primary"),
)
helper.render_hidden_fields = True
helper.render_hidden_fields = True # Include MultiFormMixin's hidden form_id_field_prefix
helper.form_show_labels = False
class Meta:
@ -163,16 +164,14 @@ RaidResponseFormSet = inlineformset_factory(
class RaidResponseFormSetHelper(FormHelper):
template = "raids/raid_response_table_inline_formset.html"
def __init__(self, form=None):
super().__init__(form)
self.layout = Layout(
Field("character", css_class="character-select"),
Field("guest_name", css_class="guest-name"),
Field("guest_klass", css_class="guest-klass"),
Field("role", css_class="role-select"),
Field("status", css_class="status-select"),
Field("group", css_class="group-select"),
Field("note"),
Field("attendance", css_class="attendance-input", style="width: 10ch"),
)
self.form_tag = False
layout = Layout(
Field("character", css_class="character-select"),
Field("guest_name", css_class="guest-name"),
Field("guest_klass", css_class="guest-klass"),
Field("role", css_class="role-select"),
Field("status", css_class="status-select"),
Field("group", css_class="group-select"),
Field("note"),
Field("attendance", css_class="attendance-input", style="width: 10ch"),
)
form_tag = False

View file

@ -209,13 +209,13 @@ class RaidChangeView(PermissionRequiredMixin, SingleObjectMixin, MultiModelFormV
def get_raid_response_formset_instance(self):
return self.object
def get_success_url(self):
return reverse("raid_detail", kwargs={"pk": self.object.pk})
def get_context_data(self, **kwargs):
kwargs["raid_response_formset_helper"] = RaidResponseFormSetHelper()
return super().get_context_data(**kwargs)
def get_success_url(self):
return reverse("raid_detail", kwargs={"pk": self.object.pk})
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super().get(request, *args, **kwargs)