2015-12-06 18:04:23 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-05-07 19:41:58 -05:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2016-11-21 17:47:24 -06:00
|
|
|
# Copyright © 2010-2016 Lance Edgar
|
2013-05-07 19:41:58 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
2013-09-01 09:27:47 -05:00
|
|
|
Person Views
|
2013-05-07 19:41:58 -05:00
|
|
|
"""
|
|
|
|
|
2015-12-06 18:04:23 -06:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
|
|
|
import sqlalchemy as sa
|
2013-05-07 19:41:58 -05:00
|
|
|
|
2016-11-21 17:47:24 -06:00
|
|
|
import formalchemy as fa
|
2015-12-06 18:04:23 -06:00
|
|
|
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
|
2016-11-21 17:47:24 -06:00
|
|
|
from webhelpers.html import HTML, tags
|
2013-05-21 23:51:41 -05:00
|
|
|
|
2015-12-06 18:04:23 -06:00
|
|
|
from tailbone.views import MasterView, AutocompleteView
|
2014-07-19 20:49:00 -05:00
|
|
|
|
|
|
|
from rattail.db import model
|
2015-12-06 18:04:23 -06:00
|
|
|
|
|
|
|
|
2016-11-21 17:47:24 -06:00
|
|
|
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
|
|
|
|
items.append(HTML.tag('li', c=tags.link_to('{} {}'.format(customer.id, customer),
|
|
|
|
self.request.route_url('customers.view', uuid=customer.uuid))))
|
|
|
|
|
|
|
|
return HTML.tag('ul', c=items)
|
|
|
|
|
|
|
|
|
2015-12-06 18:04:23 -06:00
|
|
|
class PeopleView(MasterView):
|
|
|
|
"""
|
|
|
|
Master view for the Person class.
|
|
|
|
"""
|
|
|
|
model_class = model.Person
|
|
|
|
model_title_plural = "People"
|
|
|
|
route_prefix = 'people'
|
|
|
|
|
|
|
|
def configure_grid(self, g):
|
|
|
|
g.joiners['email'] = lambda q: q.outerjoin(model.PersonEmailAddress, sa.and_(
|
|
|
|
model.PersonEmailAddress.parent_uuid == model.Person.uuid,
|
|
|
|
model.PersonEmailAddress.preference == 1))
|
|
|
|
g.joiners['phone'] = lambda q: q.outerjoin(model.PersonPhoneNumber, sa.and_(
|
|
|
|
model.PersonPhoneNumber.parent_uuid == model.Person.uuid,
|
|
|
|
model.PersonPhoneNumber.preference == 1))
|
|
|
|
|
|
|
|
g.filters['email'] = g.make_filter('email', model.PersonEmailAddress.address,
|
|
|
|
label="Email Address")
|
|
|
|
g.filters['phone'] = g.make_filter('phone', model.PersonPhoneNumber.number,
|
|
|
|
label="Phone Number")
|
|
|
|
|
|
|
|
g.filters['first_name'].default_active = True
|
|
|
|
g.filters['first_name'].default_verb = 'contains'
|
|
|
|
|
|
|
|
g.filters['last_name'].default_active = True
|
|
|
|
g.filters['last_name'].default_verb = 'contains'
|
|
|
|
|
|
|
|
g.sorters['email'] = lambda q, d: q.order_by(getattr(model.PersonEmailAddress.address, d)())
|
|
|
|
g.sorters['phone'] = lambda q, d: q.order_by(getattr(model.PersonPhoneNumber.number, d)())
|
|
|
|
|
2016-12-08 12:13:45 -06:00
|
|
|
g.default_sortkey = 'display_name'
|
2015-12-06 18:04:23 -06:00
|
|
|
|
2013-05-21 23:51:41 -05:00
|
|
|
g.configure(
|
|
|
|
include=[
|
2016-12-08 12:13:45 -06:00
|
|
|
g.display_name.label("Full Name"),
|
2013-05-21 23:51:41 -05:00
|
|
|
g.first_name,
|
|
|
|
g.last_name,
|
|
|
|
g.phone.label("Phone Number"),
|
|
|
|
g.email.label("Email Address"),
|
2015-12-06 18:04:23 -06:00
|
|
|
],
|
2013-05-21 23:51:41 -05:00
|
|
|
readonly=True)
|
|
|
|
|
2015-12-06 18:04:23 -06:00
|
|
|
def get_instance(self):
|
|
|
|
# TODO: I don't recall why this fallback check for a vendor contact
|
|
|
|
# exists here, but leaving it intact for now.
|
|
|
|
key = self.request.matchdict['uuid']
|
|
|
|
instance = self.Session.query(model.Person).get(key)
|
|
|
|
if instance:
|
|
|
|
return instance
|
|
|
|
instance = self.Session.query(model.VendorContact).get(key)
|
|
|
|
if instance:
|
|
|
|
return instance.person
|
|
|
|
raise HTTPNotFound
|
|
|
|
|
2016-11-21 17:47:24 -06:00
|
|
|
def _preconfigure_fieldset(self, fs):
|
2016-12-08 12:13:45 -06:00
|
|
|
fs.display_name.set(label="Full Name")
|
2016-11-21 17:47:24 -06:00
|
|
|
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._customers.set(renderer=CustomersFieldRenderer, readonly=True)
|
|
|
|
|
2015-12-06 18:04:23 -06:00
|
|
|
def configure_fieldset(self, fs):
|
2013-05-21 23:51:41 -05:00
|
|
|
fs.configure(
|
|
|
|
include=[
|
2016-12-08 12:13:45 -06:00
|
|
|
fs.display_name,
|
2013-05-21 23:51:41 -05:00
|
|
|
fs.first_name,
|
2016-01-08 14:42:13 -06:00
|
|
|
fs.middle_name,
|
2013-05-21 23:51:41 -05:00
|
|
|
fs.last_name,
|
2016-11-21 17:47:24 -06:00
|
|
|
fs.phone,
|
|
|
|
fs.email,
|
|
|
|
fs.address,
|
|
|
|
fs._customers,
|
2015-12-06 18:04:23 -06:00
|
|
|
])
|
2013-05-07 19:41:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
class PeopleAutocomplete(AutocompleteView):
|
|
|
|
|
2015-12-06 18:04:23 -06:00
|
|
|
mapped_class = model.Person
|
2016-12-08 12:13:45 -06:00
|
|
|
fieldname = 'display_name'
|
2013-05-07 19:41:58 -05:00
|
|
|
|
|
|
|
|
2014-07-19 20:49:00 -05:00
|
|
|
class PeopleEmployeesAutocomplete(PeopleAutocomplete):
|
|
|
|
"""
|
|
|
|
Autocomplete view for the Person model, but restricted to return only
|
|
|
|
results for people who are employees.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def filter_query(self, q):
|
|
|
|
return q.join(model.Employee)
|
|
|
|
|
|
|
|
|
2013-05-07 19:41:58 -05:00
|
|
|
def includeme(config):
|
2015-12-06 18:04:23 -06:00
|
|
|
PeopleView.defaults(config)
|
|
|
|
|
|
|
|
# autocomplete
|
|
|
|
config.add_route('people.autocomplete', '/people/autocomplete')
|
2013-05-07 19:41:58 -05:00
|
|
|
config.add_view(PeopleAutocomplete, route_name='people.autocomplete',
|
2015-12-06 18:04:23 -06:00
|
|
|
renderer='json', permission='people.list')
|
|
|
|
config.add_route('people.autocomplete.employees', '/people/autocomplete/employees')
|
|
|
|
config.add_view(PeopleEmployeesAutocomplete, route_name='people.autocomplete.employees',
|
|
|
|
renderer='json', permission='people.list')
|