# -*- coding: utf-8 -*- from __future__ import annotations 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.contrib.auth.forms import ( AuthenticationForm as BaseAuthenticationForm, PasswordChangeForm as BasePasswordChangeForm, ) from django.utils.translation import gettext_lazy as _ from .base import Form class LoginForm(BaseAuthenticationForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.attrs = { 'id': self.__class__.__name__, 'novalidate': '', } self.helper.layout = Layout( 'username', 'password', FormActions( Submit('submit', _('Log in'), css_class='btn btn-primary'), template='ui/ui/forms/formactions.html', css_class='mb-0', ), ) class ProfileForm(Form): INCLUDE_CANCEL = False username = forms.CharField( label=_('Username'), disabled=True, required=False, ) first_name = forms.CharField( label=_('First name'), max_length=150, required=True, ) last_name = forms.CharField( label=_('Last name'), max_length=150, required=True, ) email = forms.EmailField( label=_('E-mail address'), required=True, ) def get_layout_fields(self) -> list[str]: return [ 'username', 'first_name', 'last_name', 'email', ] class FederatedProfileForm(ProfileForm): username = forms.CharField( label=_('Username'), disabled=True, required=False, ) first_name = forms.CharField( label=_('First name'), disabled=True, required=False, ) last_name = forms.CharField( label=_('Last name'), disabled=True, required=False, ) email = forms.EmailField( label=_('E-mail address'), disabled=True, required=False, ) def get_submit_button(self) -> Submit: return Submit( 'submit', _('Save'), css_class='btn btn-primary', disabled=True, ) def clean(self) -> dict: return {} class PasswordForm(BasePasswordChangeForm): def get_submit_button(self) -> Submit: return Submit( 'submit', _('Save'), css_class='btn btn-primary', ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.form_class = 'form-horizontal' 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( 'old_password', 'new_password1', 'new_password2', FormActions( self.get_submit_button(), template='ui/ui/forms/formactions-horizontal.html', ), ) class FederatedPasswordForm(PasswordForm): current_password = forms.CharField( label=_('Old password'), disabled=True, required=False, ) new_password = forms.CharField( label=_('New password'), disabled=True, required=False, ) new_password_again = forms.CharField( label=_('New password confirmation'), disabled=True, required=False, ) def get_submit_button(self) -> Submit: return Submit( 'submit', _('Save'), css_class='btn btn-primary disable', disabled=True, ) def clean(self) -> dict: return {} class SettingsForm(Form): INCLUDE_CANCEL = False theme = forms.ChoiceField( label=_('Theme'), disabled=True, required=False, choices=[ (None, _('Bootstrap')), ('cosmo', _('Cosmo')), ], show_hidden_initial=True, ) auto_load_embeds = forms.ChoiceField( label=_('Auto load embedded content'), required=False, choices=[ (None, _('---')), (True, _('Yes')), (False, _('No')), ], help_text=_(( 'Auto loading embedded content (e.g. YouTube videos) means that ' 'you allow cookies from these sites.' )), ) def get_layout_fields(self) -> list[str]: return [ 'theme', 'auto_load_embeds', ] def get_submit_button(self) -> Submit: return Submit( 'submit', _('Save'), css_class='btn btn-primary', ) def clean(self) -> dict: result = super().clean() theme = result.get('theme', None) if not theme: result['theme'] = None auto_load_embeds = result.get('auto_load_embeds', None) if not auto_load_embeds: result['auto_load_embeds'] = None else: result['auto_load_embeds'] = (auto_load_embeds == 'True') return result