Brak opisu

forms.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # forms.py
  2. from django import forms
  3. from django.contrib.auth.forms import AuthenticationForm
  4. from django.contrib.auth.forms import UserCreationForm
  5. from django.contrib.auth.models import User
  6. from .models import UserProfile
  7. class CustomLoginForm(AuthenticationForm):
  8. username = forms.CharField(widget=forms.TextInput(attrs={
  9. 'placeholder': 'Username'
  10. }))
  11. password = forms.CharField(widget=forms.PasswordInput(attrs={
  12. 'placeholder': 'Password'
  13. }))
  14. # forms.py
  15. class CustomUserCreationForm(UserCreationForm):
  16. class Meta:
  17. model = User
  18. fields = ['username', 'email', 'password1', 'password2']
  19. def __init__(self, *args, **kwargs):
  20. super().__init__(*args, **kwargs)
  21. self.fields['username'].widget.attrs.update({
  22. 'placeholder': 'Username'
  23. })
  24. self.fields['email'].widget.attrs.update({
  25. 'placeholder': 'Email'
  26. })
  27. self.fields['password1'].widget.attrs.update({
  28. 'placeholder': 'Password'
  29. })
  30. self.fields['password2'].widget.attrs.update({
  31. 'placeholder': 'Confirm Password'
  32. })
  33. class UserProfileForm(forms.ModelForm):
  34. class Meta:
  35. model = UserProfile
  36. fields = ['profile_picture', 'position', 'signed_picture'] # Include the fields you want to manage
  37. class UserCustomForm(forms.ModelForm):
  38. # Profile fields
  39. profile_picture = forms.ImageField(required=False, label="Profile Picture")
  40. signed_picture = forms.ImageField(required=False, label="Signed Picture")
  41. position = forms.ChoiceField(
  42. choices=UserProfile.POSITION_CHOICES,
  43. required=False,
  44. label="Position"
  45. )
  46. class Meta:
  47. model = User
  48. fields = [
  49. "first_name",
  50. "last_name",
  51. "is_staff",
  52. "is_superuser",
  53. "is_active",
  54. ]
  55. def __init__(self, *args, **kwargs):
  56. # Allow passing `instance` to access both User and UserProfile objects
  57. user_instance = kwargs.pop('instance', None)
  58. profile_instance = getattr(user_instance, 'profile', None)
  59. super().__init__(instance=user_instance, *args, **kwargs)
  60. # Populate initial data for profile fields if `profile` exists
  61. if profile_instance:
  62. self.fields['profile_picture'].initial = profile_instance.profile_picture
  63. self.fields['signed_picture'].initial = profile_instance.signed_picture
  64. self.fields['position'].initial = profile_instance.position
  65. def save(self, commit=True):
  66. # Save the User instance first
  67. user = super().save(commit=commit)
  68. profile_data = {
  69. 'profile_picture': self.cleaned_data.get('profile_picture'),
  70. 'signed_picture': self.cleaned_data.get('signed_picture'),
  71. 'position': self.cleaned_data.get('position'),
  72. }
  73. # Ensure profile exists for the user
  74. profile, created = UserProfile.objects.get_or_create(user=user)
  75. for key, value in profile_data.items():
  76. setattr(profile, key, value)
  77. if commit:
  78. profile.save()
  79. return user