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): class BankImportForm(forms.Form):
import_string = forms.CharField(widget=forms.Textarea) import_string = forms.CharField(widget=forms.Textarea)
def __init__(self, *args, **kwargs): helper = FormHelper()
super().__init__(*args, **kwargs) helper.layout = Layout(
self.helper = FormHelper()
self.helper.layout = Layout(
Field("import_string"), Field("import_string"),
Submit("submit", "Import", css_class="btn btn-block btn-primary") Submit("submit", "Import", css_class="btn btn-block btn-primary"),
) )

View file

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

View file

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