| 1 |
# Copyright (c) 2016 Catalyst IT Ltd. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from django.utils.translation import gettext_lazy as _ |
| 16 |
|
| 17 |
from horizon import forms |
| 18 |
from horizon import messages |
| 19 |
|
| 20 |
import json |
| 21 |
|
| 22 |
from adjutant_ui.api import adjutant |
| 23 |
|
| 24 |
|
| 25 |
class UpdateTaskForm(forms.SelfHandlingForm): |
| 26 |
task_id = forms.CharField(widget=forms.HiddenInput()) |
| 27 |
task_type = forms.CharField(widget=forms.TextInput( |
| 28 |
attrs={'readonly': True})) |
| 29 |
task_data = forms.CharField(widget=forms.Textarea) |
| 30 |
|
| 31 |
def clean_task_data(self): |
| 32 |
taskdata = self.cleaned_data['task_data'] |
| 33 |
try: |
| 34 |
json.loads(taskdata) |
| 35 |
except ValueError: |
| 36 |
raise forms.ValidationError( |
| 37 |
"Invalid non-JSON data in Task Data field") |
| 38 |
|
| 39 |
return taskdata |
| 40 |
|
| 41 |
def handle(self, request, data): |
| 42 |
task_id = self.cleaned_data.pop('task_id') |
| 43 |
try: |
| 44 |
response = adjutant.task_update( |
| 45 |
request, task_id, data['task_data']) |
| 46 |
if response.status_code in [200, 202]: |
| 47 |
messages.success(request, _('Updated task successfully.')) |
| 48 |
elif response.status_code == 400: |
| 49 |
messages.error(request, _(response.text)) |
| 50 |
else: |
| 51 |
messages.error(request, _('Failed to update task.')) |
| 52 |
return True |
| 53 |
except Exception: |
| 54 |
messages.error(request, _('Failed to update task.')) |
| 55 |
return False |