Django Library PaTT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forms.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.db.models import Q
  4. from django.utils.translation import gettext as _
  5. from .models import Task, Project, Comment, Search, TASKSTATE_CHOICES, PROJECTSTATE_OPEN, ModelList, PattUserProfile, get_pattuserprofile
  6. from .search import INDEX_STATES
  7. class PattUserProfileForm(forms.ModelForm):
  8. class Meta:
  9. model = PattUserProfile
  10. fields = ['threshold_very_soon', 'threshold_soon']
  11. labels = {
  12. 'threshold_very_soon': _('Period to targetdate for warning Group "very soon"'),
  13. 'threshold_soon': _('Period to targetdate for warning Group "soon"'),
  14. }
  15. def __init__(self, request):
  16. if request.POST:
  17. super().__init__(data=request.POST, instance=get_pattuserprofile(request.user))
  18. else:
  19. super().__init__(instance=get_pattuserprofile(request.user))
  20. class TaskForm(forms.ModelForm):
  21. def __init__(self, *args, **kwargs):
  22. # get request from kwargs
  23. try:
  24. self.request = kwargs.pop('request')
  25. except KeyError:
  26. raise TypeError("needed request object is missing in kwargs")
  27. plist = self.__projectlist_taskedit__(self.request.user)
  28. if self.request.POST:
  29. project = Project.objects.get(id=self.request.POST.get('project'))
  30. else:
  31. try:
  32. project = kwargs.get('instance').project
  33. except Task.project.RelatedObjectDoesNotExist:
  34. if len(plist) > 0:
  35. project = plist[0]
  36. # init TaskForm
  37. super(TaskForm, self).__init__(*args, **kwargs)
  38. # set projectchoice for project
  39. self.fields['project'].queryset = plist
  40. self.fields['project'].empty_label = None
  41. # set userlist (projectdepending)
  42. self.fields['assigned_user'].empty_label = _('----- choose a user -----')
  43. if project is not None:
  44. self.fields['assigned_user'].queryset = self.__userlist_taskedit__(self.request.user, project)
  45. def __projectlist_taskedit__(self, user):
  46. if user.is_superuser:
  47. rv = Project.objects.all()
  48. else:
  49. rv = Project.objects.filter(
  50. Q(role_leader__id__exact=user.id) | Q(role_member__id__exact=user.id),
  51. state=PROJECTSTATE_OPEN,
  52. ).distinct()
  53. rv = ModelList(rv)
  54. rv.sort()
  55. return Project.objects.filter(pk__in=[p.pk for p in rv])
  56. def __userlist_taskedit__(self, user, project=None):
  57. if user.is_superuser:
  58. return User.objects.all().order_by('username')
  59. elif user in project.role_leader.all():
  60. uids = [u.id for u in project.role_leader.all() | project.role_member.all()]
  61. else:
  62. uids = [user.id]
  63. return User.objects.filter(id__in=set(uids)).order_by('username')
  64. class Meta:
  65. model = Task
  66. fields = ['name', 'project', 'assigned_user', 'targetdate', 'priority', 'state', 'progress', 'description']
  67. widgets = {
  68. 'assigned_user': forms.Select(attrs={'required': True}),
  69. 'project': forms.Select(attrs={'onchange': 'submit()'}),
  70. 'state': forms.Select(choices=TASKSTATE_CHOICES),
  71. 'targetdate': forms.DateInput(format="%Y-%m-%d"),
  72. 'description': forms.Textarea(attrs={'rows': 5}),
  73. }
  74. class TaskFormLimited(forms.ModelForm):
  75. def __init__(self, *args, **kwargs):
  76. # remove request from kwargs
  77. kwargs.pop('request')
  78. super(TaskFormLimited, self).__init__(*args, **kwargs)
  79. class Meta:
  80. model = Task
  81. fields = ['state', 'progress', 'description']
  82. widgets = {
  83. 'state': forms.Select(choices=TASKSTATE_CHOICES[:2]),
  84. 'description': forms.Textarea(attrs={'rows': 5}),
  85. }
  86. class ProjectForm(forms.ModelForm):
  87. class Meta:
  88. model = Project
  89. fields = ['name', 'state', 'role_leader', 'role_member', 'role_visitor', 'description']
  90. widgets = {
  91. 'description': forms.Textarea(attrs={'rows': 5}),
  92. }
  93. def __init__(self, *args, **kwargs):
  94. super(ProjectForm, self).__init__(*args, **kwargs)
  95. self.fields['role_leader'].queryset = User.objects.order_by('username')
  96. self.fields['role_member'].queryset = User.objects.order_by('username')
  97. class CommentForm(forms.ModelForm):
  98. class Meta:
  99. model = Comment
  100. fields = ['type', 'comment']
  101. def __init__(self, *args, **kwargs):
  102. try:
  103. acc = kwargs.pop('acc')
  104. except KeyError:
  105. disable_type = True
  106. else:
  107. disable_type = not acc.modify_comment
  108. super(forms.ModelForm, self).__init__(*args, **kwargs)
  109. self.fields['type'].disabled = disable_type
  110. class TaskCommentForm(forms.ModelForm):
  111. class Meta:
  112. model = Comment
  113. fields = ['comment']
  114. def __init__(self, *args, **kwargs):
  115. super(forms.ModelForm, self).__init__(*args, **kwargs)
  116. self.fields['comment'].required = False
  117. class SearchForm(forms.ModelForm):
  118. class Meta:
  119. model = Search
  120. fields = ['name', 'search_txt']
  121. labels = {
  122. 'search_txt': _('Search Text'),
  123. }
  124. class EasySearchForm(forms.Form):
  125. user_ids = forms.MultipleChoiceField(required=False, label=_('Assigned User(s)'), widget=forms.widgets.SelectMultiple(attrs={'size': 6}))
  126. states = forms.MultipleChoiceField(required=False, label=_('State(s)'))
  127. prj_ids = forms.MultipleChoiceField(required=False, label=_('Project(s)'), widget=forms.widgets.SelectMultiple(attrs={'size': 10}))
  128. def __init__(self, *args, **kwargs):
  129. super(EasySearchForm, self).__init__(*args, **kwargs)
  130. self.fields['user_ids'].choices = [(u.id, u.username) for u in User.objects.order_by('username')]
  131. self.fields['states'].choices = [(INDEX_STATES.get(task_num), task_name) for task_num, task_name in TASKSTATE_CHOICES]
  132. self.fields['prj_ids'].choices = [(p.id, p.name) for p in Project.objects.order_by('state', 'name')]