forms.py
4.19 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
131
132
133
134
135
136
137
138
139
from django import forms
from pyrosapp.models import *
from routine_manager.validators import check_album_validity
class RequestForm(forms.ModelForm):
"""
Form for Request edition
"""
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
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)
duration = forms.IntegerField(label="duration")
class Meta:
model = Sequence
fields = ("name", "target_coords", "jd1", "jd2")
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
seq = kwargs["instance"]
if seq.duration:
self.fields["duration"].initial = seq.duration * 86400
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.duration = self.cleaned_data["duration"] / 86400
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
"""
duration = forms.IntegerField(label="duration")
class Meta:
model = Plan
fields = ("name", "filter", "nb_images")
def __init__(self, *args, readonly=False, **kwargs):
super(PlanForm, self).__init__(*args, **kwargs)
plan = kwargs["instance"]
self.fields["filter"].queryset = plan.album.detector.filter_wheel.filters
for field in self.fields.values():
if readonly == True:
field.widget.attrs['readonly'] = True
field.widget.attrs['class'] = 'form-control'
field.required = True
if plan.duration:
self.fields["duration"].initial = plan.duration * 86400
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.duration = self.cleaned_data["duration"] / 86400
plan.save()
check_album_validity(plan.album)
return plan