diff --git a/ordr2/templates/admin/users_change_roles.jinja2 b/ordr2/templates/admin/users_change_roles.jinja2 new file mode 100755 index 0000000..75716e3 --- /dev/null +++ b/ordr2/templates/admin/users_change_roles.jinja2 @@ -0,0 +1,79 @@ +{% extends "ordr2:templates/layout.jinja2" %} +{% import 'ordr2:templates/macros.jinja2' as macros with context %} + +{% block subtitle %} Account | Admin | Users | Change Roles {% endblock subtitle %} + +{% block content %} +
+ +
+
+
+

Change Role of User{{ 's' if accounts|length > 1 }}

+
+
+ +
+
+ +
+

The role of the following user{{ 's' if accounts|length > 1 }} will be changed:

+
+ +
+ + + + + + + + + + + + + {% for account in accounts %} + + + + + + + + {% endfor %} + +
UsernameFirst NameLast NameEmailRole
+ {{ account.user_name }} + {{ account.first_name }} {{ account.last_name }} {{ account.email }} + +
+ +
+
+ + +
+ +
+ +
+ +
+
+ +
+ +
+{% endblock content %} diff --git a/ordr2/views/admin.py b/ordr2/views/admin.py index ad0d754..513901f 100644 --- a/ordr2/views/admin.py +++ b/ordr2/views/admin.py @@ -60,7 +60,6 @@ def change_column_view(context, request): return HTTPFound(context.url()) - @view_config( context='ordr2:resources.UserList', name='actions', @@ -70,7 +69,6 @@ def change_column_view(context, request): renderer='ordr2:templates/admin/users_delete.jinja2' ) def delete_multiple_accounts_form(context, request): - print(list(request.POST.items())) account_ids = [v for k, v in request.POST.items() if k == 'marked'] accounts = request.dbsession.\ query(User).\ @@ -82,6 +80,62 @@ def delete_multiple_accounts_form(context, request): return {'accounts': accounts} +@view_config( + context='ordr2:resources.UserList', + name='actions', + request_param='action=role', + permission='edit', + request_method='POST', + renderer='ordr2:templates/admin/users_change_roles.jinja2' + ) +def edit_multiple_roles_form(context, request): + account_ids = [v for k, v in request.POST.items() if k == 'marked'] + accounts = request.dbsession.\ + query(User).\ + filter(User.id.in_(account_ids)).\ + order_by(User.user_name).\ + all() + if len(accounts) == 0: + return HTTPFound(context.url()) + roles = [(role.name, role.value.capitalize()) for role in Role] + return {'accounts': accounts, 'roles': roles} + + +@view_config( + context='ordr2:resources.UserList', + name='roles', + permission='edit', + request_method='POST' + ) +def edit_multiple_roles_form_processing(context, request): + + if 'change' in request.POST: + count = 0 + for key, value in request.POST.items(): + if not key.startswith('account-'): + continue + _, account_id = key.split('-', 1) + account = request.dbsession.query(User).get(account_id) + if account: + was_active = account.is_active + try: + account.role = Role[value] + except ValueError: + pass + if not was_active and account.is_active: + # user account was activated, notify user + event = AccountActivation(request, account) + request.registry.notify(event) + count += 1 + + if count == 1: + request.flash('success', 'One user account was updated') + elif count > 1: + msg = '{} user accounts were updated.'.format(count) + request.flash('success', msg) + + return HTTPFound(context.url()) + @view_config( context='ordr2:resources.UserAccount',