Skip to content

Commit

Permalink
Remove "crispy forms" integration
Browse files Browse the repository at this point in the history
- No longer doing any back-end form rendering
  • Loading branch information
SchrodingersGat committed Dec 17, 2024
1 parent 24f433c commit a14e75b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 123 deletions.
114 changes: 1 addition & 113 deletions src/backend/InvenTree/InvenTree/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django import forms
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import Group
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
Expand All @@ -17,9 +17,6 @@
from allauth_2fa.adapter import OTPAdapter
from allauth_2fa.forms import TOTPDeviceForm
from allauth_2fa.utils import user_has_valid_totp_device
from crispy_forms.bootstrap import AppendedText, PrependedAppendedText, PrependedText
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Field, Layout
from dj_rest_auth.registration.serializers import RegisterSerializer
from rest_framework import serializers

Expand All @@ -31,115 +28,6 @@
logger = logging.getLogger('inventree')


class HelperForm(forms.ModelForm):
"""Provides simple integration of crispy_forms extension."""

# Custom field decorations can be specified here, per form class
field_prefix = {}
field_suffix = {}
field_placeholder = {}

def __init__(self, *args, **kwargs):
"""Setup layout."""
super(forms.ModelForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()

self.helper.form_tag = False
self.helper.form_show_errors = True

"""
Create a default 'layout' for this form.
Ref: https://django-crispy-forms.readthedocs.io/en/latest/layouts.html
This is required to do fancy things later (like adding PrependedText, etc).
Simply create a 'blank' layout for each available field.
"""

self.rebuild_layout()

def rebuild_layout(self):
"""Build crispy layout out of current fields."""
layouts = []

for field in self.fields:
prefix = self.field_prefix.get(field, None)
suffix = self.field_suffix.get(field, None)
placeholder = self.field_placeholder.get(field, '')

# Look for font-awesome icons
if prefix and prefix.startswith('fa-'):
prefix = f"<i class='fas {prefix}'/>"

if suffix and suffix.startswith('fa-'):
suffix = f"<i class='fas {suffix}'/>"

if prefix and suffix:
layouts.append(
Field(
PrependedAppendedText(
field,
prepended_text=prefix,
appended_text=suffix,
placeholder=placeholder,
)
)
)

elif prefix:
layouts.append(
Field(PrependedText(field, prefix, placeholder=placeholder))
)

elif suffix:
layouts.append(
Field(AppendedText(field, suffix, placeholder=placeholder))
)

else:
layouts.append(Field(field, placeholder=placeholder))

self.helper.layout = Layout(*layouts)


class SetPasswordForm(HelperForm):
"""Form for setting user password."""

class Meta:
"""Metaclass options."""

model = User
fields = ['enter_password', 'confirm_password', 'old_password']

enter_password = forms.CharField(
max_length=100,
min_length=8,
required=True,
initial='',
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
label=_('Enter password'),
help_text=_('Enter new password'),
)

confirm_password = forms.CharField(
max_length=100,
min_length=8,
required=True,
initial='',
widget=forms.PasswordInput(attrs={'autocomplete': 'off'}),
label=_('Confirm password'),
help_text=_('Confirm new password'),
)

old_password = forms.CharField(
label=_('Old password'),
strip=False,
required=False,
widget=forms.PasswordInput(
attrs={'autocomplete': 'current-password', 'autofocus': True}
),
)


# override allauth
class CustomLoginForm(LoginForm):
"""Custom login form to override default allauth behaviour."""
Expand Down
7 changes: 2 additions & 5 deletions src/backend/InvenTree/InvenTree/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@
'django_filters', # Extended filter functionality
'rest_framework', # DRF (Django Rest Framework)
'corsheaders', # Cross-origin Resource Sharing for DRF
'crispy_forms', # Improved form rendering
'import_export', # Import / export tables to file
'django_cleanup.apps.CleanupConfig', # Automatically delete orphaned MEDIA files
'mptt', # Modified Preorder Tree Traversal
Expand Down Expand Up @@ -1074,9 +1073,6 @@

DATE_INPUT_FORMATS = ['%Y-%m-%d']

# crispy forms use the bootstrap templates
CRISPY_TEMPLATE_PACK = 'bootstrap4'

# Use database transactions when importing / exporting data
IMPORT_EXPORT_USE_TRANSACTIONS = True

Expand Down Expand Up @@ -1336,11 +1332,12 @@
'signup': 'InvenTree.forms.CustomSignupForm',
'add_email': 'allauth.account.forms.AddEmailForm',
'change_password': 'allauth.account.forms.ChangePasswordForm',
'set_password': 'allauth.account.forms.SetPasswordForm',
# 'set_password': 'allauth.account.forms.SetPasswordForm',
'reset_password': 'allauth.account.forms.ResetPasswordForm',
'reset_password_from_key': 'allauth.account.forms.ResetPasswordKeyForm',
'disconnect': 'allauth.socialaccount.forms.DisconnectForm',
}

ALLAUTH_2FA_FORMS = {'setup': 'InvenTree.forms.CustomTOTPDeviceForm'}
# Determine if multi-factor authentication is enabled for this server (default = True)
MFA_ENABLED = get_boolean_setting('INVENTREE_MFA_ENABLED', 'mfa_enabled', True)
Expand Down
1 change: 0 additions & 1 deletion src/backend/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ django-allauth[openid,saml] # SSO for external providers via OpenI
django-allauth-2fa # MFA / 2FA
django-cleanup # Automated deletion of old / unused uploaded files
django-cors-headers # CORS headers extension for DRF
django-crispy-forms<2.0 # Form helpers # FIXED 2023-02-18 due to required updates in the new version
django-dbbackup # Backup / restore of database and media files
django-error-report-2 # Error report viewer for the admin interface
django-filter # Extended filtering options
Expand Down
4 changes: 0 additions & 4 deletions src/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,6 @@ django-cors-headers==4.6.0 \
--hash=sha256:14d76b4b4c8d39375baeddd89e4f08899051eeaf177cb02a29bd6eae8cf63aa8 \
--hash=sha256:8edbc0497e611c24d5150e0055d3b178c6534b8ed826fb6f53b21c63f5d48ba3
# via -r src/backend/requirements.in
django-crispy-forms==1.14.0 \
--hash=sha256:35887b8851a931374dd697207a8f56c57a9c5cb9dbf0b9fa54314da5666cea5b \
--hash=sha256:bc4d2037f6de602d39c0bc452ac3029d1f5d65e88458872cc4dbc01c3a400604
# via -r src/backend/requirements.in
django-dbbackup==4.2.1 \
--hash=sha256:157a2ec10d482345cd75092e510ac40d6e2ee6084604a1d17abe178c2f06bc69 \
--hash=sha256:b23265600ead0780ca781b1b4b594949aaa8a20d74f08701f91ee9d7eb1f08cd
Expand Down

0 comments on commit a14e75b

Please sign in to comment.