from django.conf import settings from django import forms from common.models import * from routine_manager.validators import check_album_validity import logging log = logging.getLogger("routine_manager-views") 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) if (settings.DEBUG): log.info("From __init__ of RequestForm") 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) if (settings.DEBUG): log.info("From __init__ of SequenceForm") 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 (settings.DEBUG): log.info("From save function of SequenceForm") 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; if self.cleaned_data["duration"]: 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) if (settings.DEBUG): log.info("From __init__ of AlbumForm") 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) if (settings.DEBUG): log.info("From __init__ of PlanForm") 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 = int(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() if (settings.DEBUG): log.info("From save of PlanForm") plan.complete = True plan.duration = self.cleaned_data["duration"] / 86400 plan.save() check_album_validity(plan.album) return plan