justpalma

forms.py

Sep 25th, 2020
871
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from crispy_forms.helper import FormHelper
  2. from crispy_forms.layout import Submit
  3. from django import forms
  4. from django.contrib.auth.forms import UserCreationForm, SetPasswordForm
  5.  
  6. from user_mgmt.models import AccountUser, Club, Player
  7.  
  8.  
  9. class AccountClubCreationForm(UserCreationForm):
  10.  
  11.     helper = FormHelper()
  12.     helper.add_input(
  13.         Submit('submit', 'Register', css_class="btn btn-success")
  14.     )
  15.     helper.form_method = 'POST'
  16.  
  17.     def __init_(self, *args, **kwargs):
  18.         super().__init__(*args, **kwargs)
  19.         self.fields['username'].widget.attrs['placeholder'] = 'username'
  20.         self.fields['email'].widget.attrs['placeholder'] = 'example@example.com'
  21.         self.fields['password1'].widget.attrs['placeholder'] = 'choose your password wisely'
  22.         self.fields['password2'].widget.attrs['placeholder'] = 'ripeta la password inserita'
  23.  
  24.  
  25.     def save(self, commit=True):
  26.         user = super().save(commit=False)
  27.         user.is_club = True
  28.         if commit:
  29.             user.save()
  30.         return user
  31.  
  32.     class Meta:
  33.         model = AccountUser
  34.         fields = (
  35.             'username',
  36.             'email',
  37.             'password1',
  38.             'password2',
  39.             'terms_of_service_acceptance',
  40.         )
  41.  
  42.  
  43. class AccountPlayerCreationForm(UserCreationForm):
  44.  
  45.     helper = FormHelper()
  46.     helper.add_input(
  47.         Submit('submit', 'Register', css_class="btn btn-success")
  48.     )
  49.     helper.form_method = 'POST'
  50.  
  51.     def __init_(self, *args, **kwargs):
  52.         super().__init__(*args, **kwargs)
  53.         self.fields['username'].widget.attrs['placeholder'] = 'username'
  54.         self.fields['email'].widget.attrs['placeholder'] = 'example@example.com'
  55.         self.fields['password2'].widget.attrs['placeholder'] = 'ripeta la password inserita'
  56.  
  57.     def save(self, commit=True):
  58.         user = super().save(commit=False)
  59.         user.is_club = False
  60.         if commit:
  61.             user.save()
  62.         return user
  63.  
  64.     class Meta:
  65.         model = AccountUser
  66.         fields = (
  67.             'username',
  68.             'email',
  69.             'password1',
  70.             'password2',
  71.             'terms_of_service_acceptance',
  72.         )
  73.  
  74.  
  75. class ClubCreationForm(UserCreationForm):
  76.  
  77.     def __init_(self, *args, **kwargs):
  78.         super().__init__(*args, **kwargs)
  79.         self.fields['club_name'].widget.attrs['placeholder'] = 'Nome del centro sportivo'
  80.         self.fields['middle_name'].widget.attrs['placeholder'] = 'Se presente'
  81.         self.fields['city'].widget.attrs['placeholder'] = 'Città'
  82.         self.helper = FormHelper()
  83.         self.helper.add_input(
  84.             Submit('submit', 'Register', css_class='btn btn-success')
  85.         )
  86.  
  87.     class Meta:
  88.         model = Club
  89.         fields = (
  90.             'first_name',
  91.             'middle_name',
  92.             'last_name',
  93.             'club_name',
  94.             'city',
  95.         )
  96.  
  97.  
  98. class PlayerCreationForm(UserCreationForm):
  99.     email = forms.EmailField(required=True)
  100.  
  101.     def __init__(self, *args, **kwargs):
  102.         super().__init__(*args, **kwargs)
  103.         self.fields['middle_name'].widget.attrs['placeholder'] = 'Se presente'
  104.         self.helper = FormHelper()
  105.         self.helper.add_input(
  106.             Submit('submit', 'Register', css_class='btn btn-success')
  107.         )
  108.  
  109.     class Meta:
  110.         model = Player
  111.         fields = (
  112.             'first_name',
  113.             'middle_name',
  114.             'last_name',
  115.         )
RAW Paste Data