2017-07-06 00:19:31 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2013-05-07 19:41:58 -05:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2019-02-02 20:30:35 -06:00
|
|
|
# Copyright © 2010-2019 Lance Edgar
|
2013-05-07 19:41:58 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2013-05-07 19:41:58 -05:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2013-05-07 19:41:58 -05:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2013-05-07 19:41:58 -05:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
2013-09-01 09:27:47 -05:00
|
|
|
User Views
|
2013-05-07 19:41:58 -05:00
|
|
|
"""
|
|
|
|
|
2016-02-01 16:11:24 -06:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2014-08-05 23:23:55 -05:00
|
|
|
|
2017-02-04 18:16:18 -06:00
|
|
|
import copy
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
import six
|
2015-08-11 23:19:41 -05:00
|
|
|
from sqlalchemy import orm
|
|
|
|
|
2015-02-09 15:33:16 -06:00
|
|
|
from rattail.db import model
|
2019-10-04 22:31:19 -05:00
|
|
|
from rattail.db.auth import administrator_role, guest_role, authenticated_role, set_user_password, has_permission
|
2015-01-19 00:45:26 -06:00
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
import colander
|
|
|
|
from deform import widget as dfwidget
|
2017-07-06 20:13:42 -05:00
|
|
|
from webhelpers2.html import HTML, tags
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2018-02-11 22:48:48 -06:00
|
|
|
from tailbone import forms
|
2015-08-11 23:19:41 -05:00
|
|
|
from tailbone.db import Session
|
2018-02-05 21:23:23 -06:00
|
|
|
from tailbone.views import MasterView
|
2017-12-06 12:40:27 -06:00
|
|
|
from tailbone.views.principal import PrincipalMasterView, PermissionsRenderer
|
2018-11-26 22:07:30 -06:00
|
|
|
from tailbone.config import expose_vuejs_experiments
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
|
2017-02-06 13:37:42 -06:00
|
|
|
class UsersView(PrincipalMasterView):
|
2015-08-11 23:19:41 -05:00
|
|
|
"""
|
|
|
|
Master view for the User model.
|
|
|
|
"""
|
|
|
|
model_class = model.User
|
2017-08-04 17:14:38 -05:00
|
|
|
has_rows = True
|
|
|
|
model_row_class = model.UserEvent
|
2017-07-15 02:46:39 -05:00
|
|
|
has_versions = True
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
grid_columns = [
|
2017-02-17 12:49:15 -06:00
|
|
|
'username',
|
2017-12-06 12:40:27 -06:00
|
|
|
'person',
|
2017-02-17 12:49:15 -06:00
|
|
|
'active',
|
|
|
|
]
|
2015-01-19 00:45:26 -06:00
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
form_fields = [
|
2017-07-07 09:13:53 -05:00
|
|
|
'username',
|
|
|
|
'person',
|
2019-02-02 20:30:35 -06:00
|
|
|
'first_name_',
|
|
|
|
'last_name_',
|
|
|
|
'display_name_',
|
2017-10-13 16:12:28 -05:00
|
|
|
'active',
|
2017-12-06 12:40:27 -06:00
|
|
|
'active_sticky',
|
2018-05-30 16:31:19 -05:00
|
|
|
'set_password',
|
2017-12-06 12:40:27 -06:00
|
|
|
'roles',
|
2017-07-07 09:13:53 -05:00
|
|
|
]
|
|
|
|
|
2017-08-04 17:14:38 -05:00
|
|
|
row_grid_columns = [
|
|
|
|
'type_code',
|
|
|
|
'occurred',
|
|
|
|
]
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
mergeable = True
|
|
|
|
merge_additive_fields = [
|
|
|
|
'sent_message_count',
|
|
|
|
'received_message_count',
|
|
|
|
]
|
2019-05-06 21:43:59 -05:00
|
|
|
merge_coalesce_fields = [
|
|
|
|
'person_uuid',
|
|
|
|
'person_name',
|
|
|
|
]
|
2017-12-06 12:40:27 -06:00
|
|
|
merge_fields = merge_additive_fields + [
|
|
|
|
'uuid',
|
|
|
|
'username',
|
|
|
|
'person_uuid',
|
|
|
|
'person_name',
|
|
|
|
'role_count',
|
|
|
|
'active',
|
|
|
|
]
|
|
|
|
|
2015-08-11 23:19:41 -05:00
|
|
|
def query(self, session):
|
2019-10-04 22:31:19 -05:00
|
|
|
query = super(UsersView, self).query(session)
|
|
|
|
|
|
|
|
# bring in the related Person(s)
|
|
|
|
query = query.outerjoin(model.Person)\
|
|
|
|
.options(orm.joinedload(model.User.person))
|
|
|
|
|
|
|
|
return query
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2015-08-11 23:19:41 -05:00
|
|
|
def configure_grid(self, g):
|
2017-07-07 09:13:53 -05:00
|
|
|
super(UsersView, self).configure_grid(g)
|
|
|
|
|
2015-08-11 23:19:41 -05:00
|
|
|
del g.filters['salt']
|
|
|
|
g.filters['username'].default_active = True
|
|
|
|
g.filters['username'].default_verb = 'contains'
|
|
|
|
g.filters['active'].default_active = True
|
|
|
|
g.filters['active'].default_verb = 'is_true'
|
2017-07-07 09:13:53 -05:00
|
|
|
g.filters['person'] = g.make_filter('person', model.Person.display_name,
|
2015-08-11 23:19:41 -05:00
|
|
|
default_active=True, default_verb='contains')
|
2018-05-30 16:31:19 -05:00
|
|
|
|
|
|
|
# password
|
|
|
|
g.set_filter('password', model.User.password,
|
|
|
|
verbs=['is_null', 'is_not_null'])
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2017-08-09 21:41:42 -05:00
|
|
|
g.set_sorter('person', model.Person.display_name)
|
|
|
|
g.set_sorter('first_name', model.Person.first_name)
|
|
|
|
g.set_sorter('last_name', model.Person.last_name)
|
|
|
|
g.set_sorter('display_name', model.Person.display_name)
|
2017-12-04 22:40:10 -06:00
|
|
|
g.set_sort_defaults('username')
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
g.set_label('person', "Person's Name")
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2017-08-02 20:40:02 -05:00
|
|
|
g.set_link('username')
|
|
|
|
g.set_link('person')
|
2017-08-09 21:41:42 -05:00
|
|
|
g.set_link('first_name')
|
|
|
|
g.set_link('last_name')
|
|
|
|
g.set_link('display_name')
|
2017-08-02 20:40:02 -05:00
|
|
|
|
2018-11-26 22:07:30 -06:00
|
|
|
def template_kwargs_index(self, **kwargs):
|
|
|
|
kwargs['expose_vuejs_experiments'] = expose_vuejs_experiments(self.rattail_config)
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def vue_index(self):
|
|
|
|
if not expose_vuejs_experiments(self.rattail_config):
|
|
|
|
raise self.notfound()
|
|
|
|
|
|
|
|
return self.render_to_response('vue_index', {})
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
def unique_username(self, node, value):
|
|
|
|
query = self.Session.query(model.User)\
|
|
|
|
.filter(model.User.username == value)
|
|
|
|
if self.editing:
|
|
|
|
user = self.get_instance()
|
|
|
|
query = query.filter(model.User.uuid != user.uuid)
|
|
|
|
if query.count():
|
|
|
|
raise colander.Invalid(node, "Username must be unique")
|
|
|
|
|
|
|
|
def configure_form(self, f):
|
|
|
|
super(UsersView, self).configure_form(f)
|
|
|
|
user = f.model_instance
|
|
|
|
|
|
|
|
# username
|
|
|
|
f.set_validator('username', self.unique_username)
|
|
|
|
|
|
|
|
# person
|
|
|
|
f.set_renderer('person', self.render_person)
|
|
|
|
if self.creating or self.editing:
|
2018-02-22 13:25:20 -06:00
|
|
|
if 'person' in f.fields:
|
|
|
|
f.replace('person', 'person_uuid')
|
|
|
|
f.set_node('person_uuid', colander.String(), missing=colander.null)
|
|
|
|
person_display = ""
|
|
|
|
if self.request.method == 'POST':
|
|
|
|
if self.request.POST.get('person_uuid'):
|
|
|
|
person = self.Session.query(model.Person).get(self.request.POST['person_uuid'])
|
|
|
|
if person:
|
|
|
|
person_display = six.text_type(person)
|
|
|
|
elif self.editing:
|
|
|
|
person_display = six.text_type(user.person or '')
|
|
|
|
people_url = self.request.route_url('people.autocomplete')
|
|
|
|
f.set_widget('person_uuid', forms.widgets.JQueryAutocompleteWidget(
|
|
|
|
field_display=person_display, service_url=people_url))
|
|
|
|
f.set_label('person_uuid', "Person")
|
2017-12-06 12:40:27 -06:00
|
|
|
|
2019-02-02 20:30:35 -06:00
|
|
|
# person name(s)
|
|
|
|
if self.editing:
|
|
|
|
# must explicitly set default, for "custom" field names
|
|
|
|
f.set_default('first_name_', user.first_name or "")
|
|
|
|
f.set_default('last_name_', user.last_name or "")
|
|
|
|
f.set_default('display_name_', user.display_name or "")
|
|
|
|
elif not self.creating:
|
|
|
|
# must provide custom renderer as well
|
|
|
|
f.set_renderer('first_name_', self.render_person_name)
|
|
|
|
f.set_renderer('last_name_', self.render_person_name)
|
|
|
|
f.set_renderer('display_name_', self.render_person_name)
|
|
|
|
|
2018-05-30 16:31:19 -05:00
|
|
|
# set_password
|
|
|
|
f.set_widget('set_password', dfwidget.CheckedPasswordWidget())
|
2017-12-06 12:40:27 -06:00
|
|
|
# if self.creating:
|
|
|
|
# f.set_required('password')
|
|
|
|
|
|
|
|
# roles
|
|
|
|
f.set_renderer('roles', self.render_roles)
|
|
|
|
if self.creating or self.editing:
|
2019-10-04 22:31:19 -05:00
|
|
|
if not self.has_perm('edit_roles'):
|
|
|
|
f.remove_field('roles')
|
|
|
|
else:
|
|
|
|
roles = self.get_possible_roles().all()
|
|
|
|
role_values = [(s.uuid, six.text_type(s)) for s in roles]
|
|
|
|
f.set_node('roles', colander.Set())
|
|
|
|
size = len(roles)
|
|
|
|
if size < 3:
|
|
|
|
size = 3
|
|
|
|
f.set_widget('roles', dfwidget.SelectWidget(multiple=True,
|
|
|
|
size=size,
|
|
|
|
values=role_values))
|
|
|
|
if self.editing:
|
|
|
|
f.set_default('roles', [r.uuid for r in user.roles])
|
|
|
|
elif not self.has_perm('view_roles'):
|
|
|
|
f.remove_field('roles')
|
2017-12-06 12:40:27 -06:00
|
|
|
|
|
|
|
f.set_label('display_name', "Full Name")
|
|
|
|
|
|
|
|
# # hm this should work according to MDN but doesn't seem to...
|
|
|
|
# # https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
|
|
|
|
# fs.username.attrs(autocomplete='new-password')
|
|
|
|
# fs.password.attrs(autocomplete='new-password')
|
|
|
|
# fs.confirm_password.attrs(autocomplete='new-password')
|
|
|
|
|
2015-08-11 23:19:41 -05:00
|
|
|
if self.viewing:
|
2016-08-12 01:58:07 -05:00
|
|
|
permissions = self.request.registry.settings.get('tailbone_permissions', {})
|
2017-12-06 12:40:27 -06:00
|
|
|
f.append('permissions')
|
|
|
|
f.set_renderer('permissions', PermissionsRenderer(permissions=permissions,
|
|
|
|
include_guest=True,
|
|
|
|
include_authenticated=True))
|
|
|
|
|
2016-10-25 18:14:43 -05:00
|
|
|
if self.viewing or self.deleting:
|
2018-05-30 16:31:19 -05:00
|
|
|
f.remove('set_password')
|
2017-12-06 12:40:27 -06:00
|
|
|
|
|
|
|
def get_possible_roles(self):
|
2019-10-04 22:31:19 -05:00
|
|
|
|
|
|
|
# some roles should never have users "belong" to them
|
2017-12-06 12:40:27 -06:00
|
|
|
excluded = [
|
|
|
|
guest_role(self.Session()).uuid,
|
|
|
|
authenticated_role(self.Session()).uuid,
|
|
|
|
]
|
2019-10-04 22:31:19 -05:00
|
|
|
|
|
|
|
# only allow "root" user to change admin role membership
|
|
|
|
if not self.request.is_root:
|
|
|
|
excluded.append(administrator_role(self.Session()).uuid)
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
return self.Session.query(model.Role)\
|
|
|
|
.filter(~model.Role.uuid.in_(excluded))\
|
|
|
|
.order_by(model.Role.name)
|
|
|
|
|
2019-02-19 17:10:02 -06:00
|
|
|
def objectify(self, form, data=None):
|
2019-02-02 20:30:35 -06:00
|
|
|
|
|
|
|
# create/update user as per normal
|
|
|
|
if data is None:
|
|
|
|
data = form.validated
|
2017-12-06 12:40:27 -06:00
|
|
|
user = super(UsersView, self).objectify(form, data)
|
2019-02-02 20:30:35 -06:00
|
|
|
|
|
|
|
# create/update person as needed
|
|
|
|
names = {}
|
|
|
|
if 'first_name_' in form:
|
|
|
|
names['first'] = data['first_name_']
|
|
|
|
if 'last_name_' in form:
|
|
|
|
names['last'] = data['last_name_']
|
|
|
|
if 'display_name_' in form:
|
|
|
|
names['display'] = data['display_name_']
|
|
|
|
# note, do *not* create new person unless name(s) provided
|
|
|
|
if not user.person and any([n for n in names.values()]):
|
|
|
|
user.person = model.Person()
|
|
|
|
if user.person:
|
|
|
|
if 'first' in names:
|
|
|
|
user.person.first_name = names['first']
|
|
|
|
if 'last' in names:
|
|
|
|
user.person.last_name = names['last']
|
|
|
|
if 'display' in names:
|
|
|
|
user.person.display_name = names['display']
|
|
|
|
|
2019-10-04 22:31:19 -05:00
|
|
|
# force "local only" flag unless global access granted
|
|
|
|
if self.secure_global_objects:
|
|
|
|
if not self.has_perm('view_global'):
|
|
|
|
user.person.local_only = True
|
|
|
|
|
2019-02-02 20:30:35 -06:00
|
|
|
# maybe set user password
|
2018-05-30 16:31:19 -05:00
|
|
|
if data['set_password']:
|
|
|
|
set_user_password(user, data['set_password'])
|
2019-02-02 20:30:35 -06:00
|
|
|
|
|
|
|
# update roles for user
|
2017-12-06 12:40:27 -06:00
|
|
|
self.update_roles(user, data)
|
2019-02-02 20:30:35 -06:00
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
return user
|
|
|
|
|
|
|
|
def update_roles(self, user, data):
|
2019-10-24 12:25:59 -05:00
|
|
|
if not self.has_perm('edit_roles'):
|
|
|
|
return
|
|
|
|
if 'roles' not in data:
|
|
|
|
return
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
old_roles = set([r.uuid for r in user.roles])
|
|
|
|
new_roles = data['roles']
|
2019-10-04 22:31:19 -05:00
|
|
|
admin = administrator_role(self.Session())
|
|
|
|
|
|
|
|
# add any new roles for the user, taking care not to add the admin role
|
|
|
|
# unless acting as root
|
2017-12-06 12:40:27 -06:00
|
|
|
for uuid in new_roles:
|
|
|
|
if uuid not in old_roles:
|
2019-10-04 22:31:19 -05:00
|
|
|
if self.request.is_root or uuid != admin.uuid:
|
|
|
|
user._roles.append(model.UserRole(role_uuid=uuid))
|
|
|
|
|
|
|
|
# remove any roles which were *not* specified, although must take care
|
|
|
|
# not to remove admin role, unless acting as root
|
2017-12-06 12:40:27 -06:00
|
|
|
for uuid in old_roles:
|
|
|
|
if uuid not in new_roles:
|
2019-10-04 22:31:19 -05:00
|
|
|
if self.request.is_root or uuid != admin.uuid:
|
|
|
|
role = self.Session.query(model.Role).get(uuid)
|
|
|
|
user.roles.remove(role)
|
2017-12-06 12:40:27 -06:00
|
|
|
|
|
|
|
def render_person(self, user, field):
|
|
|
|
person = user.person
|
|
|
|
if not person:
|
|
|
|
return ""
|
|
|
|
text = six.text_type(person)
|
|
|
|
url = self.request.route_url('people.view', uuid=person.uuid)
|
|
|
|
return tags.link_to(person, url)
|
|
|
|
|
2019-02-02 20:30:35 -06:00
|
|
|
def render_person_name(self, user, field):
|
|
|
|
if not field.endswith('_'):
|
|
|
|
return ""
|
|
|
|
name = getattr(user, field[:-1], None)
|
|
|
|
if not name:
|
|
|
|
return ""
|
|
|
|
return six.text_type(name)
|
|
|
|
|
2017-12-06 12:40:27 -06:00
|
|
|
def render_roles(self, user, field):
|
|
|
|
roles = user.roles
|
|
|
|
items = []
|
|
|
|
for role in roles:
|
|
|
|
text = role.name
|
|
|
|
url = self.request.route_url('roles.view', uuid=role.uuid)
|
2018-02-12 19:22:05 -06:00
|
|
|
items.append(HTML.tag('li', c=[tags.link_to(text, url)]))
|
2017-12-06 12:40:27 -06:00
|
|
|
return HTML.tag('ul', c=items)
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2017-03-19 11:21:00 -05:00
|
|
|
def editable_instance(self, user):
|
|
|
|
if self.rattail_config.demo():
|
|
|
|
return user.username != 'chuck'
|
|
|
|
return True
|
|
|
|
|
|
|
|
def deletable_instance(self, user):
|
|
|
|
if self.rattail_config.demo():
|
|
|
|
return user.username != 'chuck'
|
|
|
|
return True
|
|
|
|
|
2017-08-04 17:14:38 -05:00
|
|
|
def get_row_data(self, user):
|
|
|
|
return self.Session.query(model.UserEvent)\
|
|
|
|
.filter(model.UserEvent.user == user)
|
|
|
|
|
|
|
|
def configure_row_grid(self, g):
|
|
|
|
super(UsersView, self).configure_row_grid(g)
|
|
|
|
g.width = 'half'
|
|
|
|
g.filterable = False
|
2017-12-04 22:40:10 -06:00
|
|
|
g.set_sort_defaults('occurred', 'desc')
|
2017-08-04 17:14:38 -05:00
|
|
|
g.set_enum('type_code', self.enum.USER_EVENT)
|
|
|
|
g.set_label('type_code', "Event Type")
|
|
|
|
g.main_actions = []
|
|
|
|
|
2017-07-15 02:46:39 -05:00
|
|
|
def get_version_child_classes(self):
|
|
|
|
return [
|
|
|
|
(model.UserRole, 'user_uuid'),
|
|
|
|
]
|
|
|
|
|
2017-02-06 13:37:42 -06:00
|
|
|
def find_principals_with_permission(self, session, permission):
|
2017-02-04 18:16:18 -06:00
|
|
|
# TODO: this should search Permission table instead, and work backward to User?
|
|
|
|
all_users = session.query(model.User)\
|
|
|
|
.filter(model.User.active == True)\
|
|
|
|
.order_by(model.User.username)\
|
|
|
|
.options(orm.joinedload(model.User._roles)\
|
|
|
|
.joinedload(model.UserRole.role)\
|
|
|
|
.joinedload(model.Role._permissions))
|
|
|
|
users = []
|
|
|
|
for user in all_users:
|
|
|
|
if has_permission(session, user, permission):
|
|
|
|
users.append(user)
|
|
|
|
return users
|
|
|
|
|
2017-02-17 12:49:15 -06:00
|
|
|
def get_merge_data(self, user):
|
|
|
|
return {
|
|
|
|
'uuid': user.uuid,
|
|
|
|
'username': user.username,
|
|
|
|
'person_uuid': user.person_uuid,
|
|
|
|
'person_name': user.person.display_name if user.person else None,
|
|
|
|
'_roles': user.roles,
|
|
|
|
'role_count': len(user.roles),
|
|
|
|
'active': user.active,
|
|
|
|
'sent_message_count': len(user.sent_messages),
|
|
|
|
'received_message_count': len(user._messages),
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_merge_resulting_data(self, remove, keep):
|
|
|
|
result = super(UsersView, self).get_merge_resulting_data(remove, keep)
|
|
|
|
result['role_count'] = len(set(remove['_roles'] + keep['_roles']))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def merge_objects(self, removing, keeping):
|
|
|
|
# TODO: merge roles, messages
|
|
|
|
assert not removing.sent_messages
|
|
|
|
assert not removing._messages
|
|
|
|
assert not removing._roles
|
|
|
|
self.Session.delete(removing)
|
|
|
|
|
2018-11-26 22:07:30 -06:00
|
|
|
@classmethod
|
|
|
|
def defaults(cls, config):
|
2019-10-04 22:31:19 -05:00
|
|
|
|
|
|
|
# TODO: probably should stop doing this one
|
|
|
|
cls._vue_index_defaults(config)
|
|
|
|
|
|
|
|
cls._user_defaults(config)
|
|
|
|
cls._principal_defaults(config)
|
|
|
|
cls._defaults(config)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _user_defaults(cls, config):
|
|
|
|
"""
|
|
|
|
Provide extra default configuration for the User master view.
|
|
|
|
"""
|
|
|
|
permission_prefix = cls.get_permission_prefix()
|
|
|
|
model_title = cls.get_model_title()
|
|
|
|
|
|
|
|
# view/edit roles
|
|
|
|
config.add_tailbone_permission(permission_prefix, '{}.view_roles'.format(permission_prefix),
|
|
|
|
"View the Roles to which a {} belongs".format(model_title))
|
|
|
|
config.add_tailbone_permission(permission_prefix, '{}.edit_roles'.format(permission_prefix),
|
|
|
|
"Edit the Roles to which a {} belongs".format(model_title))
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _vue_index_defaults(cls, config):
|
|
|
|
"""
|
|
|
|
Provide default configuration for the "Vue.js index" view. This was
|
|
|
|
essentially an experiment and probably should be abandoned.
|
|
|
|
"""
|
2018-11-26 22:07:30 -06:00
|
|
|
rattail_config = config.registry.settings.get('rattail_config')
|
|
|
|
route_prefix = cls.get_route_prefix()
|
|
|
|
url_prefix = cls.get_url_prefix()
|
|
|
|
permission_prefix = cls.get_permission_prefix()
|
|
|
|
|
|
|
|
# vue-index
|
|
|
|
config.add_route('{}.vue_index'.format(route_prefix), '{}/vue-index/'.format(url_prefix))
|
|
|
|
config.add_view(cls, attr='vue_index', route_name='{}.vue_index'.format(route_prefix),
|
|
|
|
permission='{}.list'.format(permission_prefix))
|
|
|
|
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2017-08-04 17:14:38 -05:00
|
|
|
class UserEventsView(MasterView):
|
|
|
|
"""
|
|
|
|
Master view for all user events
|
|
|
|
"""
|
|
|
|
model_class = model.UserEvent
|
|
|
|
url_prefix = '/user-events'
|
|
|
|
viewable = False
|
|
|
|
creatable = False
|
|
|
|
editable = False
|
|
|
|
deletable = False
|
|
|
|
|
|
|
|
grid_columns = [
|
|
|
|
'user',
|
|
|
|
'person',
|
|
|
|
'type_code',
|
|
|
|
'occurred',
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_data(self, session=None):
|
|
|
|
query = super(UserEventsView, self).get_data(session=session)
|
|
|
|
return query.join(model.User)
|
|
|
|
|
|
|
|
def configure_grid(self, g):
|
|
|
|
super(UserEventsView, self).configure_grid(g)
|
|
|
|
g.set_joiner('person', lambda q: q.outerjoin(model.Person))
|
|
|
|
g.set_sorter('user', model.User.username)
|
|
|
|
g.set_sorter('person', model.Person.display_name)
|
|
|
|
g.filters['user'] = g.make_filter('user', model.User.username)
|
|
|
|
g.filters['person'] = g.make_filter('person', model.Person.display_name)
|
|
|
|
g.set_enum('type_code', self.enum.USER_EVENT)
|
|
|
|
g.set_type('occurred', 'datetime')
|
|
|
|
g.set_renderer('user', self.render_user)
|
|
|
|
g.set_renderer('person', self.render_person)
|
2017-12-04 22:40:10 -06:00
|
|
|
g.set_sort_defaults('occurred', 'desc')
|
2017-08-04 17:14:38 -05:00
|
|
|
g.set_label('user', "Username")
|
|
|
|
g.set_label('type_code', "Event Type")
|
|
|
|
|
|
|
|
def render_user(self, event, column):
|
|
|
|
return event.user.username
|
|
|
|
|
|
|
|
def render_person(self, event, column):
|
2017-08-09 21:41:42 -05:00
|
|
|
if event.user.person:
|
|
|
|
return event.user.person.display_name
|
2017-08-04 17:14:38 -05:00
|
|
|
|
|
|
|
|
2013-05-07 19:41:58 -05:00
|
|
|
def includeme(config):
|
2015-08-11 23:19:41 -05:00
|
|
|
UsersView.defaults(config)
|
2017-08-04 17:14:38 -05:00
|
|
|
UserEventsView.defaults(config)
|