From 332fadd42ee122deb08a0b4473e40e5de56486f6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 4 Dec 2017 18:49:52 -0600 Subject: [PATCH] Refactor people view to use master3 --- tailbone/forms2/core.py | 3 + tailbone/views/people.py | 135 +++++++++++++++++++++------------------ 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index 4631afce..646d001a 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -331,6 +331,9 @@ class Form(object): return fields + def append(self, field): + self.fields.append(field) + def insert_before(self, field, newfield): self.fields.insert_before(field, newfield) diff --git a/tailbone/views/people.py b/tailbone/views/people.py index 203c4382..c88d48b5 100644 --- a/tailbone/views/people.py +++ b/tailbone/views/people.py @@ -31,49 +31,10 @@ 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 -from tailbone import forms -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 "" +from tailbone.views import MasterView3 as MasterView, AutocompleteView class PeopleView(MasterView): @@ -93,6 +54,19 @@ class PeopleView(MasterView): 'email', ] + form_fields = [ + 'first_name', + 'middle_name', + 'last_name', + 'display_name', + 'phone', + 'email', + 'address', + 'employee', + 'customers', + 'users', + ] + def configure_grid(self, 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 True - def _preconfigure_fieldset(self, fs): - fs.display_name.set(label="Full Name") - 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_form(self, f): + super(PeopleView, self).configure_form(f) - def configure_fieldset(self, fs): - fs.configure( - include=[ - fs.first_name, - fs.middle_name, - fs.last_name, - fs.display_name, - fs.phone, - fs.email, - fs.address, - fs.employee, - fs._customers, - fs.users, - ]) + f.set_label('display_name', "Full Name") + + f.set_readonly('phone') + f.set_label('phone', "Phone Number") + + f.set_readonly('email') + f.set_label('email', "Email Address") + + f.set_readonly('address') + f.set_label('address', "Mailing Address") + + f.set_readonly('employee') + 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): return [