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:
parent
53040dc6be
commit
7f0c571a44
|
@ -24,11 +24,14 @@
|
||||||
Person Views
|
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 wuttaweb.views import people as wutta
|
||||||
from tailbone.views import people as tailbone
|
from tailbone.views import people as tailbone
|
||||||
from tailbone.db import Session
|
from tailbone.db import Session
|
||||||
|
from rattail.db.model import Person
|
||||||
|
|
||||||
|
|
||||||
class PersonView(wutta.PersonView):
|
class PersonView(wutta.PersonView):
|
||||||
|
@ -42,9 +45,9 @@ class PersonView(wutta.PersonView):
|
||||||
model_class = Person
|
model_class = Person
|
||||||
Session = Session
|
Session = Session
|
||||||
|
|
||||||
# labels = {
|
labels = {
|
||||||
# 'display_name': "Full Name",
|
'display_name': "Full Name",
|
||||||
# }
|
}
|
||||||
|
|
||||||
grid_columns = [
|
grid_columns = [
|
||||||
'display_name',
|
'display_name',
|
||||||
|
@ -60,15 +63,16 @@ class PersonView(wutta.PersonView):
|
||||||
'middle_name',
|
'middle_name',
|
||||||
'last_name',
|
'last_name',
|
||||||
'display_name',
|
'display_name',
|
||||||
'default_phone',
|
'phone',
|
||||||
'default_email',
|
'email',
|
||||||
|
# TODO
|
||||||
# 'address',
|
# 'address',
|
||||||
# 'employee',
|
|
||||||
'customers',
|
|
||||||
# 'members',
|
|
||||||
'users',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
##############################
|
||||||
|
# CRUD methods
|
||||||
|
##############################
|
||||||
|
|
||||||
def get_query(self, session=None):
|
def get_query(self, session=None):
|
||||||
""" """
|
""" """
|
||||||
model = self.app.model
|
model = self.app.model
|
||||||
|
@ -76,25 +80,64 @@ class PersonView(wutta.PersonView):
|
||||||
return session.query(model.Person)\
|
return session.query(model.Person)\
|
||||||
.order_by(model.Person.display_name)
|
.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):
|
def configure_form(self, f):
|
||||||
""" """
|
""" """
|
||||||
super().configure_form(f)
|
super().configure_form(f)
|
||||||
|
|
||||||
# default_phone
|
# email
|
||||||
f.set_required('default_phone', False)
|
|
||||||
|
|
||||||
# default_email
|
|
||||||
f.set_required('default_email', False)
|
|
||||||
|
|
||||||
# customers
|
|
||||||
if self.creating or self.editing:
|
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):
|
def defaults(config, **kwargs):
|
||||||
base = globals()
|
kwargs.setdefault('PersonView', PersonView)
|
||||||
|
|
||||||
kwargs.setdefault('PersonView', base['PersonView'])
|
|
||||||
tailbone.defaults(config, **kwargs)
|
tailbone.defaults(config, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,19 @@ class TestPersonView(WebTestCase):
|
||||||
query = view.get_query(session=self.session)
|
query = view.get_query(session=self.session)
|
||||||
self.assertIsInstance(query, orm.Query)
|
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):
|
def test_configure_form(self):
|
||||||
model = self.app.model
|
model = self.app.model
|
||||||
barney = model.User(username='barney')
|
barney = model.User(username='barney')
|
||||||
|
@ -30,18 +43,45 @@ class TestPersonView(WebTestCase):
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
view = self.make_view()
|
view = self.make_view()
|
||||||
|
|
||||||
# customers field remains when viewing
|
# email field remains when viewing
|
||||||
with patch.object(view, 'viewing', new=True):
|
with patch.object(view, 'viewing', new=True):
|
||||||
form = view.make_form(model_instance=barney,
|
form = view.make_form(model_instance=barney,
|
||||||
fields=view.get_form_fields())
|
fields=view.get_form_fields())
|
||||||
self.assertIn('customers', form.fields)
|
self.assertIn('email', form.fields)
|
||||||
view.configure_form(form)
|
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):
|
with patch.object(view, 'editing', new=True):
|
||||||
form = view.make_form(model_instance=barney,
|
form = view.make_form(model_instance=barney,
|
||||||
fields=view.get_form_fields())
|
fields=view.get_form_fields())
|
||||||
self.assertIn('customers', form.fields)
|
self.assertIn('email', form.fields)
|
||||||
view.configure_form(form)
|
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)
|
||||||
|
|
Loading…
Reference in a new issue