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 5.7KB

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