From 2bbe6c834633d95d943312f3f9c5c5ac23b395e6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 29 Oct 2017 01:18:05 -0700 Subject: [PATCH] Add "make user" button when viewing person w/ no user account --- tailbone/templates/people/view.mako | 27 +++++++++++++++++ tailbone/views/people.py | 46 +++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 tailbone/templates/people/view.mako diff --git a/tailbone/templates/people/view.mako b/tailbone/templates/people/view.mako new file mode 100644 index 00000000..72079db8 --- /dev/null +++ b/tailbone/templates/people/view.mako @@ -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'): + + % endif + + +${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 diff --git a/tailbone/views/people.py b/tailbone/views/people.py index c8dc73b6..203c4382 100644 --- a/tailbone/views/people.py +++ b/tailbone/views/people.py @@ -29,6 +29,8 @@ from __future__ import unicode_literals, absolute_import import six import sqlalchemy as sa +from rattail.db import model, api + import formalchemy as fa from pyramid.httpexceptions import HTTPFound, HTTPNotFound from webhelpers2.html import HTML, tags @@ -36,8 +38,6 @@ from webhelpers2.html import HTML, tags from tailbone import forms from tailbone.views import MasterView2 as MasterView, AutocompleteView -from rattail.db import model - class CustomersFieldRenderer(fa.FieldRenderer): @@ -63,14 +63,17 @@ class UsersFieldRenderer(fa.FieldRenderer): def render_readonly(self, **kwargs): users = self.raw_value - if not users: - return "" items = [] for user in users: text = user.username url = self.request.route_url('users.view', uuid=user.uuid) 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): @@ -182,6 +185,39 @@ class PeopleView(MasterView): (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):