File: forms.py

package info (click to toggle)
mistral-dashboard 20.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: python: 3,383; sh: 376; makefile: 27
file content (102 lines) | stat: -rw-r--r-- 3,550 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2016 - Nokia.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.utils.translation import gettext_lazy as _

from horizon import forms

from mistraldashboard import api
from mistraldashboard.handle_errors import handle_errors


class UpdateForm(forms.SelfHandlingForm):
    action_execution_id = forms.CharField(label=_("Action Execution ID"),
                                          widget=forms.HiddenInput(),
                                          required=False)
    output_source = forms.ChoiceField(
        label=_('Output'),
        help_text=_('Content for output. '
                    'Select either file, raw content or Null value.'),
        choices=[('null', _('<null> (sends empty value)')),
                 ('file', _('File')),
                 ('raw', _('Direct Input'))],
        widget=forms.Select(
            attrs={'class': 'switchable',
                   'data-slug': 'outputsource'}
        ),
        required=False
    )
    output_upload = forms.FileField(
        label=_('Output File'),
        help_text=_('A local output to upload'),
        widget=forms.FileInput(
            attrs={'class': 'switched',
                   'data-switch-on': 'outputsource',
                   'data-outputsource-file': _('Output File')}
        ),
        required=False
    )
    output_data = forms.CharField(
        label=_('Output Data'),
        help_text=_('The raw content for output'),
        widget=forms.widgets.Textarea(
            attrs={'class': 'switched',
                   'data-switch-on': 'outputsource',
                   'data-outputsource-raw': _('Output Data'),
                   'rows': 4}
        ),
        required=False
    )

    state = forms.ChoiceField(
        label=_('State'),
        help_text=_('Select state to update'),
        choices=[('null', _('<null> (sends empty value)')),
                 ('SUCCESS', _('Success')),
                 ('ERROR', _('Error'))],
        widget=forms.Select(
            attrs={'class': 'switchable'}
        ),
        required=False

    )

    def clean(self):
        cleaned_data = super(UpdateForm, self).clean()
        cleaned_data['output'] = None

        if cleaned_data.get('output_upload'):
            files = self.request.FILES
            cleaned_data['output'] = files['output_upload'].read()
        elif cleaned_data.get('output_data'):
            cleaned_data['output'] = cleaned_data['output_data']
        elif cleaned_data.get('output_source') == 'null':
            cleaned_data['output'] = None

        del cleaned_data['output_upload']
        del cleaned_data['output_data']
        del cleaned_data['output_source']

        if cleaned_data['state'] == 'null':
            cleaned_data['state'] = None

        return cleaned_data

    @handle_errors(_("Unable to update Action Execution"), [])
    def handle(self, request, data):
        return api.action_execution_update(
            request,
            data['action_execution_id'],
            data['state'],
            data['output'],
        )