Merge "Remove RSA Files from Repo"
[laas.git] / src / booking / forms.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 import django.forms as forms
12
13 from booking.models import Installer, Scenario, Opsys
14 from datetime import datetime
15
16 class BookingForm(forms.Form):
17     fields = ['start', 'end', 'purpose', 'opsys', 'reset', 'installer', 'scenario']
18
19     start = forms.DateTimeField()
20     end = forms.DateTimeField()
21     reset = forms.ChoiceField(choices = ((True, 'Yes'),(False, 'No')), label="Reset System", initial='False', required=False)
22     purpose = forms.CharField(max_length=300)
23     opsys = forms.ModelChoiceField(queryset=Opsys.objects.all(), required=False)
24     opsys.label = "Operating System"
25     installer = forms.ModelChoiceField(queryset=Installer.objects.all(), required=False)
26     scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=False)
27
28 class BookingEditForm(forms.Form):
29     fields = ['start', 'end', 'purpose', 'opsys', 'reset', 'installer', 'scenario']
30
31     start = forms.DateTimeField()
32     end = forms.DateTimeField()
33     purpose = forms.CharField(max_length=300)
34     opsys = forms.ModelChoiceField(queryset=Opsys.objects.all(), required=False)
35     installer = forms.ModelChoiceField(queryset=Installer.objects.all(), required=False)
36     scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=False)
37     reset = forms.ChoiceField(choices = ((True, 'Yes'),(False, 'No')), label="Reset System", initial='False', required=True)
38
39
40     def __init__(self, *args, **kwargs ):
41         cloned_kwargs = {}
42         cloned_kwargs['purpose'] = kwargs.pop('purpose')
43         cloned_kwargs['start'] = kwargs.pop('start')
44         cloned_kwargs['end'] = kwargs.pop('end')
45         if 'installer' in kwargs:
46             cloned_kwargs['installer'] = kwargs.pop('installer')
47         if 'scenario' in kwargs:
48             cloned_kwargs['scenario'] = kwargs.pop('scenario')
49         super(BookingEditForm, self).__init__( *args, **kwargs)
50
51         self.fields['purpose'].initial = cloned_kwargs['purpose']
52         self.fields['start'].initial = cloned_kwargs['start'].strftime('%m/%d/%Y %H:%M')
53         self.fields['end'].initial = cloned_kwargs['end'].strftime('%m/%d/%Y %H:%M')
54         try:
55             self.fields['installer'].initial = cloned_kwargs['installer'].id
56         except KeyError:
57             pass
58         except AttributeError:
59             pass
60         try:
61             self.fields['scenario'].initial = cloned_kwargs['scenario'].id
62         except KeyError:
63             pass
64         except AttributeError:
65             pass