forms.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from django import forms
  2. from .models import Musician, Song, Rating, Meloman
  3. from django.contrib.auth.models import User
  4. from django.contrib.auth.forms import UserCreationForm
  5. class MusicianForm(forms.ModelForm):
  6. class Meta:
  7. model = Musician
  8. fields = ['name', 'city', 'birth_year']
  9. widgets = {
  10. 'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Название исполнителя'}),
  11. 'city': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Город'}),
  12. 'birth_year': forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Год основания'}),
  13. }
  14. labels = {
  15. 'name': 'Имя исполнителя',
  16. 'city': 'Город',
  17. 'birth_year': 'Год рождения/основания',
  18. }
  19. class SongForm(forms.ModelForm):
  20. class Meta:
  21. model = Song
  22. fields = ['title', 'duration_sec', 'album', 'genre']
  23. widgets = {
  24. 'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Название песни'}),
  25. 'duration_sec': forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Длительность в секундах'}),
  26. 'album': forms.Select(attrs={'class': 'form-control'}),
  27. 'genre': forms.Select(attrs={'class': 'form-control'}),
  28. }
  29. class RatingForm(forms.ModelForm):
  30. class Meta:
  31. model = Rating
  32. fields = ['score']
  33. widgets = {
  34. 'score': forms.NumberInput(attrs={'min': 1, 'max': 10, 'class': 'form-control', 'style': 'width: 100px;'})
  35. }
  36. class RegistrationForm(UserCreationForm):
  37. email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}))
  38. full_name = forms.CharField(max_length=150, required=True, widget=forms.TextInput(attrs={'class': 'form-control'}))
  39. city = forms.CharField(max_length=100, required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
  40. birth_year = forms.IntegerField(required=False, widget=forms.NumberInput(attrs={'class': 'form-control'}))
  41. gender = forms.ChoiceField(choices=[('M', 'Мужской'), ('F', 'Женский')], required=False, widget=forms.Select(attrs={'class': 'form-control'}))
  42. class Meta:
  43. model = User
  44. fields = ['username', 'email', 'password1', 'password2']
  45. widgets = {
  46. 'username': forms.TextInput(attrs={'class': 'form-control'}),
  47. 'email': forms.EmailInput(attrs={'class': 'form-control'}),
  48. 'password1': forms.PasswordInput(attrs={'class': 'form-control'}),
  49. 'password2': forms.PasswordInput(attrs={'class': 'form-control'}),
  50. }
  51. def save(self, commit=True):
  52. user = super().save(commit=False)
  53. user.email = self.cleaned_data['email']
  54. if commit:
  55. user.save()
  56. # Создаем профиль меломана
  57. Meloman.objects.create(
  58. user=user,
  59. full_name=self.cleaned_data['full_name'],
  60. city=self.cleaned_data.get('city', ''),
  61. birth_year=self.cleaned_data.get('birth_year'),
  62. gender=self.cleaned_data.get('gender')
  63. )
  64. return user