1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Copyright 2011 Nebula, Inc.
9
# Licensed under the Apache License, Version 2.0 (the "License"); you may
10
# not use this file except in compliance with the License. You may obtain
11
# a copy of the License at
13
# http://www.apache.org/licenses/LICENSE-2.0
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
# License for the specific language governing permissions and limitations
23
from django import shortcuts
24
from django.contrib import messages
25
from django.utils.translation import ugettext as _
26
from openstackx.api import exceptions as api_exceptions
28
from horizon import api
29
from horizon import forms
32
LOG = logging.getLogger(__name__)
35
class UserForm(forms.Form):
36
def __init__(self, *args, **kwargs):
37
tenant_list = kwargs.pop('tenant_list', None)
38
super(UserForm, self).__init__(*args, **kwargs)
39
self.fields['tenant_id'].choices = [[tenant.id, tenant.name]
40
for tenant in tenant_list]
42
name = forms.CharField(label=_("Name"))
43
email = forms.CharField(label=_("Email"))
44
password = forms.CharField(label=_("Password"),
45
widget=forms.PasswordInput(render_value=False),
47
tenant_id = forms.ChoiceField(label=_("Primary Tenant"))
50
class UserUpdateForm(forms.Form):
51
def __init__(self, *args, **kwargs):
52
tenant_list = kwargs.pop('tenant_list', None)
53
super(UserUpdateForm, self).__init__(*args, **kwargs)
54
self.fields['tenant_id'].choices = [[tenant.id, tenant.name]
55
for tenant in tenant_list]
57
id = forms.CharField(label=_("ID"),
58
widget=forms.TextInput(attrs={'readonly': 'readonly'}))
59
# FIXME: keystone doesn't return the username from a get API call.
60
#name = forms.CharField(label=_("Name"))
61
email = forms.CharField(label=_("Email"))
62
password = forms.CharField(label=_("Password"),
63
widget=forms.PasswordInput(render_value=False),
65
tenant_id = forms.ChoiceField(label=_("Primary Tenant"))
68
class UserDeleteForm(forms.SelfHandlingForm):
69
user = forms.CharField(required=True)
71
def handle(self, request, data):
72
user_id = data['user']
73
LOG.info('Deleting user with id "%s"' % user_id)
74
api.user_delete(request, user_id)
75
messages.info(request, _('%(user)s was successfully deleted.')
77
return shortcuts.redirect(request.build_absolute_uri())
80
class UserEnableDisableForm(forms.SelfHandlingForm):
81
id = forms.CharField(label=_("ID (username)"), widget=forms.HiddenInput())
82
enabled = forms.ChoiceField(label=_("enabled"), widget=forms.HiddenInput(),
84
for c in ("disable", "enable")])
86
def handle(self, request, data):
88
enabled = data['enabled'] == "enable"
91
api.user_update_enabled(request, user_id, enabled)
92
messages.info(request,
93
_("User %(user)s %(state)s") %
95
"state": "enabled" if enabled else "disabled"})
96
except api_exceptions.ApiException:
97
messages.error(request,
98
_("Unable to %(state)s user %(user)s") %
99
{"state": "enable" if enabled else "disable",
102
return shortcuts.redirect(request.build_absolute_uri())