Refactor people view to use master3

This commit is contained in:
Lance Edgar 2017-12-04 18:49:52 -06:00
parent c3fb86e391
commit 332fadd42e
2 changed files with 76 additions and 62 deletions

View file

@ -331,6 +331,9 @@ class Form(object):
return fields return fields
def append(self, field):
self.fields.append(field)
def insert_before(self, field, newfield): def insert_before(self, field, newfield):
self.fields.insert_before(field, newfield) self.fields.insert_before(field, newfield)

View file

@ -31,49 +31,10 @@ import sqlalchemy as sa
from rattail.db import model, api from rattail.db import model, api
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
from tailbone import forms from tailbone.views import MasterView3 as MasterView, AutocompleteView
from tailbone.views import MasterView2 as MasterView, AutocompleteView
class CustomersFieldRenderer(fa.FieldRenderer):
def render_readonly(self, **kwargs):
customers = self.raw_value
if not customers:
return ''
items = []
for customer in customers:
customer = customer.customer
text = six.text_type(customer)
if customer.id:
text = "({}) {}".format(customer.id, text)
elif customer.number:
text = "({}) {}".format(customer.number, text)
items.append(HTML.tag('li', c=tags.link_to(text, self.request.route_url('customers.view', uuid=customer.uuid))))
return HTML.tag('ul', c=items)
class UsersFieldRenderer(fa.FieldRenderer):
def render_readonly(self, **kwargs):
users = self.raw_value
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)))
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):
@ -93,6 +54,19 @@ class PeopleView(MasterView):
'email', 'email',
] ]
form_fields = [
'first_name',
'middle_name',
'last_name',
'display_name',
'phone',
'email',
'address',
'employee',
'customers',
'users',
]
def configure_grid(self, g): def configure_grid(self, g):
super(PeopleView, self).configure_grid(g) super(PeopleView, self).configure_grid(g)
@ -151,29 +125,66 @@ class PeopleView(MasterView):
return not bool(person.user and person.user.username == 'chuck') return not bool(person.user and person.user.username == 'chuck')
return True return True
def _preconfigure_fieldset(self, fs): def configure_form(self, f):
fs.display_name.set(label="Full Name") super(PeopleView, self).configure_form(f)
fs.phone.set(label="Phone Number", readonly=True)
fs.email.set(label="Email Address", readonly=True)
fs.address.set(label="Mailing Address", readonly=True)
fs.employee.set(renderer=forms.renderers.EmployeeFieldRenderer, attrs={'hyperlink': True}, readonly=True)
fs._customers.set(renderer=CustomersFieldRenderer, readonly=True)
fs.users.set(renderer=UsersFieldRenderer, readonly=True)
def configure_fieldset(self, fs): f.set_label('display_name', "Full Name")
fs.configure(
include=[ f.set_readonly('phone')
fs.first_name, f.set_label('phone', "Phone Number")
fs.middle_name,
fs.last_name, f.set_readonly('email')
fs.display_name, f.set_label('email', "Email Address")
fs.phone,
fs.email, f.set_readonly('address')
fs.address, f.set_label('address', "Mailing Address")
fs.employee,
fs._customers, f.set_readonly('employee')
fs.users, f.set_renderer('employee', self.render_employee)
])
f.set_readonly('customers')
f.set_renderer('customers', self.render_customers)
f.set_readonly('users')
f.set_renderer('users', self.render_users)
def render_employee(self, person, field):
employee = person.employee
if not employee:
return ""
text = six.text_type(employee)
url = self.request.route_url('employees.view', uuid=employee.uuid)
return tags.link_to(text, url)
def render_customers(self, person, field):
customers = person._customers
if not customers:
return ""
items = []
for customer in customers:
customer = customer.customer
text = six.text_type(customer)
if customer.id:
text = "({}) {}".format(customer.id, text)
elif customer.number:
text = "({}) {}".format(customer.number, text)
url = self.request.route_url('customers.view', uuid=customer.uuid)
items.append(HTML.tag('li', c=tags.link_to(text, url)))
return HTML.tag('ul', c=items)
def render_users(self, person, field):
users = person.users
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)))
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 ""
def get_version_child_classes(self): def get_version_child_classes(self):
return [ return [