"Fossies" - the Fresh Open Source Software Archive

Member "horizon-14.0.4/openstack_dashboard/dashboards/admin/snapshots/forms.py" (22 Oct 2019, 2853 Bytes) of package /linux/misc/openstack/horizon-14.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #      http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 
   14 from django.urls import reverse
   15 from django.utils.translation import ugettext_lazy as _
   16 
   17 from horizon import exceptions
   18 from horizon import forms
   19 from horizon import messages
   20 
   21 from openstack_dashboard.api import cinder
   22 from openstack_dashboard.dashboards.project.volumes.tables \
   23     import VolumesTableBase as volumes_table
   24 
   25 # This set of states was pulled from cinder's snapshot_actions.py
   26 SETTABLE_STATUSES = (
   27     'available', 'creating', 'deleting', 'error', 'error_deleting'
   28 )
   29 STATUS_CHOICES = tuple(
   30     status for status in volumes_table.STATUS_DISPLAY_CHOICES
   31     if status[0] in SETTABLE_STATUSES
   32 )
   33 
   34 
   35 def populate_status_choices(current_status, status_choices):
   36     status_choices = [status for status in status_choices
   37                       if status[0] != current_status]
   38     status_choices.insert(0, ("", _("Select a new status")))
   39 
   40     return status_choices
   41 
   42 
   43 class UpdateStatus(forms.SelfHandlingForm):
   44     status = forms.ThemableChoiceField(label=_("Status"))
   45 
   46     def __init__(self, request, *args, **kwargs):
   47         # Obtain the localized status to use as initial value, has to be done
   48         # before super() otherwise the initial value will get overwritten back
   49         # to the raw value
   50         current_status = kwargs['initial']['status']
   51         choices = dict(STATUS_CHOICES)
   52         kwargs['initial']['status'] = choices[current_status]
   53 
   54         super(UpdateStatus, self).__init__(request, *args, **kwargs)
   55 
   56         self.fields['status'].choices = populate_status_choices(
   57             current_status, STATUS_CHOICES)
   58 
   59     def handle(self, request, data):
   60         try:
   61             cinder.volume_snapshot_reset_state(request,
   62                                                self.initial['snapshot_id'],
   63                                                data['status'])
   64 
   65             choices = dict(STATUS_CHOICES)
   66             choice = choices[data['status']]
   67             messages.success(request, _('Successfully updated volume snapshot'
   68                                         ' status: "%s".') % choice)
   69             return True
   70         except Exception:
   71             redirect = reverse("horizon:admin:snapshots:index")
   72             exceptions.handle(request,
   73                               _('Unable to update volume snapshot status.'),
   74                               redirect=redirect)