Implement Booking Modification Interface
[pharos-tools.git] / dashboard / 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     installer = forms.ModelChoiceField(queryset=Installer.objects.all(), required=False)
25     scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=False)
26
27 class BookingEditForm(forms.Form):
28     fields = ['start', 'end', 'purpose', 'opsys', 'reset', 'installer', 'scenario']
29
30     start = forms.DateTimeField()
31     end = forms.DateTimeField()
32     purpose = forms.CharField(max_length=300)
33     opsys = forms.ModelChoiceField(queryset=Opsys.objects.all(), required=False)
34     installer = forms.ModelChoiceField(queryset=Installer.objects.all(), required=False)
35     scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=False)
36     reset = forms.ChoiceField(choices = ((True, 'Yes'),(False, 'No')), label="Reset System", initial='False', required=True)
37
38
39     def __init__(self, *args, **kwargs ):
40         cloned_kwargs = {}
41         cloned_kwargs['purpose'] = kwargs.pop('purpose')
42         cloned_kwargs['start'] = kwargs.pop('start')
43         cloned_kwargs['end'] = kwargs.pop('end')
44         if 'installer' in kwargs:
45             cloned_kwargs['installer'] = kwargs.pop('installer')
46         if 'scenario' in kwargs:
47             cloned_kwargs['scenario'] = kwargs.pop('scenario')
48         super(BookingEditForm, self).__init__( *args, **kwargs)
49
50         self.fields['purpose'].initial = cloned_kwargs['purpose']
51         self.fields['start'].initial = cloned_kwargs['start'].strftime('%m/%d/%Y %H:%M')
52         self.fields['end'].initial = cloned_kwargs['end'].strftime('%m/%d/%Y %H:%M')
53         try:
54             self.fields['installer'].initial = cloned_kwargs['installer'].id
55         except KeyError:
56             pass
57         except AttributeError:
58             pass
59         try:
60             self.fields['scenario'].initial = cloned_kwargs['scenario'].id
61         except KeyError:
62             pass
63         except AttributeError:
64             pass