forms.py
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# coding=utf-8
from django import forms
from datetime import datetime
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.forms.models import inlineformset_factory
from subjects.models import Tag
from pendencies.forms import PendenciesLimitedForm
from pendencies.models import Pendencies
from .models import Goals, GoalItem, MyGoals
class GoalsForm(forms.ModelForm):
subject = None
topic = None
control_subject = forms.CharField(widget = forms.HiddenInput())
def __init__(self, *args, **kwargs):
super(GoalsForm, self).__init__(*args, **kwargs)
self.subject = kwargs['initial'].get('subject', None)
self.topic = kwargs['initial'].get('topic', None)
if self.instance.id:
self.subject = self.instance.topic.subject
self.topic = self.instance.topic
self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
self.initial['control_subject'] = self.subject.id
tags = forms.CharField(label = _('Tags'), required = False)
class Meta:
model = Goals
fields = ['name', 'presentation', 'limit_submission_date', 'brief_description', 'show_window', 'visible']
labels = {
'name': _('Name'),
}
widgets = {
'presentation': forms.Textarea,
'brief_description': forms.Textarea,
}
def clean(self):
cleaned_data = super(GoalsForm, self).clean()
limit_submission_date = cleaned_data.get('limit_submission_date', None)
if self.topic:
if self.instance.id:
exist = self.topic.resource_topic.filter(goals__isnull = False).exclude(id = self.instance.id).exists()
else:
exist = self.topic.resource_topic.filter(goals__isnull = False).exists()
if exist:
self.add_error('name', _('There already is another resource with the goals specification for the Topic %s')%(str(self.topic)))
if limit_submission_date:
if not limit_submission_date == ValueError:
if not self.instance.id and limit_submission_date.date() < datetime.today().date():
self.add_error('limit_submission_date', _("This input should be filled with a date equal or after today's date."))
if limit_submission_date.date() < self.subject.init_date:
self.add_error('limit_submission_date', _('This input should be filled with a date equal or after the subject begin date.'))
if limit_submission_date.date() > self.subject.end_date:
self.add_error('limit_submission_date', _('This input should be filled with a date equal or after the subject end date.'))
return cleaned_data
def save(self, commit = True):
super(GoalsForm, self).save(commit = True)
self.instance.save()
previous_tags = self.instance.tags.all()
tags = self.cleaned_data['tags'].split(",")
#Excluding unwanted tags
for prev in previous_tags:
if not prev.name in tags:
self.instance.tags.remove(prev)
for tag in tags:
tag = tag.strip()
exist = Tag.objects.filter(name = tag).exists()
if exist:
new_tag = Tag.objects.get(name = tag)
else:
new_tag = Tag.objects.create(name = tag)
if not new_tag in self.instance.tags.all():
self.instance.tags.add(new_tag)
return self.instance
class GoalItemForm(forms.ModelForm):
class Meta:
model = GoalItem
fields = ['description', 'ref_value']
def clean(self):
cleaned_data = super(GoalItemForm, self).clean()
description = cleaned_data.get('description', None)
ref_value = cleaned_data.get('ref_value', None)
if ref_value and ref_value != "0":
if not description:
self.add_error('description', _('This field is required.'))
return cleaned_data
class MyGoalsForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyGoalsForm, self).__init__(*args, **kwargs)
self.fields['item'].widget = forms.HiddenInput()
class Meta:
model = MyGoals
fields = ['value', 'item']
InlinePendenciesFormset = inlineformset_factory(Goals, Pendencies, form = PendenciesLimitedForm, extra = 1, max_num = 3, validate_max = True, can_delete = True)
InlineGoalItemFormset = inlineformset_factory(Goals, GoalItem, form = GoalItemForm, extra = 1, can_delete = True)