123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- from django import forms
- from django.contrib.auth.models import User
- from django.db.models import Q
- from django.utils.translation import gettext as _
- from .models import Task, Project, Comment, Search, TASKSTATE_CHOICES, PROJECTSTATE_OPEN, ModelList
- from .search import INDEX_STATES
-
-
- class TaskForm(forms.ModelForm):
-
- def __init__(self, *args, **kwargs):
- # get request from kwargs
- try:
- self.request = kwargs.pop('request')
- except KeyError:
- raise TypeError("needed request object is missing in kwargs")
- plist = self.__projectlist_taskedit__(self.request.user)
- if self.request.POST:
- project = Project.objects.get(id=self.request.POST.get('project'))
- else:
- try:
- project = kwargs.get('instance').project
- except Task.project.RelatedObjectDoesNotExist:
- if len(plist) > 0:
- project = plist[0]
- # init TaskForm
- super(TaskForm, self).__init__(*args, **kwargs)
- # set projectchoice for project
- self.fields['project'].queryset = plist
- self.fields['project'].empty_label = None
- # set userlist (projectdepending)
- self.fields['assigned_user'].empty_label = _('----- choose a user -----')
- if project is not None:
- self.fields['assigned_user'].queryset = self.__userlist_taskedit__(self.request.user, project)
-
- def __projectlist_taskedit__(self, user):
- if user.is_superuser:
- rv = Project.objects.all()
- else:
- rv = Project.objects.filter(
- Q(role_leader__id__exact=user.id) | Q(role_member__id__exact=user.id),
- state=PROJECTSTATE_OPEN,
- ).distinct()
- rv = ModelList(rv)
- rv.sort()
- return Project.objects.filter(pk__in=[p.pk for p in rv])
-
- def __userlist_taskedit__(self, user, project=None):
- if user.is_superuser:
- return User.objects.all().order_by('username')
- elif user in project.role_leader.all():
- uids = [u.id for u in project.role_leader.all() | project.role_member.all()]
- else:
- uids = [user.id]
- return User.objects.filter(id__in=set(uids)).order_by('username')
-
- class Meta:
- model = Task
- fields = ['project', 'assigned_user', 'name', 'state', 'priority', 'targetdate', 'progress', 'description']
- widgets = {
- 'assigned_user': forms.Select(attrs={'required': True}),
- 'project': forms.Select(attrs={'onchange': 'submit()'}),
- 'state': forms.Select(choices=TASKSTATE_CHOICES),
- 'targetdate': forms.DateInput(format="%Y-%m-%d"),
- 'description': forms.Textarea(attrs={'rows': 5}),
- }
-
-
- class TaskFormLimited(forms.ModelForm):
- def __init__(self, *args, **kwargs):
- # remove request from kwargs
- kwargs.pop('request')
- super(TaskFormLimited, self).__init__(*args, **kwargs)
-
- class Meta:
- model = Task
- fields = ['state', 'progress', 'description']
- widgets = {
- 'state': forms.Select(choices=TASKSTATE_CHOICES[:2]),
- 'description': forms.Textarea(attrs={'rows': 5}),
- }
-
-
- class ProjectForm(forms.ModelForm):
- class Meta:
- model = Project
- fields = ['name', 'state', 'role_leader', 'role_member', 'role_visitor', 'description', 'days_late', 'days_very_soon', 'days_soon']
- widgets = {
- 'description': forms.Textarea(attrs={'rows': 5}),
- }
- labels = {
- 'days_late': _('Days to deadline (late)'),
- 'days_very_soon': _('Days to deadline (very soon)'),
- 'days_soon': _('Days to deadline (soon)'),
- }
-
- def __init__(self, *args, **kwargs):
- super(ProjectForm, self).__init__(*args, **kwargs)
- self.fields['role_leader'].queryset = User.objects.order_by('username')
- self.fields['role_member'].queryset = User.objects.order_by('username')
-
-
- class CommentForm(forms.ModelForm):
- class Meta:
- model = Comment
- fields = ['comment']
-
-
- class TaskCommentForm(forms.ModelForm):
- class Meta:
- model = Comment
- fields = ['comment']
-
- def __init__(self, *args, **kwargs):
- super(forms.ModelForm, self).__init__(*args, **kwargs)
- self.fields['comment'].required = False
-
-
- class SearchForm(forms.ModelForm):
- class Meta:
- model = Search
- fields = ['name', 'search_txt']
- labels = {
- 'search_txt': _('Search Text'),
- }
-
-
- class EasySearchForm(forms.Form):
- user_ids = forms.MultipleChoiceField(required=False, label=_('Assigned User(s)'), widget=forms.widgets.SelectMultiple(attrs={'size': 6}))
- states = forms.MultipleChoiceField(required=False, label=_('State(s)'))
- prj_ids = forms.MultipleChoiceField(required=False, label=_('Project(s)'), widget=forms.widgets.SelectMultiple(attrs={'size': 10}))
-
- def __init__(self, *args, **kwargs):
- super(EasySearchForm, self).__init__(*args, **kwargs)
- self.fields['user_ids'].choices = [(u.id, u.username) for u in User.objects.order_by('username')]
- self.fields['states'].choices = [(INDEX_STATES.get(task_num), task_name) for task_num, task_name in TASKSTATE_CHOICES]
- self.fields['prj_ids'].choices = [(p.id, p.name) for p in Project.objects.order_by('state', 'name')]
|