forms.py 3.85 KB
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