fix: improve wutta People view a bit

try to behave more like traditional tailbone, for the few things
supported so far.  taking a conservative approach here for now since
probably other things are more pressing.
This commit is contained in:
Lance Edgar 2024-08-15 21:12:34 -05:00
parent 53040dc6be
commit 7f0c571a44
2 changed files with 110 additions and 27 deletions

View file

@ -24,11 +24,14 @@
Person Views
"""
from rattail.db.model import Person
import colander
import sqlalchemy as sa
from webhelpers2.html import HTML
from wuttaweb.views import people as wutta
from tailbone.views import people as tailbone
from tailbone.db import Session
from rattail.db.model import Person
class PersonView(wutta.PersonView):
@ -42,9 +45,9 @@ class PersonView(wutta.PersonView):
model_class = Person
Session = Session
# labels = {
# 'display_name': "Full Name",
# }
labels = {
'display_name': "Full Name",
}
grid_columns = [
'display_name',
@ -60,15 +63,16 @@ class PersonView(wutta.PersonView):
'middle_name',
'last_name',
'display_name',
'default_phone',
'default_email',
'phone',
'email',
# TODO
# 'address',
# 'employee',
'customers',
# 'members',
'users',
]
##############################
# CRUD methods
##############################
def get_query(self, session=None):
""" """
model = self.app.model
@ -76,25 +80,64 @@ class PersonView(wutta.PersonView):
return session.query(model.Person)\
.order_by(model.Person.display_name)
def configure_grid(self, g):
""" """
super().configure_grid(g)
# display_name
g.set_link('display_name')
# first_name
g.set_link('first_name')
# last_name
g.set_link('last_name')
# merge_requested
g.set_label('merge_requested', "MR")
g.set_renderer('merge_requested', self.render_merge_requested)
def configure_form(self, f):
""" """
super().configure_form(f)
# default_phone
f.set_required('default_phone', False)
# default_email
f.set_required('default_email', False)
# customers
# email
if self.creating or self.editing:
f.remove('customers')
f.remove('email')
else:
# nb. avoid colanderalchemy
f.set_node('email', colander.String())
# phone
if self.creating or self.editing:
f.remove('phone')
else:
# nb. avoid colanderalchemy
f.set_node('phone', colander.String())
##############################
# support methods
##############################
def render_merge_requested(self, person, key, value, session=None):
""" """
model = self.app.model
session = session or self.Session()
merge_request = session.query(model.MergePeopleRequest)\
.filter(sa.or_(
model.MergePeopleRequest.removing_uuid == person.uuid,
model.MergePeopleRequest.keeping_uuid == person.uuid))\
.filter(model.MergePeopleRequest.merged == None)\
.first()
if merge_request:
return HTML.tag('span',
class_='has-text-danger has-text-weight-bold',
title="A merge has been requested for this person.",
c="MR")
def defaults(config, **kwargs):
base = globals()
kwargs.setdefault('PersonView', base['PersonView'])
kwargs.setdefault('PersonView', PersonView)
tailbone.defaults(config, **kwargs)

View file

@ -23,6 +23,19 @@ class TestPersonView(WebTestCase):
query = view.get_query(session=self.session)
self.assertIsInstance(query, orm.Query)
def test_configure_grid(self):
model = self.app.model
barney = model.User(username='barney')
self.session.add(barney)
self.session.commit()
view = self.make_view()
# sanity / coverage check
grid = view.make_grid(model_class=model.Person)
self.assertNotIn('first_name', grid.linked_columns)
view.configure_grid(grid)
self.assertIn('first_name', grid.linked_columns)
def test_configure_form(self):
model = self.app.model
barney = model.User(username='barney')
@ -30,18 +43,45 @@ class TestPersonView(WebTestCase):
self.session.commit()
view = self.make_view()
# customers field remains when viewing
# email field remains when viewing
with patch.object(view, 'viewing', new=True):
form = view.make_form(model_instance=barney,
fields=view.get_form_fields())
self.assertIn('customers', form.fields)
self.assertIn('email', form.fields)
view.configure_form(form)
self.assertIn('customers', form)
self.assertIn('email', form)
# customers field removed when editing
# email field removed when editing
with patch.object(view, 'editing', new=True):
form = view.make_form(model_instance=barney,
fields=view.get_form_fields())
self.assertIn('customers', form.fields)
self.assertIn('email', form.fields)
view.configure_form(form)
self.assertNotIn('customers', form)
self.assertNotIn('email', form)
def test_render_merge_requested(self):
model = self.app.model
barney = model.Person(display_name="Barney Rubble")
self.session.add(barney)
user = model.User(username='user')
self.session.add(user)
self.session.commit()
view = self.make_view()
# null by default
html = view.render_merge_requested(barney, 'merge_requested', None,
session=self.session)
self.assertIsNone(html)
# unless a merge request exists
barney2 = model.Person(display_name="Barney Rubble")
self.session.add(barney2)
self.session.commit()
mr = model.MergePeopleRequest(removing_uuid=barney2.uuid,
keeping_uuid=barney.uuid,
requested_by=user)
self.session.add(mr)
self.session.commit()
html = view.render_merge_requested(barney, 'merge_requested', None,
session=self.session)
self.assertIn('<span ', html)