forms.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 martor.fields import MartorFormField
  8. from main.models import Story, Epic, Comment, Sprint
  9. from django.forms.fields import DateField
  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. class Meta:
  36. model = Story
  37. widgets = {'author': forms.HiddenInput()}
  38. fields = ('epic', 'author', 'name', 'weight', 'description', 'assignees', 'sprints')
  39. def __init__(self, *args, **kwargs):
  40. super(StoryForm, self).__init__(*args, **kwargs)
  41. self.fields['description'].required = False
  42. self.fields['assignees'].required = False
  43. self.fields['sprints'].required = False
  44. self.fields['weight'].required = False
  45. def save(self, *args, **kwargs):
  46. if self.cleaned_data['weight'] == '':
  47. self.cleaned_data['weight'] = None
  48. return super(StoryForm, self).save(*args, **kwargs)
  49. class CommentForm(forms.ModelForm):
  50. class Meta:
  51. model = Comment
  52. widgets = {'content_type': forms.HiddenInput(), 'object_id': forms.HiddenInput()}
  53. fields = ('content','content_type', 'object_id')
  54. content = MartorFormField(label="Commentaire")
  55. def __init__(self, *args, **kwargs):
  56. super(CommentForm, self).__init__(*args, **kwargs)
  57. if not self.prefix:
  58. self.prefix = str(self.instance.id) if self.instance and self.instance.id else "new"
  59. class NewSprintForm(forms.ModelForm):
  60. class Meta:
  61. model = Sprint
  62. widgets = {'number': forms.HiddenInput()}
  63. fields = ('date_start','date_end','number')
  64. date_start = DateField(label='Date de début', widget=forms.TextInput(attrs={'class':'datepicker'}))
  65. date_end = DateField(label='Date de fin', widget=forms.TextInput(attrs={'class':'datepicker'}))
  66. def clean(self):
  67. cleaned_data = super().clean()
  68. if not cleaned_data.get("date_end") > cleaned_data.get("date_start"):
  69. raise forms.ValidationError("La date de fin doit être postérieure à la date de début.")
  70. if not cleaned_data.get("date_start") >= Sprint.current().date_end:
  71. raise forms.ValidationError("La date de début doit être postérieure à la date de fin du sprint en cours.")
  72. class SprintForm(forms.ModelForm):
  73. class Meta:
  74. model = Sprint
  75. fields = ('retro',)
  76. retro = MartorFormField(label="Rétrospective")