Browse Source

multiple user edits and deltes working

php2python
Holger Frey 7 years ago
parent
commit
c638ede5a3
  1. 79
      ordr2/templates/admin/users_change_roles.jinja2
  2. 58
      ordr2/views/admin.py

79
ordr2/templates/admin/users_change_roles.jinja2

@ -0,0 +1,79 @@ @@ -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 %}
<div class="content controls">
<div class="container-fluid">
<div class="row-fluid">
<div class="page-controls">
<h1>Change Role of User{{ 's' if accounts|length > 1 }}</h1>
</div>
</div>
<div class="row">
<div class="span10">
<div class="action-header">
<h3>The role of the following user{{ 's' if accounts|length > 1 }} will be changed:</h3>
</div>
<form action="{{ request.resource_url(context, 'roles') }}" method="POST" class="action">
<input type="hidden" name="csrf_token" value="{{get_csrf_token()}}">
<table class="table">
<thead>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Role</th>
</thead>
<tbody>
{% for account in accounts %}
<tr>
<td class="column-user">
{{ account.user_name }}
</td>
<td>{{ account.first_name }} </td>
<td>{{ account.last_name }} </td>
<td>{{ account.email }} </td>
<td>
<select name="account-{{ account.id }}" class="select-role span2">
{% for value, display in roles %}
<option value="{{ value }}" {{ 'selected="selected"' if value == account.role.name }}>{{ display }}</option>
{% endfor %}
</select>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<fieldset class="form-actions">
<div class="right">
<button name="change" type="submit" value="change" class="btn btn-large btn-danger">Change Role{{ 's' if accounts|length > 1 }}</button>
<button name="cancel" type="submit" value="cancel" class="btn btn-large">Cancel</button>
</div>
<div class="btn-group quick-action left" data-action="account">
<a data-value="USER" href="#" class="btn btn-large btn-primary">Set all to User</a>
<a href="#" data-toggle="dropdown" class="btn btn-large btn-primary dropdown-toggle"><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a data-value="INACTIVE" href="#">Set all to Inactive</a></li>
<li><a data-value="PURCHASER" href="#">Set all to Purchaser</a></li>
</ul>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}

58
ordr2/views/admin.py

@ -60,7 +60,6 @@ def change_column_view(context, request): @@ -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): @@ -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): @@ -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',