Blame view

src/routine_manager/forms.py 4.24 KB
986d5dec   haribo   Date: 14/06/2016
1
from django import forms
ddf59dd4   haribo   Remaniement :
2
from common.models import *
bbf6e698   haribo   Date: 14/06/2016
3
from routine_manager.validators import check_album_validity
986d5dec   haribo   Date: 14/06/2016
4
5

class RequestForm(forms.ModelForm):
bbf6e698   haribo   Date: 14/06/2016
6
7
8
    """
        Form for Request edition
    """
986d5dec   haribo   Date: 14/06/2016
9

986d5dec   haribo   Date: 14/06/2016
10
11
12
13
14
15
16
17
18
19
20
    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
986d5dec   haribo   Date: 14/06/2016
21
22
23


class SequenceForm(forms.ModelForm):
bbf6e698   haribo   Date: 14/06/2016
24
25
26
    """
        Form for Sequence edition
    """
986d5dec   haribo   Date: 14/06/2016
27
28
29
30
31
32
33
34

    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)
e5189e4f   haribo   Date: 20/06/2016
35
    duration = forms.IntegerField(label="duration")
986d5dec   haribo   Date: 14/06/2016
36
37
38

    class Meta:
        model = Sequence
e5189e4f   haribo   Date: 20/06/2016
39
        fields = ("name", "target_coords", "jd1", "jd2")
986d5dec   haribo   Date: 14/06/2016
40
41
42
43
44
45
46
47
48
49
50
51

    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

e5189e4f   haribo   Date: 20/06/2016
52
53
54
55
        seq = kwargs["instance"]
        if seq.duration:
            self.fields["duration"].initial = seq.duration * 86400

986d5dec   haribo   Date: 14/06/2016
56
57
58
59
60
61
62
63
    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;
aed99094   haribo   Debug routine MGR...
64
65
        if self.cleaned_data["duration"]:
            seq.duration = self.cleaned_data["duration"] / 86400
986d5dec   haribo   Date: 14/06/2016
66
67
68
69
70
        seq.save()
        return seq


class AlbumForm(forms.ModelForm):
bbf6e698   haribo   Date: 14/06/2016
71
72
73
    """
        Form for Album edition
    """
986d5dec   haribo   Date: 14/06/2016
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    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):
bbf6e698   haribo   Date: 14/06/2016
89
90
91
    """
        Form for Plan edition
    """
986d5dec   haribo   Date: 14/06/2016
92

aed99094   haribo   Debug routine MGR...
93
    duration = forms.IntegerField(label="Duration")
e5189e4f   haribo   Date: 20/06/2016
94

986d5dec   haribo   Date: 14/06/2016
95
96
    class Meta:
        model = Plan
e5189e4f   haribo   Date: 20/06/2016
97
        fields = ("name", "filter", "nb_images")
986d5dec   haribo   Date: 14/06/2016
98
99
100

    def __init__(self, *args, readonly=False, **kwargs):
        super(PlanForm, self).__init__(*args, **kwargs)
dd3121ae   haribo   Minor debugs on r...
101
102
103
104
        plan = kwargs["instance"]

        self.fields["filter"].queryset = plan.album.detector.filter_wheel.filters

986d5dec   haribo   Date: 14/06/2016
105
106
107
108
109
110
        for field in self.fields.values():
            if readonly == True:
                field.widget.attrs['readonly'] = True
            field.widget.attrs['class'] = 'form-control'
            field.required = True

e5189e4f   haribo   Date: 20/06/2016
111
112
113
        if plan.duration:
            self.fields["duration"].initial = plan.duration * 86400

986d5dec   haribo   Date: 14/06/2016
114
115
116
117
118
    def clean_duration(self):
        '''
            Checks if duration is at least of 1s
        '''

aed99094   haribo   Debug routine MGR...
119
        duration = int(self.cleaned_data.get("duration"))
986d5dec   haribo   Date: 14/06/2016
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        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
e5189e4f   haribo   Date: 20/06/2016
137
        plan.duration = self.cleaned_data["duration"] / 86400
986d5dec   haribo   Date: 14/06/2016
138
139
140
        plan.save()
        check_album_validity(plan.album)
        return plan