|
1 from django import forms |
|
2 from pytask.taskapp.models import Task |
|
3 |
|
4 class TaskCreateForm(forms.ModelForm): |
|
5 class Meta: |
|
6 model = Task |
|
7 fields = ['title', 'desc', 'tags_field', 'credits'] |
|
8 #publish = forms.BooleanField(required=False) |
|
9 |
|
10 def clean_title(self): |
|
11 data = self.cleaned_data['title'].strip() |
|
12 try: |
|
13 Task.objects.exclude(status="DL").get(title__iexact=data) |
|
14 raise forms.ValidationError("Another task with same title exists") |
|
15 except Task.DoesNotExist: |
|
16 return data |
|
17 |
|
18 def clean_desc(self): |
|
19 data = self.cleaned_data['desc'].strip() |
|
20 if not data: |
|
21 raise forms.ValidationError("Enter some description for the task") |
|
22 |
|
23 return data |
|
24 |
|
25 class EditTaskForm(forms.ModelForm): |
|
26 class Meta: |
|
27 model = Task |
|
28 fields = ['title', 'desc', 'tags_field', 'credits'] |
|
29 |
|
30 def clean_desc(self): |
|
31 data = self.cleaned_data['desc'].strip() |
|
32 if not data: |
|
33 raise forms.ValidationError("Enter some description for the task") |
|
34 |
|
35 return data |
|
36 |
|
37 def clean_title(self): |
|
38 data = self.cleaned_data['title'].strip() |
|
39 try: |
|
40 prev_task = Task.objects.exclude(status="DL").get(title__iexact=data) |
|
41 if prev_task.id != self.instance.id: |
|
42 raise forms.ValidationError("Another task with same title exists") |
|
43 else: |
|
44 return data |
|
45 except Task.DoesNotExist: |
|
46 return data |
|
47 |
|
48 def AddMentorForm(choices,instance=None): |
|
49 """ return a form object with appropriate choices """ |
|
50 |
|
51 class myform(forms.Form): |
|
52 mentor = forms.ChoiceField(choices=choices, required=True) |
|
53 form = myform(instance) if instance else myform() |
|
54 return form |
|
55 |
|
56 class ClaimTaskForm(forms.Form): |
|
57 message = forms.CharField(label="Proposal") |
|
58 |
|
59 def clean_message(self): |
|
60 data = self.cleaned_data['message'].strip() |
|
61 if not data: |
|
62 raise forms.ValidationError('Enter something as a proposal') |
|
63 return data |
|
64 |
|
65 |
|
66 def ChoiceForm(choices, instance=None): |
|
67 """ return a form object with appropriate choices """ |
|
68 |
|
69 class myform(forms.Form): |
|
70 choice = forms.ChoiceField(choices=choices, required=True) |
|
71 form = myform(instance) if instance else myform() |
|
72 return form |
|
73 |
|
74 def AddTaskForm(task_choices, is_plain=False): |
|
75 """ if is_plain is true, it means the task has no subs/deps. |
|
76 so we also give a radio button to choose between subs and dependencies. |
|
77 else we only give choices. |
|
78 """ |
|
79 |
|
80 class myForm(forms.Form): |
|
81 if is_plain: |
|
82 type_choices = [('S','Subtasks'),('D','Dependencies')] |
|
83 type = forms.ChoiceField(type_choices, widget=forms.RadioSelect) |
|
84 |
|
85 task = forms.ChoiceField(choices=task_choices) |
|
86 return myForm() |
|
87 |
|
88 def AssignCreditForm(choices, instance=None): |
|
89 |
|
90 class myForm(forms.Form): |
|
91 user = forms.ChoiceField(choices=choices, required=True) |
|
92 pynts = forms.IntegerField(min_value=0, required=True, help_text="Choose wisely since it cannot be undone.") |
|
93 return myForm(instance) if instance else myForm() |
|
94 |
|
95 def RemoveUserForm(choices, instance=None): |
|
96 |
|
97 class myForm(forms.Form): |
|
98 user = forms.ChoiceField(choices=choices, required=True) |
|
99 reason = forms.CharField(min_length=1, required=True) |
|
100 return myForm(instance) if instance else myForm() |
|
101 |