From 4949ec8818ecb6cf15f24d5b78cb9e07ee767970 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 10 Aug 2012 14:18:34 -0700 Subject: [PATCH] add customer views --- rattail/pyramid/templates/customers/crud.mako | 7 ++ .../pyramid/templates/customers/index.mako | 5 + rattail/pyramid/templates/customers/read.mako | 49 ++++++++ rattail/pyramid/views/customers.py | 118 ++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 rattail/pyramid/templates/customers/crud.mako create mode 100644 rattail/pyramid/templates/customers/index.mako create mode 100644 rattail/pyramid/templates/customers/read.mako create mode 100644 rattail/pyramid/views/customers.py diff --git a/rattail/pyramid/templates/customers/crud.mako b/rattail/pyramid/templates/customers/crud.mako new file mode 100644 index 00000000..c8393e94 --- /dev/null +++ b/rattail/pyramid/templates/customers/crud.mako @@ -0,0 +1,7 @@ +<%inherit file="/crud.mako" /> + +<%def name="context_menu_items()"> +

${h.link_to("Back to Customers", url('customers'))}

+ + +${parent.body()} diff --git a/rattail/pyramid/templates/customers/index.mako b/rattail/pyramid/templates/customers/index.mako new file mode 100644 index 00000000..a73d411c --- /dev/null +++ b/rattail/pyramid/templates/customers/index.mako @@ -0,0 +1,5 @@ +<%inherit file="/index.mako" /> + +<%def name="title()">Customers + +${parent.body()} diff --git a/rattail/pyramid/templates/customers/read.mako b/rattail/pyramid/templates/customers/read.mako new file mode 100644 index 00000000..261765a3 --- /dev/null +++ b/rattail/pyramid/templates/customers/read.mako @@ -0,0 +1,49 @@ +<%inherit file="/customers/crud.mako" /> + +${parent.body()} + +

People

+% if fieldset.model.people: +

Customer account is associated with the following people:

+
+ + + + + + + % for i, person in enumerate(fieldset.model.people, 1): + + + + + % endfor + +
First NameLast Name
${person.first_name or ''}${person.last_name or ''}
+
+% else: +

Customer account is not associated with any people.

+% endif + +

Groups

+% if fieldset.model.groups: +

Customer account belongs to the following groups:

+
+ + + + + + + % for i, group in enumerate(fieldset.model.groups, 1): + + + + + % endfor + +
IDName
${group.id}${group.name or ''}
+
+% else: +

Customer account doesn't belong to any groups.

+% endif diff --git a/rattail/pyramid/views/customers.py b/rattail/pyramid/views/customers.py new file mode 100644 index 00000000..c3365790 --- /dev/null +++ b/rattail/pyramid/views/customers.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2012 Lance Edgar +# +# 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 . +# +################################################################################ + +""" +``rattail.pyramid.views.customers`` -- Customer Views +""" + +from sqlalchemy import and_ + +from edbob.pyramid.views import SearchableAlchemyGridView +from edbob.pyramid.views.crud import Crud + +import rattail + + +class CustomersGrid(SearchableAlchemyGridView): + + mapped_class = rattail.Customer + route_name = 'customers' + route_url = '/customers' + renderer = '/customers/index.mako' + permission = 'customers.list' + sort = 'name' + clickable = True + + def join_map(self): + return { + 'email': + lambda q: q.outerjoin(rattail.CustomerEmailAddress, and_( + rattail.CustomerEmailAddress.parent_uuid == rattail.Customer.uuid, + rattail.CustomerEmailAddress.preference == 1)), + 'phone': + lambda q: q.outerjoin(rattail.CustomerPhoneNumber, and_( + rattail.CustomerPhoneNumber.parent_uuid == rattail.Customer.uuid, + rattail.CustomerPhoneNumber.preference == 1)), + } + + def filter_map(self): + return self.make_filter_map( + exact=['id'], + ilike=['name'], + email=self.filter_ilike(rattail.CustomerEmailAddress.address), + phone=self.filter_ilike(rattail.CustomerPhoneNumber.number)) + + def filter_config(self): + return self.make_filter_config( + include_filter_name=True, + filter_type_name='lk', + filter_label_phone="Phone Number", + filter_label_email="Email Address", + filter_label_id="ID") + + def sort_map(self): + return self.make_sort_map( + 'id', 'name', + email=self.sorter(rattail.CustomerEmailAddress.address), + phone=self.sorter(rattail.CustomerPhoneNumber.number)) + + def grid(self): + g = self.make_grid() + g.configure( + include=[ + g.id.label("ID"), + g.name, + g.phone.label("Phone Number"), + g.email.label("Email Address"), + ], + readonly=True) + + def attrs(row, i): + return {'onclick': "location.href = '%s';" + % self.request.route_url('customer.read', uuid=row.uuid)} + + g.row_attrs = attrs + return g + + +class CustomerCrud(Crud): + + mapped_class = rattail.Customer + home_route = 'customers' + + def fieldset(self, obj): + fs = self.make_fieldset(obj) + fs.configure( + include=[ + fs.id.label("ID"), + fs.name, + fs.phone, + fs.email, + ]) + return fs + + +def includeme(config): + CustomersGrid.add_route(config) + CustomerCrud.add_routes(config)