Cleanup on syspanel.users to use new CBV's.

Change-Id: I8f75f5f569f104d88e74c03410644147e77dfc53
This commit is contained in:
Gabriel Hurley 2012-01-03 16:27:19 -08:00
parent 30a60bd92b
commit a3aeda1616
8 changed files with 54 additions and 102 deletions

View File

@ -9,5 +9,5 @@
{% endblock page_header %}
{% block syspanel_main %}
{% include "syspanel/tenants/_list.html" %}
{{ table.render }}
{% endblock %}

View File

@ -2,7 +2,7 @@
{% load i18n %}
{% block form_id %}update_tenant_form{% endblock %}
{% block form_action %}{% url horizon:syspanel:users:update user_id %}{% endblock %}
{% block form_action %}{% url horizon:syspanel:users:update user.id %}{% endblock %}
{% block modal-header %}{% trans "Update User" %}{% endblock %}

View File

@ -9,5 +9,5 @@
{% endblock page_header %}
{% block syspanel_main %}
{{ table.render }}
{{ table.render }}
{% endblock %}

View File

@ -71,7 +71,8 @@ class UpdateView(forms.ModalFormView):
template_name = 'syspanel/tenants/update.html'
context_object_name = 'tenant'
def get_object(self, tenant_id):
def get_object(self, *args, **kwargs):
tenant_id = kwargs['tenant_id']
try:
return api.tenant_get(self.request, tenant_id)
except Exception as e:

View File

@ -48,7 +48,7 @@ class BaseUserForm(forms.SelfHandlingForm):
return cls(request, *args, **kwargs)
class UserForm(BaseUserForm):
class CreateUserForm(BaseUserForm):
name = forms.CharField(label=_("Name"))
email = forms.CharField(label=_("Email"))
password = forms.CharField(label=_("Password"),
@ -95,7 +95,7 @@ class UserForm(BaseUserForm):
return shortcuts.redirect('horizon:syspanel:users:index')
class UserUpdateForm(BaseUserForm):
class UpdateUserForm(BaseUserForm):
id = forms.CharField(label=_("ID"),
widget=forms.TextInput(attrs={'readonly': 'readonly'}))
# FIXME: keystone doesn't return the username from a get API call.
@ -121,40 +121,3 @@ class UserUpdateForm(BaseUserForm):
_('Updated %(attrib)s for %(user)s.') %
{"attrib": ', '.join(updated), "user": data['id']})
return shortcuts.redirect('horizon:syspanel:users:index')
class UserDeleteForm(forms.SelfHandlingForm):
user = forms.CharField(required=True)
def handle(self, request, data):
user_id = data['user']
LOG.info('Deleting user with id "%s"' % user_id)
api.user_delete(request, user_id)
messages.info(request, _('%(user)s was successfully deleted.')
% {"user": user_id})
return shortcuts.redirect(request.build_absolute_uri())
class UserEnableDisableForm(forms.SelfHandlingForm):
id = forms.CharField(label=_("ID (username)"), widget=forms.HiddenInput())
enabled = forms.ChoiceField(label=_("enabled"), widget=forms.HiddenInput(),
choices=[[c, c]
for c in ("disable", "enable")])
def handle(self, request, data):
user_id = data['id']
enabled = data['enabled'] == "enable"
try:
api.user_update_enabled(request, user_id, enabled)
messages.info(request,
_("User %(user)s %(state)s") %
{"user": user_id,
"state": "enabled" if enabled else "disabled"})
except api_exceptions.ApiException:
messages.error(request,
_("Unable to %(state)s user %(user)s") %
{"state": "enable" if enabled else "disable",
"user": user_id})
return shortcuts.redirect(request.build_absolute_uri())

View File

@ -20,7 +20,9 @@
from django.conf.urls.defaults import patterns, url
from .views import IndexView, CreateView, UpdateView
urlpatterns = patterns('horizon.dashboards.syspanel.users.views',
url(r'^$', 'index', name='index'),
url(r'^(?P<user_id>[^/]+)/update/$', 'update', name='update'),
url(r'^create/$', 'create', name='create'))
url(r'^$', IndexView.as_view(), name='index'),
url(r'^(?P<user_id>[^/]+)/update/$', UpdateView.as_view(), name='update'),
url(r'^create/$', CreateView.as_view(), name='create'))

View File

@ -27,70 +27,57 @@ from django.utils.translation import ugettext as _
from keystoneclient import exceptions as api_exceptions
from horizon import api
from horizon.dashboards.syspanel.users.forms import (UserForm, UserUpdateForm,
UserDeleteForm, UserEnableDisableForm)
from horizon.dashboards.syspanel.users.tables import UsersTable
from horizon import forms
from horizon import tables
from .forms import CreateUserForm, UpdateUserForm
from .tables import UsersTable
LOG = logging.getLogger(__name__)
@login_required
def index(request):
users = []
try:
users = api.user_list(request)
except api_exceptions.AuthorizationFailure, e:
LOG.exception("Unauthorized attempt to list users.")
messages.error(request, _('Unable to get user info: %s') % e.message)
except Exception, e:
LOG.exception('Exception while getting user list')
if not hasattr(e, 'message'):
e.message = str(e)
messages.error(request, _('Unable to get user info: %s') % e.message)
class IndexView(tables.DataTableView):
table_class = UsersTable
template_name = 'syspanel/users/index.html'
table = UsersTable(request, users)
handled = table.maybe_handle()
if handled:
return handled
context = {'table': table}
template = 'syspanel/users/index.html'
return shortcuts.render(request, template, context)
def get_data(self):
users = []
try:
users = api.user_list(self.request)
except api_exceptions.AuthorizationFailure, e:
LOG.exception("Unauthorized attempt to list users.")
messages.error(self.request,
_('Unable to get user info: %s') % e.message)
except Exception, e:
LOG.exception('Exception while getting user list')
if not hasattr(e, 'message'):
e.message = str(e)
messages.error(self.request,
_('Unable to get user info: %s') % e.message)
return users
@login_required
def update(request, user_id):
user = api.user_get(request, user_id)
form, handled = UserUpdateForm.maybe_handle(request, initial={
'id': user_id,
'tenant_id': getattr(user, 'tenantId', None),
'email': getattr(user, 'email', '')})
if handled:
return handled
class UpdateView(forms.ModalFormView):
form_class = UpdateUserForm
template_name = 'syspanel/users/update.html'
context_object_name = 'user'
context = {'form': form,
'user_id': user_id}
if request.is_ajax():
template = 'syspanel/users/_update.html'
context['hide'] = True
else:
template = 'syspanel/users/update.html'
def get_object(self, *args, **kwargs):
user_id = kwargs['user_id']
try:
return api.user_get(self.request, user_id)
except Exception as e:
LOG.exception('Error fetching user with id "%s"' % user_id)
messages.error(self.request,
_('Unable to update user: %s') % e.message)
raise http.Http404("User with id %s not found." % user_id)
return shortcuts.render(request, template, context)
def get_initial(self):
return {'id': self.object.id,
'tenant_id': getattr(self.object, 'tenantId', None),
'email': getattr(self.object, 'email', '')}
@login_required
def create(request):
form, handled = UserForm.maybe_handle(request)
if handled:
return handled
context = {'form': form}
if request.is_ajax():
template = 'syspanel/users/_create.html'
context['hide'] = True
else:
template = 'syspanel/users/create.html'
return shortcuts.render(request, template, context)
class CreateView(forms.ModalFormView):
form_class = CreateUserForm
template_name = 'syspanel/users/create.html'