diff --git a/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/_list.html b/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/_list.html deleted file mode 100644 index 99f86d427..000000000 --- a/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/_list.html +++ /dev/null @@ -1 +0,0 @@ -{{ table.render }} diff --git a/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/index.html b/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/index.html index 32333e603..cd46a0ec3 100644 --- a/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/index.html +++ b/horizon/horizon/dashboards/syspanel/templates/syspanel/tenants/index.html @@ -9,5 +9,5 @@ {% endblock page_header %} {% block syspanel_main %} - {% include "syspanel/tenants/_list.html" %} + {{ table.render }} {% endblock %} diff --git a/horizon/horizon/dashboards/syspanel/templates/syspanel/users/_update.html b/horizon/horizon/dashboards/syspanel/templates/syspanel/users/_update.html index 10b5437a3..630f99f35 100644 --- a/horizon/horizon/dashboards/syspanel/templates/syspanel/users/_update.html +++ b/horizon/horizon/dashboards/syspanel/templates/syspanel/users/_update.html @@ -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 %} diff --git a/horizon/horizon/dashboards/syspanel/templates/syspanel/users/index.html b/horizon/horizon/dashboards/syspanel/templates/syspanel/users/index.html index 00ce931aa..a19978d43 100644 --- a/horizon/horizon/dashboards/syspanel/templates/syspanel/users/index.html +++ b/horizon/horizon/dashboards/syspanel/templates/syspanel/users/index.html @@ -9,5 +9,5 @@ {% endblock page_header %} {% block syspanel_main %} -{{ table.render }} + {{ table.render }} {% endblock %} diff --git a/horizon/horizon/dashboards/syspanel/tenants/views.py b/horizon/horizon/dashboards/syspanel/tenants/views.py index 935c8a6d5..9411502f3 100644 --- a/horizon/horizon/dashboards/syspanel/tenants/views.py +++ b/horizon/horizon/dashboards/syspanel/tenants/views.py @@ -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: diff --git a/horizon/horizon/dashboards/syspanel/users/forms.py b/horizon/horizon/dashboards/syspanel/users/forms.py index 598d76ded..9fbf97f08 100644 --- a/horizon/horizon/dashboards/syspanel/users/forms.py +++ b/horizon/horizon/dashboards/syspanel/users/forms.py @@ -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()) diff --git a/horizon/horizon/dashboards/syspanel/users/urls.py b/horizon/horizon/dashboards/syspanel/users/urls.py index 12a26a75a..708f180b0 100644 --- a/horizon/horizon/dashboards/syspanel/users/urls.py +++ b/horizon/horizon/dashboards/syspanel/users/urls.py @@ -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[^/]+)/update/$', 'update', name='update'), - url(r'^create/$', 'create', name='create')) + url(r'^$', IndexView.as_view(), name='index'), + url(r'^(?P[^/]+)/update/$', UpdateView.as_view(), name='update'), + url(r'^create/$', CreateView.as_view(), name='create')) diff --git a/horizon/horizon/dashboards/syspanel/users/views.py b/horizon/horizon/dashboards/syspanel/users/views.py index f6c45dc7b..1faa2cae9 100644 --- a/horizon/horizon/dashboards/syspanel/users/views.py +++ b/horizon/horizon/dashboards/syspanel/users/views.py @@ -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'