forms.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. '''
  2. @author: olivier.massot, 2018
  3. '''
  4. from django import forms
  5. from django.contrib.auth.forms import UserCreationForm
  6. from django.contrib.auth.models import User
  7. from django.forms.fields import DateField, ChoiceField, MultipleChoiceField
  8. from martor.fields import MartorFormField
  9. from main.models import Story, Epic, Comment, Sprint
  10. class RegisterForm(UserCreationForm):
  11. class Meta:
  12. model = User
  13. fields = ('username', 'first_name', 'last_name', 'email')
  14. def save(self, commit=True):
  15. user = super(RegisterForm, self).save(commit=False)
  16. user.first_name = self.cleaned_data["first_name"]
  17. user.last_name = self.cleaned_data["last_name"]
  18. user.email = self.cleaned_data["email"]
  19. if commit:
  20. user.save()
  21. class ProfileForm(forms.ModelForm):
  22. class Meta:
  23. model = User
  24. fields = ('username', 'first_name', 'last_name', 'email')
  25. class EpicForm(forms.ModelForm):
  26. description = MartorFormField(label="Description")
  27. class Meta:
  28. model = Epic
  29. fields = ('project', 'name', 'size', 'value', 'description')
  30. def __init__(self, *args, **kwargs):
  31. super(EpicForm, self).__init__(*args, **kwargs)
  32. self.fields['description'].required = False
  33. class StoryForm(forms.ModelForm):
  34. description = MartorFormField(label="Description")
  35. epic = ChoiceField(choices=[(epic.id, epic.name) for epic in Epic.objects.filter(closed=False).order_by('-value')],
  36. widget=forms.Select(attrs={'class':'filtered-dropdown'}))
  37. assignees = MultipleChoiceField(choices=[(user.id, user.get_full_name() or user.username) for user in User.objects.all()])
  38. class Meta:
  39. model = Story
  40. widgets = {'author': forms.HiddenInput()}
  41. fields = ('epic', 'author', 'name', 'weight', 'time_spent', 'story_type', 'description', 'assignees', 'sprints')
  42. def __init__(self, *args, **kwargs):
  43. super(StoryForm, self).__init__(*args, **kwargs)
  44. self.fields['description'].required = False
  45. self.fields['assignees'].required = False
  46. self.fields['sprints'].required = False
  47. self.fields['weight'].required = False
  48. self.fields['time_spent'].required = False
  49. self.fields['story_type'].required = False
  50. def save(self, *args, **kwargs):
  51. if self.cleaned_data['weight'] == '':
  52. self.cleaned_data['weight'] = None
  53. return super(StoryForm, self).save(*args, **kwargs)
  54. class CommentForm(forms.ModelForm):
  55. class Meta:
  56. model = Comment
  57. widgets = {'content_type': forms.HiddenInput(), 'object_id': forms.HiddenInput()}
  58. fields = ('content','content_type', 'object_id')
  59. content = MartorFormField(label="Commentaire")
  60. def __init__(self, *args, **kwargs):
  61. super(CommentForm, self).__init__(*args, **kwargs)
  62. if not self.prefix:
  63. self.prefix = str(self.instance.id) if self.instance and self.instance.id else "new"
  64. class NewSprintForm(forms.ModelForm):
  65. class Meta:
  66. model = Sprint
  67. widgets = {'number': forms.HiddenInput()}
  68. fields = ('date_start','date_end','number')
  69. date_start = DateField(label='Date de début', widget=forms.TextInput(attrs={'class':'datepicker'}))
  70. date_end = DateField(label='Date de fin', widget=forms.TextInput(attrs={'class':'datepicker'}))
  71. def clean(self):
  72. cleaned_data = super().clean()
  73. if not cleaned_data.get("date_end") > cleaned_data.get("date_start"):
  74. raise forms.ValidationError("La date de fin doit être postérieure à la date de début.")
  75. if not cleaned_data.get("date_start") >= Sprint.current().date_end:
  76. raise forms.ValidationError("La date de début doit être postérieure à la date de fin du sprint en cours.")
  77. class SprintForm(forms.ModelForm):
  78. class Meta:
  79. model = Sprint
  80. fields = ('retro','improvements')
  81. retro = MartorFormField(label="Rétrospective")
  82. improvements = MartorFormField(label="Améliorations")