Skip to content

Commit

Permalink
- Upgraded django-crispy-forms to 1.7.2.
Browse files Browse the repository at this point in the history
- Instead of using FormHelper() directly, use FormHelperWithDefaults() where render_unmentioned_fields = True.
[django-crispy-forms/django-crispy-forms#848]

- TODO: check if forms will not contain unnecessary fields because of this change.
  • Loading branch information
uri-rodberg committed Jan 30, 2019
1 parent 13ce411 commit 05f00e4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# python==3.5
Django==1.11.18
django-crispy-forms==1.6.1
django-crispy-forms==1.7.2
django-environ==0.4.5
django-friendship==1.8.0
django-modeltranslation==0.12.2
Expand Down
27 changes: 13 additions & 14 deletions speedy/core/accounts/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from itertools import zip_longest

from crispy_forms.bootstrap import InlineField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Div, HTML, Row, Hidden, Layout
from django import forms
from django.conf import settings as django_settings
Expand All @@ -11,7 +10,7 @@
from django.utils.translation import gettext_lazy as _, pgettext_lazy
from django.core.exceptions import ValidationError

from speedy.core.base.forms import ModelFormWithDefaults
from speedy.core.base.forms import ModelFormWithDefaults, FormHelperWithDefaults
from speedy.core.accounts.utils import get_site_profile_model
from speedy.core.base.mail import send_mail
from speedy.core.base.utils import normalize_username
Expand Down Expand Up @@ -95,7 +94,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['slug'].label = _('New username')
self.fields['date_of_birth'].input_formats = django_settings.DATE_FIELD_FORMATS
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_input(Submit('submit', _('Create an account'), css_class='btn-lg btn-arrow-right'))

def save(self, commit=True):
Expand All @@ -120,7 +119,7 @@ def __init__(self, *args, **kwargs):
self.fields['date_of_birth'].input_formats = django_settings.DATE_FIELD_FORMATS
self.fields['date_of_birth'].widget.format = django_settings.DEFAULT_DATE_FIELD_FORMAT
self.fields['slug'].label = pgettext_lazy(context=self.instance.get_gender(), message='username (slug)')
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
# split into two columns
field_names = list(self.fields.keys())
self.helper.add_layout(Div(*[
Expand Down Expand Up @@ -153,7 +152,7 @@ def __init__(self, *args, **kwargs):
if (field.name in self._profile_fields):
self.fields[field.name] = field.formfield()
self.fields[field.name].initial = getattr(self.instance.profile, field.name)
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes')))

def save(self, commit=True):
Expand All @@ -173,7 +172,7 @@ def __init__(self, *args, **kwargs):
if ('username' in self.data):
self.data['username'] = self.data['username'].lower()
self.fields['username'].label = _('Email or Username')
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_layout(Div(
'username',
'password',
Expand All @@ -191,7 +190,7 @@ def confirm_login_allowed(self, user):
class PasswordResetForm(auth_forms.PasswordResetForm):
@property
def helper(self):
helper = FormHelper()
helper = FormHelperWithDefaults()
helper.add_input(Submit('submit', _('Submit')))
return helper

Expand All @@ -206,15 +205,15 @@ def send_mail(self, subject_template_name, email_template_name, context, from_em
class SetPasswordForm(AddAttributesToFieldsMixin, CleanNewPasswordMixin, auth_forms.SetPasswordForm):
@property
def helper(self):
helper = FormHelper()
helper = FormHelperWithDefaults()
helper.add_input(Submit('submit', _('Submit')))
return helper


class PasswordChangeForm(AddAttributesToFieldsMixin, CleanNewPasswordMixin, auth_forms.PasswordChangeForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_input(Hidden('_form', 'password'))
self.helper.add_input(Submit('submit', _('Change')))

Expand All @@ -227,7 +226,7 @@ class Meta:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
site = Site.objects.get_current()
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.user.get_gender(), message='Activate your {site_name} account').format(site_name=_(site.name))))

def save(self, commit=True):
Expand All @@ -243,7 +242,7 @@ def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
site = Site.objects.get_current()
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_input(Submit('submit', pgettext_lazy(context=self.user.get_gender(), message='Deactivate your {site_name} account').format(site_name=_(site.name)), css_class='btn-danger'))

def clean_password(self):
Expand All @@ -260,7 +259,7 @@ class Meta:

@property
def helper(self):
helper = FormHelper()
helper = FormHelperWithDefaults()
helper.add_input(Submit('submit', pgettext_lazy(context=self.defaults['user'].get_gender(), message='Add')))
return helper

Expand All @@ -272,7 +271,7 @@ class Meta:

@property
def helper(self):
helper = FormHelper()
helper = FormHelperWithDefaults()
helper.form_class = 'form-inline'
helper.form_action = reverse('accounts:change_email_privacy', kwargs={'pk': self.instance.id})
helper.field_template = 'bootstrap3/layout/inline_field.html'
Expand All @@ -289,7 +288,7 @@ class Meta:

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes')))


5 changes: 5 additions & 0 deletions speedy/core/base/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from crispy_forms.helper import FormHelper
from django import forms


Expand All @@ -15,3 +16,7 @@ def save(self, commit=True):
return instance


class FormHelperWithDefaults(FormHelper):
render_unmentioned_fields = True


5 changes: 2 additions & 3 deletions speedy/core/feedback/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Div, Row
from django.utils.translation import gettext_lazy as _, pgettext_lazy

from speedy.core.base.forms import ModelFormWithDefaults
from speedy.core.base.forms import ModelFormWithDefaults, FormHelperWithDefaults
from .models import Feedback


Expand All @@ -13,7 +12,7 @@ class Meta:

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
if (self.defaults.get('sender')):
del self.fields['sender_name']
del self.fields['sender_email']
Expand Down
4 changes: 2 additions & 2 deletions speedy/core/im/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from crispy_forms.bootstrap import InlineField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit
from django import forms
from django.urls import reverse
from django.utils.translation import pgettext_lazy

from speedy.core.base.forms import FormHelperWithDefaults
from .models import Message


Expand All @@ -19,7 +19,7 @@ def __init__(self, *args, **kwargs):
self.chat = kwargs.pop('chat', None)
assert bool(self.from_entity and self.to_entity) != bool(self.from_entity and self.chat)
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
if (self.chat):
self.helper.form_action = reverse('im:chat_send', kwargs={'chat_slug': self.chat.get_slug(current_user=self.from_entity)})
else:
Expand Down
4 changes: 2 additions & 2 deletions speedy/match/matches/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from itertools import zip_longest

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Div, Row, Submit, Field
from django.utils.translation import pgettext_lazy

from speedy.core.base.forms import FormHelperWithDefaults
from speedy.match.accounts.forms import SpeedyMatchProfileActivationForm


Expand All @@ -15,7 +15,7 @@ def get_fields(self):
class MatchSettingsFullForm(SpeedyMatchProfileActivationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper = FormHelperWithDefaults()
# split into two columns
field_names = list(self.fields.keys())
custom_field_names = ('gender_to_match', 'diet_match', 'smoking_status_match', 'marital_status_match')
Expand Down

0 comments on commit 05f00e4

Please sign in to comment.