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, PattUserProfile from .search import INDEX_STATES class PattUserProfileForm(forms.ModelForm): class Meta: model = PattUserProfile fields = ['threshold_very_soon', 'threshold_soon'] labels = { 'threshold_very_soon': _('Period to targetdate for warning Group "very soon"'), 'threshold_soon': _('Period to targetdate for warning Group "soon"'), } 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 = ['name', 'project', 'assigned_user', 'targetdate', 'priority', 'state', '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'] widgets = { 'description': forms.Textarea(attrs={'rows': 5}), } 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 = ['type', 'comment'] def __init__(self, *args, **kwargs): try: acc = kwargs.pop('acc') except KeyError: disable_type = True else: disable_type = not acc.modify_comment super(forms.ModelForm, self).__init__(*args, **kwargs) self.fields['type'].disabled = disable_type 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')]