Django Library PaTT
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

forms.py 5.3KB

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