taskapp/forms/task.py
changeset 181 be8ad7e26176
parent 179 8d1fdb148c27
child 205 0c317f68df49
equal deleted inserted replaced
180:972745147e3f 181:be8ad7e26176
    20         if not data:
    20         if not data:
    21             raise forms.ValidationError("Enter some description for the task")
    21             raise forms.ValidationError("Enter some description for the task")
    22 
    22 
    23         return data
    23         return data
    24 
    24 
    25 def EditTaskForm(task, instance=None):
    25 class EditTaskForm(forms.ModelForm):
    26     class myForm(forms.ModelForm):
    26     class Meta:
    27         class Meta:
    27         model = Task
    28             model = Task
    28         fields = ['title', 'desc', 'tags_field', 'credits']
    29             fields = ['title', 'desc', 'tags_field', 'credits']
       
    30 
    29 
    31         def clean_desc(self):
    30     def clean_desc(self):
    32             data = self.cleaned_data['desc'].strip()
    31         data = self.cleaned_data['desc'].strip()
    33             if not data:
    32         if not data:
    34                 raise forms.ValidationError("Enter some description for the task")
    33             raise forms.ValidationError("Enter some description for the task")
    35 
    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:
    36             return data
    46             return data
    37 
       
    38         def clean_title(self):
       
    39             data = self.cleaned_data['title'].strip()
       
    40             try:
       
    41                 prev_task = Task.objects.exclude(status="DL").get(title__iexact=data)
       
    42                 if prev_task != task:
       
    43                     raise forms.ValidationError("Another task with same title exists")
       
    44                 else:
       
    45                     return data
       
    46             except:
       
    47                 return data
       
    48 
       
    49     data = {
       
    50         'title': task.title,
       
    51         'desc': task.desc,
       
    52         'tags_field': task.tags_field,
       
    53         'credits': task.credits,
       
    54     }
       
    55     return myForm(instance) if instance else myForm(data)
       
    56 
    47 
    57 def AddMentorForm(choices,instance=None):
    48 def AddMentorForm(choices,instance=None):
    58     """ return a form object with appropriate choices """
    49     """ return a form object with appropriate choices """
    59     
    50     
    60     class myform(forms.Form):
    51     class myform(forms.Form):