forms.py
3.85 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
from django import forms
from pyrosapp.models import *
from routine_manager.validators import check_album_validity
class RequestForm(forms.ModelForm):
"""
Form for Request edition
"""
time_start = forms.CharField(label="Time start", widget=forms.TextInput)
class Meta:
model = Request
fields = ("name", "scientific_program", "target_type")
def __init__(self, *args, readonly=False, **kwargs):
super(RequestForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
if readonly == True:
field.widget.attrs['readonly'] = True
field.widget.attrs['class'] = 'form-control'
field.required = True
self.fields['time_start'].widget.attrs['readonly'] = True
self.fields['time_start'].required = False
class SequenceForm(forms.ModelForm):
"""
Form for Sequence edition
"""
START_EXPO_PREF = (
('IM', 'IMMEDIATE'),
('BE', 'BEST_ELEVATION'),
('MD', 'BETWEEN_JD1_JD2'),
)
start_expo_pref = forms.ChoiceField(label="Start exposure prefered time", choices=START_EXPO_PREF)
class Meta:
model = Sequence
fields = ("name", "target_coords", "jd1", "jd2", "duration")
def __init__(self, *args, readonly=False, **kwargs):
super(SequenceForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
if readonly == True:
field.widget.attrs['readonly'] = True
field.widget.attrs['class'] = 'form-control'
field.required = True
self.fields['duration'].widget.attrs['readonly'] = True
self.fields['duration'].required = False
def save(self):
seq = super(SequenceForm, self).save()
if self.cleaned_data["start_expo_pref"] == "IM":
seq.t_prefered = -1;
elif self.cleaned_data["start_expo_pref"] == "BE":
seq.t_prefered = -1; # TODO : calculer avec le programme de simulation ?
elif self.cleaned_data["start_expo_pref"] == "MD":
seq.t_prefered = (seq.jd1 + seq .jd2) / 2;
seq.save()
return seq
class AlbumForm(forms.ModelForm):
"""
Form for Album edition
"""
class Meta:
model = Album
fields = ("name", "detector")
def __init__(self, *args, readonly=False, **kwargs):
super(AlbumForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
if readonly == True:
field.widget.attrs['readonly'] = True
field.widget.attrs['class'] = 'form-control'
field.required = True
class PlanForm(forms.ModelForm):
"""
Form for Plan edition
"""
class Meta:
model = Plan
fields = ("name", "filter", "duration", "nb_images")
def __init__(self, *args, readonly=False, **kwargs):
super(PlanForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
if readonly == True:
field.widget.attrs['readonly'] = True
field.widget.attrs['class'] = 'form-control'
field.required = True
def clean_duration(self):
'''
Checks if duration is at least of 1s
'''
duration = self.cleaned_data.get("duration")
if duration < 1:
raise forms.ValidationError("Duration (in seconds) must be at least of 1")
return duration
def clean_nb_images(self):
'''
Checks if the is at least 1 image
'''
nb_images = self.cleaned_data.get("nb_images")
if nb_images < 1:
raise forms.ValidationError("There must be at least 1 image")
return nb_images
def save(self):
plan = super(PlanForm, self).save()
plan.complete = True
plan.save()
check_album_validity(plan.album)
return plan