pytask/taskapp/forms.py
author Nishanth Amuluru <nishanth@fossee.in>
Tue, 11 Jan 2011 17:40:14 +0530
changeset 145 4252da60a4ef
parent 143 da4c6b1cec7d
permissions -rw-r--r--
submit report works fine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
     1
from django import forms
130
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
     2
from pytask.taskapp.models import Task, WorkReport, TaskComment, TaskClaim, \
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
     3
                                  TextBook
94
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
     4
98
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
     5
class CreateTaskForm(forms.ModelForm):
94
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
     6
    class Meta:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
     7
        model = Task
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
     8
        fields = ['title', 'desc', 'tags_field', 'pynts']
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
     9
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    10
    def clean_title(self):
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    11
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    12
        data = self.cleaned_data['title'].strip()
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    13
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    14
        if not data:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    15
            raise forms.ValidationError("This field is required")
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    16
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    17
        try:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    18
            Task.objects.exclude(status="DL").get(title__iexact=data)
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    19
            raise forms.ValidationError("Another task with same title exists")
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    20
        except Task.DoesNotExist:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    21
            return data
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    22
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    23
    def clean_desc(self):
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    24
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    25
        data = self.cleaned_data['desc'].strip()
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    26
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    27
        if not data:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    28
            raise forms.ValidationError("This field is required")
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    29
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    30
        return data
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    31
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    32
class EditTaskForm(forms.ModelForm):
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    33
    class Meta:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    34
        model = Task
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    35
        fields = ['title', 'desc', 'tags_field', 'pynts']
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    36
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    37
    def clean_desc(self):
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    38
        data = self.cleaned_data['desc'].strip()
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    39
        if not data:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    40
            raise forms.ValidationError("Enter some description for the task")
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    41
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    42
        return data
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    43
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    44
    def clean_title(self):
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    45
        data = self.cleaned_data['title'].strip()
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    46
        try:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    47
            prev_task = Task.objects.exclude(status="DL").get(title__iexact=data)
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    48
            if prev_task.id != self.instance.id:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    49
                raise forms.ValidationError("Another task with same title exists")
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    50
            else:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    51
                return data
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    52
        except Task.DoesNotExist:
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    53
            return data
385702a0bbde Added forms for creating and editing a task
Nishanth Amuluru <nishanth@fossee.in>
parents:
diff changeset
    54
109
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    55
class TaskCommentForm(forms.ModelForm):
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    56
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    57
    class Meta:
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    58
        model = TaskComment
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    59
        fields = ['data']
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    60
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    61
    def clean_data(self):
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    62
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    63
        data = self.cleaned_data['data'].strip()
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    64
        if not data:
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    65
            raise forms.ValidationError("Please add some content")
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    66
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    67
        return data
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    68
114
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    69
class ClaimTaskForm(forms.ModelForm):
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    70
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    71
    class Meta:
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    72
        model = TaskClaim
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    73
        fields = ["proposal"]
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    74
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    75
    def clean_proposal(self):
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    76
        data = self.cleaned_data['proposal'].strip()
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    77
        if not data:
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    78
            raise forms.ValidationError('Enter something as a proposal')
1a88b04adb39 created form for making a claim
Nishanth Amuluru <nishanth@fossee.in>
parents: 109
diff changeset
    79
        return data
109
d291c08c62de added a form for commenting on tasks
Nishanth Amuluru <nishanth@fossee.in>
parents: 98
diff changeset
    80
143
da4c6b1cec7d add reviewer works now
Nishanth Amuluru <nishanth@fossee.in>
parents: 135
diff changeset
    81
def ChoiceForm(choices, data=None, label="choice"):
121
fc26de9c943f added form to select users
Nishanth Amuluru <nishanth@fossee.in>
parents: 114
diff changeset
    82
    """ return a form object with appropriate choices """
fc26de9c943f added form to select users
Nishanth Amuluru <nishanth@fossee.in>
parents: 114
diff changeset
    83
    
fc26de9c943f added form to select users
Nishanth Amuluru <nishanth@fossee.in>
parents: 114
diff changeset
    84
    class myform(forms.Form):
143
da4c6b1cec7d add reviewer works now
Nishanth Amuluru <nishanth@fossee.in>
parents: 135
diff changeset
    85
        choice = forms.ChoiceField(choices=choices, required=True, label=label)
121
fc26de9c943f added form to select users
Nishanth Amuluru <nishanth@fossee.in>
parents: 114
diff changeset
    86
    form = myform(data) if data else myform()
fc26de9c943f added form to select users
Nishanth Amuluru <nishanth@fossee.in>
parents: 114
diff changeset
    87
    return form
fc26de9c943f added form to select users
Nishanth Amuluru <nishanth@fossee.in>
parents: 114
diff changeset
    88
130
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
    89
class CreateTextbookForm(forms.ModelForm):
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
    90
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
    91
    class Meta:
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
    92
        model = TextBook
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
    93
        fields = ['name', 'chapters', 'tags_field']
a4fa11b2cb5c add textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 121
diff changeset
    94
135
014d812e625e edit textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 130
diff changeset
    95
class EditTextbookForm(forms.ModelForm):
014d812e625e edit textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 130
diff changeset
    96
014d812e625e edit textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 130
diff changeset
    97
    class Meta:
014d812e625e edit textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 130
diff changeset
    98
        model = TextBook
014d812e625e edit textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 130
diff changeset
    99
        fields = ['name', 'chapters', 'tags_field']
014d812e625e edit textbook works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 130
diff changeset
   100
98
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   101
def AddTaskForm(task_choices, is_plain=False):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   102
    """ if is_plain is true, it means the task has no subs/deps.
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   103
    so we also give a radio button to choose between subs and dependencies.
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   104
    else we only give choices.
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   105
    """
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   106
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   107
    class myForm(forms.Form):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   108
        if is_plain:
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   109
            type_choices = [('S','Subtasks'),('D','Dependencies')]
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   110
            type = forms.ChoiceField(type_choices, widget=forms.RadioSelect)
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   111
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   112
        task = forms.ChoiceField(choices=task_choices)
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   113
    return myForm()
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   114
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   115
def AssignPyntForm(choices, instance=None):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   116
    
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   117
    class myForm(forms.Form):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   118
        user = forms.ChoiceField(choices=choices, required=True)
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   119
        pynts = forms.IntegerField(min_value=0, required=True, help_text="Choose wisely since it cannot be undone.")
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   120
    return myForm(instance) if instance else myForm()
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   121
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   122
def RemoveUserForm(choices, instance=None):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   123
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   124
    class myForm(forms.Form):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   125
        user = forms.ChoiceField(choices=choices, required=True)
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   126
        reason = forms.CharField(min_length=1, required=True)
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   127
    return myForm(instance) if instance else myForm()
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   128
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   129
class WorkReportForm(forms.ModelForm):
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   130
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   131
    class Meta:
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   132
        model = WorkReport
145
4252da60a4ef submit report works fine
Nishanth Amuluru <nishanth@fossee.in>
parents: 143
diff changeset
   133
        fields = ['data', 'summary', 'attachment']
98
01c1e6966d86 created template for create_task
Nishanth Amuluru <nishanth@fossee.in>
parents: 94
diff changeset
   134