1 ##############################################################################
2 # Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
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 ##############################################################################
11 import django.forms as forms
12 from django.forms import widgets
13 from django.utils.safestring import mark_safe
14 from django.template.loader import render_to_string
15 from django.forms.widgets import NumberInput
17 from account.models import Lab
18 from account.models import UserProfile
19 from resource_inventory.models import (
20 GenericResourceBundle,
29 class SearchableSelectMultipleWidget(widgets.SelectMultiple):
30 template_name = 'dashboard/searchable_select_multiple.html'
32 def __init__(self, attrs=None):
33 self.items = attrs['set']
34 self.show_from_noentry = attrs['show_from_noentry']
35 self.show_x_results = attrs['show_x_results']
36 self.results_scrollable = attrs['scrollable']
37 self.selectable_limit = attrs['selectable_limit']
38 self.placeholder = attrs['placeholder']
39 self.name = attrs['name']
40 self.initial = attrs.get("initial", "")
41 self.default_entry = attrs.get("default_entry", "")
42 self.edit = attrs.get("edit", False)
43 self.wf_type = attrs.get("wf_type")
45 super(SearchableSelectMultipleWidget, self).__init__(attrs)
47 def render(self, name, value, attrs=None, renderer=None):
49 context = self.get_context(attrs)
50 return mark_safe(render_to_string(self.template_name, context))
52 def get_context(self, attrs):
56 'show_from_noentry': self.show_from_noentry,
57 'show_x_results': self.show_x_results,
58 'results_scrollable': self.results_scrollable,
59 'selectable_limit': self.selectable_limit,
60 'placeholder': self.placeholder,
61 'initial': self.initial,
62 'default_entry': self.default_entry,
64 'wf_type': self.wf_type
68 class ResourceSelectorForm(forms.Form):
70 def __init__(self, data=None, **kwargs):
74 if "chosen_resource" in kwargs:
75 chosen_resource = kwargs.pop("chosen_resource")
76 if "bundle" in kwargs:
77 bundle = kwargs.pop("bundle")
79 edit = kwargs.pop("edit")
80 super(ResourceSelectorForm, self).__init__(data=data, **kwargs)
81 queryset = GenericResourceBundle.objects.select_related("owner").all()
82 if data and 'user' in data:
83 queryset = queryset.filter(owner=data['user'])
85 attrs = self.build_search_widget_attrs(chosen_resource, bundle, edit, queryset)
87 self.fields['generic_resource_bundle'] = forms.CharField(
88 widget=SearchableSelectMultipleWidget(attrs=attrs)
91 def build_search_widget_attrs(self, chosen_resource, bundle, edit, queryset):
95 displayable['small_name'] = res.name
97 displayable['expanded_name'] = res.owner.username
99 displayable['expanded_name'] = ""
100 displayable['string'] = res.description
101 displayable['id'] = res.id
102 resources[res.id] = displayable
106 displayable['small_name'] = bundle.name
107 displayable['expanded_name'] = "Current bundle"
108 displayable['string'] = bundle.description
109 displayable['id'] = "repo bundle"
110 resources["repo bundle"] = displayable
113 'show_from_noentry': "true",
114 'show_x_results': -1,
115 'scrollable': "true",
116 'selectable_limit': 1,
117 'name': "generic_resource_bundle",
118 'placeholder': "resource",
119 'initial': chosen_resource,
126 class SWConfigSelectorForm(forms.Form):
128 def __init__(self, *args, **kwargs):
133 if "chosen_software" in kwargs:
134 chosen_software = kwargs.pop("chosen_software")
136 if "bundle" in kwargs:
137 bundle = kwargs.pop("bundle")
139 edit = kwargs.pop("edit")
140 if "resource" in kwargs:
141 resource = kwargs.pop("resource")
142 super(SWConfigSelectorForm, self).__init__(*args, **kwargs)
143 attrs = self.build_search_widget_attrs(chosen_software, bundle, edit, resource)
144 self.fields['software_bundle'] = forms.CharField(
145 widget=SearchableSelectMultipleWidget(attrs=attrs)
148 def build_search_widget_attrs(self, chosen, bundle, edit, resource):
150 queryset = ConfigBundle.objects.select_related('owner').all()
152 queryset = queryset.filter(bundle=resource)
154 for config in queryset:
156 displayable['small_name'] = config.name
157 displayable['expanded_name'] = config.owner.username
158 displayable['string'] = config.description
159 displayable['id'] = config.id
160 configs[config.id] = displayable
164 displayable['small_name'] = bundle.name
165 displayable['expanded_name'] = "Current configuration"
166 displayable['string'] = bundle.description
167 displayable['id'] = "repo bundle"
168 configs['repo bundle'] = displayable
172 'show_from_noentry': "true",
173 'show_x_results': -1,
174 'scrollable': "true",
175 'selectable_limit': 1,
176 'name': "software_bundle",
177 'placeholder': "config",
185 class BookingMetaForm(forms.Form):
187 length = forms.IntegerField(
197 purpose = forms.CharField(max_length=1000)
198 project = forms.CharField(max_length=400)
199 info_file = forms.CharField(max_length=1000, required=False)
201 def __init__(self, data=None, *args, **kwargs):
203 if "default_user" in kwargs:
204 default_user = kwargs.pop("default_user")
207 self.default_user = default_user
208 if "chosen_users" in kwargs:
209 chosen_users = kwargs.pop("chosen_users")
210 elif data and "users" in data:
211 chosen_users = data.getlist("users")
215 super(BookingMetaForm, self).__init__(data=data, **kwargs)
217 self.fields['users'] = forms.CharField(
218 widget=SearchableSelectMultipleWidget(
219 attrs=self.build_search_widget_attrs(chosen_users, default_user=default_user)
224 def build_user_list(self):
226 returns a mapping of UserProfile ids to displayable objects expected by
227 searchable multiple select widget
231 d_qset = UserProfile.objects.select_related('user').all().exclude(user__username=self.default_user)
232 for userprofile in d_qset:
234 'id': userprofile.user.id,
235 'expanded_name': userprofile.full_name,
236 'small_name': userprofile.user.username,
237 'string': userprofile.email_addr
240 users[userprofile.user.id] = user
246 def build_search_widget_attrs(self, chosen_users, default_user="you"):
249 'set': self.build_user_list(),
250 'show_from_noentry': "false",
251 'show_x_results': 10,
252 'scrollable': "false",
253 'selectable_limit': -1,
255 'placeholder': "username",
256 'initial': chosen_users,
262 class MultipleSelectFilterWidget(forms.Widget):
263 def __init__(self, attrs=None):
264 super(MultipleSelectFilterWidget, self).__init__(attrs)
266 self.template_name = "dashboard/multiple_select_filter_widget.html"
268 def render(self, name, value, attrs=None, renderer=None):
270 self.context = self.get_context(name, value, attrs)
271 html = render_to_string(self.template_name, context=self.context)
272 return mark_safe(html)
274 def get_context(self, name, value, attrs):
278 class MultipleSelectFilterField(forms.Field):
280 def __init__(self, required=True, widget=None, label=None, initial=None,
281 help_text='', error_messages=None, show_hidden_initial=False,
282 validators=(), localize=False, disabled=False, label_suffix=None):
283 """from the documentation:
284 # required -- Boolean that specifies whether the field is required.
286 # widget -- A Widget class, or instance of a Widget class, that should
287 # be used for this Field when displaying it. Each Field has a
288 # default Widget that it'll use if you don't specify this. In
289 # most cases, the default widget is TextInput.
290 # label -- A verbose name for this field, for use in displaying this
291 # field in a form. By default, Django will use a "pretty"
292 # version of the form field name, if the Field is part of a
294 # initial -- A value to use in this Field's initial display. This value
295 # is *not* used as a fallback if data isn't given.
296 # help_text -- An optional string to use as "help text" for this Field.
297 # error_messages -- An optional dictionary to override the default
298 # messages that the field will raise.
299 # show_hidden_initial -- Boolean that specifies if it is needed to render a
300 # hidden widget with initial value after widget.
301 # validators -- List of additional validators to use
302 # localize -- Boolean that specifies if the field should be localized.
303 # disabled -- Boolean that specifies whether the field is disabled, that
304 # is its widget is shown in the form but not editable.
305 # label_suffix -- Suffix to be added to the label. Overrides
306 # form's label_suffix.
308 # this is bad, but django forms are annoying
310 if self.widget is None:
311 self.widget = MultipleSelectFilterWidget()
312 super(MultipleSelectFilterField, self).__init__(
318 error_messages=error_messages,
319 show_hidden_initial=show_hidden_initial,
320 validators=validators,
323 label_suffix=label_suffix
328 This method will raise a django.forms.ValidationError or return clean data
337 Gets all labs and thier host profiles and returns a serialized version the form can understand.
338 Should be rewritten with a related query to make it faster
339 Should be moved outside of global scope
345 for lab in Lab.objects.all():
347 slab['id'] = "lab_" + str(lab.lab_user.id)
348 slab['name'] = lab.name
349 slab['description'] = lab.description
351 slab['selectable'] = 1
354 items[slab['id']] = slab
355 mapping[slab['id']] = []
356 labs[slab['id']] = slab
357 for host in lab.hostprofiles.all():
359 shost['forms'] = [{"name": "host_name", "type": "text", "placeholder": "hostname"}]
360 shost['id'] = "host_" + str(host.id)
361 shost['name'] = host.name
362 shost['description'] = host.description
363 shost['selected'] = 0
364 shost['selectable'] = 1
366 shost['multiple'] = 1
367 items[shost['id']] = shost
368 mapping[slab['id']].append(shost['id'])
369 if shost['id'] not in mapping:
370 mapping[shost['id']] = []
371 mapping[shost['id']].append(slab['id'])
372 hosts[shost['id']] = shost
374 filter_objects = [("labs", labs.values()), ("hosts", hosts.values())]
377 'filter_objects': filter_objects,
384 class HardwareDefinitionForm(forms.Form):
386 def __init__(self, *args, **kwargs):
387 selection_data = kwargs.pop("selection_data", False)
388 super(HardwareDefinitionForm, self).__init__(*args, **kwargs)
389 attrs = FormUtils.getLabData()
390 attrs['selection_data'] = selection_data
391 self.fields['filter_field'] = MultipleSelectFilterField(
392 widget=MultipleSelectFilterWidget(
398 class PodDefinitionForm(forms.Form):
401 xml = forms.CharField()
404 class ResourceMetaForm(forms.Form):
406 bundle_name = forms.CharField(label="POD Name")
407 bundle_description = forms.CharField(label="POD Description", widget=forms.Textarea)
410 class GenericHostMetaForm(forms.Form):
412 host_profile = forms.CharField(label="Host Type", disabled=True, required=False)
413 host_name = forms.CharField(label="Host Name")
416 class NetworkDefinitionForm(forms.Form):
417 def __init__(self, *args, **kwargs):
418 super(NetworkDefinitionForm, self).__init__(**kwargs)
421 class NetworkConfigurationForm(forms.Form):
422 def __init__(self, *args, **kwargs):
423 super(NetworkConfigurationForm).__init__(**kwargs)
426 class HostSoftwareDefinitionForm(forms.Form):
427 fields = ["host_name", "role", "image"]
429 host_name = forms.CharField(max_length=200, disabled=True, required=False)
430 role = forms.ModelChoiceField(queryset=OPNFVRole.objects.all())
431 image = forms.ModelChoiceField(queryset=Image.objects.all())
434 class SoftwareConfigurationForm(forms.Form):
436 name = forms.CharField(max_length=200)
437 description = forms.CharField(widget=forms.Textarea)
438 opnfv = forms.BooleanField(disabled=True, required=False)
439 installer = forms.ModelChoiceField(queryset=Installer.objects.all(), disabled=True, required=False)
440 scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), disabled=True, required=False)
443 class WorkflowSelectionForm(forms.Form):
444 fields = ['workflow']
446 empty_permitted = False
448 workflow = forms.ChoiceField(
451 (1, 'Resource Bundle'),
452 (2, 'Software Configuration')
454 label="Choose Workflow",
460 class SnapshotHostSelectForm(forms.Form):
461 host = forms.CharField()
464 class SnapshotMetaForm(forms.Form):
465 name = forms.CharField()
466 description = forms.CharField()
469 class ConfirmationForm(forms.Form):
472 confirm = forms.ChoiceField(