Add "make user" button when viewing person w/ no user account

This commit is contained in:
Lance Edgar 2017-10-29 01:18:05 -07:00
parent b26036f366
commit 2bbe6c8346
2 changed files with 68 additions and 5 deletions

View file

@ -0,0 +1,27 @@
## -*- coding: utf-8; -*-
<%inherit file="/master/view.mako" />
<%def name="extra_javascript()">
${parent.extra_javascript()}
% if not instance.users and request.has_perm('users.create'):
<script type="text/javascript">
$(function() {
$('#make-user').click(function() {
if (confirm("Really make a user account for this person?")) {
disable_button(this);
$('form[name="make-user-form"]').submit();
}
});
});
</script>
% endif
</%def>
${parent.body()}
% if not instance.users and request.has_perm('users.create'):
${h.form(url('people.make_user'), name='make-user-form')}
${h.csrf_token(request)}
${h.hidden('person_uuid', value=instance.uuid)}
${h.end_form()}
% endif

View file

@ -29,6 +29,8 @@ from __future__ import unicode_literals, absolute_import
import six import six
import sqlalchemy as sa import sqlalchemy as sa
from rattail.db import model, api
import formalchemy as fa import formalchemy as fa
from pyramid.httpexceptions import HTTPFound, HTTPNotFound from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from webhelpers2.html import HTML, tags from webhelpers2.html import HTML, tags
@ -36,8 +38,6 @@ from webhelpers2.html import HTML, tags
from tailbone import forms from tailbone import forms
from tailbone.views import MasterView2 as MasterView, AutocompleteView from tailbone.views import MasterView2 as MasterView, AutocompleteView
from rattail.db import model
class CustomersFieldRenderer(fa.FieldRenderer): class CustomersFieldRenderer(fa.FieldRenderer):
@ -63,14 +63,17 @@ class UsersFieldRenderer(fa.FieldRenderer):
def render_readonly(self, **kwargs): def render_readonly(self, **kwargs):
users = self.raw_value users = self.raw_value
if not users:
return ""
items = [] items = []
for user in users: for user in users:
text = user.username text = user.username
url = self.request.route_url('users.view', uuid=user.uuid) url = self.request.route_url('users.view', uuid=user.uuid)
items.append(HTML.tag('li', c=tags.link_to(text, url))) items.append(HTML.tag('li', c=tags.link_to(text, url)))
return HTML.tag('ul', c=items) if items:
return HTML.tag('ul', c=items)
elif self.request.has_perm('users.create'):
return HTML.tag('button', type='button', id='make-user', c="Make User")
else:
return ""
class PeopleView(MasterView): class PeopleView(MasterView):
@ -182,6 +185,39 @@ class PeopleView(MasterView):
(model.VendorContact, 'person_uuid'), (model.VendorContact, 'person_uuid'),
] ]
def make_user(self):
uuid = self.request.POST['person_uuid']
person = self.Session.query(model.Person).get(uuid)
if not person:
return self.notfound()
if person.users:
raise RuntimeError("person {} already has {} user accounts: ".format(
person.uuid, len(person.users), person))
user = model.User()
user.username = api.make_username(person)
user.person = person
user.active = False
self.Session.add(user)
self.Session.flush()
self.request.session.flash("User has been created: {}".format(user.username))
return self.redirect(self.request.route_url('users.view', uuid=user.uuid))
@classmethod
def defaults(cls, config):
cls._people_defaults(config)
cls._defaults(config)
@classmethod
def _people_defaults(cls, config):
route_prefix = cls.get_route_prefix()
url_prefix = cls.get_url_prefix()
# make user for person
config.add_route('{}.make_user'.format(route_prefix), '{}/make-user'.format(url_prefix),
request_method='POST')
config.add_view(cls, attr='make_user', route_name='{}.make_user'.format(route_prefix),
permission='users.create')
class PeopleAutocomplete(AutocompleteView): class PeopleAutocomplete(AutocompleteView):