"Fossies" - the Fresh Open Source Software Archive 
Member "horizon-16.0.0/openstack_dashboard/dashboards/admin/networks/agents/forms.py" (16 Oct 2019, 3471 Bytes) of package /linux/misc/openstack/horizon-16.0.0.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 # Copyright 2014 Kylincloud
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from django.urls import reverse
16 from django.utils.translation import ugettext_lazy as _
17
18 from horizon import exceptions
19 from horizon import forms
20 from horizon import messages
21
22 from openstack_dashboard import api
23
24
25 class AddDHCPAgent(forms.SelfHandlingForm):
26 network_id = forms.CharField(widget=forms.HiddenInput())
27 network_name = forms.CharField(label=_("Network Name"),
28 widget=forms.TextInput(
29 attrs={'readonly': 'readonly'}))
30 agent = forms.ThemableChoiceField(
31 label=_("New DHCP Agent"),
32 help_text=_("Choose an DHCP Agent to attach to."))
33
34 def __init__(self, request, *args, **kwargs):
35 super(AddDHCPAgent, self).__init__(request, *args, **kwargs)
36 initial = kwargs.get('initial', {})
37 self.fields['agent'].choices = self._populate_agent_choices(request,
38 initial)
39
40 def _populate_agent_choices(self, request, initial):
41 network_id = initial.get('network_id')
42 agents = initial.get('agents')
43 try:
44 exist_agents = [agent.id for agent in
45 api.neutron.list_dhcp_agent_hosting_networks(
46 request, network_id)]
47 agent_list = [(agent.id, agent.host) for agent in agents
48 if agent.id not in exist_agents]
49 if agent_list:
50 agent_list.insert(0, ("", _("Select a new agent")))
51 else:
52 agent_list.insert(0, ("", _("No other agents available.")))
53 return agent_list
54 except Exception:
55 redirect = reverse('horizon:admin:networks:detail',
56 args=(network_id,))
57 msg = _('Unable to list dhcp agents hosting network.')
58 exceptions.handle(request, msg, redirect=redirect)
59
60 def handle(self, request, data):
61 # Get the agent name for message
62 agent_name = data['agent']
63 for choice in self.fields['agent'].choices:
64 if choice[0] == data['agent']:
65 agent_name = choice[1]
66 try:
67 api.neutron.add_network_to_dhcp_agent(request, data['agent'],
68 data['network_id'])
69 msg = _('Agent %s was successfully added.') % agent_name
70 messages.success(request, msg)
71 return True
72 except Exception:
73 redirect = reverse('horizon:admin:networks:detail',
74 args=(data['network_id'],))
75 msg = _('Failed to add agent %(agent_name)s for '
76 'network %(network)s.') \
77 % {'agent_name': agent_name,
78 'network': data['network_name']}
79 exceptions.handle(request, msg, redirect=redirect)