Blame view

src/routine_manager/forms.py 4.81 KB
b5e9fde1   Jeremy   Update django ver...
1
from django.conf import settings
986d5dec   haribo   Date: 14/06/2016
2
from django import forms
ddf59dd4   haribo   Remaniement :
3
from common.models import *
bbf6e698   haribo   Date: 14/06/2016
4
from routine_manager.validators import check_album_validity
986d5dec   haribo   Date: 14/06/2016
5

b5e9fde1   Jeremy   Update django ver...
6
7
8
import logging
log = logging.getLogger("routine_manager-views")

986d5dec   haribo   Date: 14/06/2016
9
class RequestForm(forms.ModelForm):
bbf6e698   haribo   Date: 14/06/2016
10
11
12
    """
        Form for Request edition
    """
986d5dec   haribo   Date: 14/06/2016
13

986d5dec   haribo   Date: 14/06/2016
14
15
16
17
18
19
    class Meta:
        model = Request
        fields = ("name", "scientific_program", "target_type")

    def __init__(self, *args, readonly=False, **kwargs):
        super(RequestForm, self).__init__(*args, **kwargs)
b5e9fde1   Jeremy   Update django ver...
20
21
        if (settings.DEBUG):
            log.info("From __init__ of RequestForm")
986d5dec   haribo   Date: 14/06/2016
22
23
24
25
26
        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
27
28
29


class SequenceForm(forms.ModelForm):
bbf6e698   haribo   Date: 14/06/2016
30
31
32
    """
        Form for Sequence edition
    """
986d5dec   haribo   Date: 14/06/2016
33
34
35
36
37
38
39
40

    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
41
    duration = forms.IntegerField(label="duration")
986d5dec   haribo   Date: 14/06/2016
42
43
44

    class Meta:
        model = Sequence
e5189e4f   haribo   Date: 20/06/2016
45
        fields = ("name", "target_coords", "jd1", "jd2")
986d5dec   haribo   Date: 14/06/2016
46
47
48

    def __init__(self, *args, readonly=False, **kwargs):
        super(SequenceForm, self).__init__(*args, **kwargs)
b5e9fde1   Jeremy   Update django ver...
49
50
        if (settings.DEBUG):
            log.info("From __init__ of SequenceForm")
986d5dec   haribo   Date: 14/06/2016
51
52
53
54
55
56
57
58
59
        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
60
61
62
63
        seq = kwargs["instance"]
        if seq.duration:
            self.fields["duration"].initial = seq.duration * 86400

986d5dec   haribo   Date: 14/06/2016
64
65
    def save(self):
        seq = super(SequenceForm, self).save()
b5e9fde1   Jeremy   Update django ver...
66
67
        if (settings.DEBUG):
            log.info("From save function of SequenceForm")
986d5dec   haribo   Date: 14/06/2016
68
69
70
71
72
73
        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...
74
75
        if self.cleaned_data["duration"]:
            seq.duration = self.cleaned_data["duration"] / 86400
986d5dec   haribo   Date: 14/06/2016
76
77
78
79
80
        seq.save()
        return seq


class AlbumForm(forms.ModelForm):
bbf6e698   haribo   Date: 14/06/2016
81
82
83
    """
        Form for Album edition
    """
986d5dec   haribo   Date: 14/06/2016
84
85
86
87
88
89
90

    class Meta:
        model = Album
        fields = ("name", "detector")

    def __init__(self, *args, readonly=False, **kwargs):
        super(AlbumForm, self).__init__(*args, **kwargs)
b5e9fde1   Jeremy   Update django ver...
91
92
        if (settings.DEBUG):
            log.info("From __init__ of AlbumForm")
986d5dec   haribo   Date: 14/06/2016
93
94
95
96
97
98
99
100
        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
101
102
103
    """
        Form for Plan edition
    """
986d5dec   haribo   Date: 14/06/2016
104

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

986d5dec   haribo   Date: 14/06/2016
107
108
    class Meta:
        model = Plan
e5189e4f   haribo   Date: 20/06/2016
109
        fields = ("name", "filter", "nb_images")
986d5dec   haribo   Date: 14/06/2016
110
111
112

    def __init__(self, *args, readonly=False, **kwargs):
        super(PlanForm, self).__init__(*args, **kwargs)
b5e9fde1   Jeremy   Update django ver...
113
114
        if (settings.DEBUG):
            log.info("From __init__ of PlanForm")
dd3121ae   haribo   Minor debugs on r...
115
116
117
118
        plan = kwargs["instance"]

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

986d5dec   haribo   Date: 14/06/2016
119
120
121
122
123
124
        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
125
126
127
        if plan.duration:
            self.fields["duration"].initial = plan.duration * 86400

986d5dec   haribo   Date: 14/06/2016
128
129
130
131
132
    def clean_duration(self):
        '''
            Checks if duration is at least of 1s
        '''

aed99094   haribo   Debug routine MGR...
133
        duration = int(self.cleaned_data.get("duration"))
986d5dec   haribo   Date: 14/06/2016
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        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()
b5e9fde1   Jeremy   Update django ver...
150
151
        if (settings.DEBUG):
            log.info("From save of PlanForm")
986d5dec   haribo   Date: 14/06/2016
152
        plan.complete = True
e5189e4f   haribo   Date: 20/06/2016
153
        plan.duration = self.cleaned_data["duration"] / 86400
986d5dec   haribo   Date: 14/06/2016
154
155
156
        plan.save()
        check_album_validity(plan.album)
        return plan