improve product views (plus temp hacks)

This commit is contained in:
Lance Edgar 2012-07-26 14:04:54 -07:00
parent 36c969c7f8
commit b4193db0f2
2 changed files with 243 additions and 132 deletions

View file

@ -1,7 +1,20 @@
<%inherit file="/products/base.mako" />
<%inherit file="/index.mako" />
<%def name="menu()">
<p>${h.link_to("New Batch from Results", url('products.batch'))}</p>
</%def>
<%def name="title()">Products</%def>
<%def name="head_tags()">
${parent.head_tags()}
<style type="text/css">
div.grid.Product table tbody td.size,
div.grid.Product table tbody td.regular_price,
div.grid.Product table tbody td.sale_price {
padding-right: 6px;
text-align: right;
}
</style>
</%def>
${parent.body()}

View file

@ -26,148 +26,246 @@
``rattail.pyramid.views.products`` -- Product Views
"""
from sqlalchemy.orm import joinedload
import transaction
from pyramid.httpexceptions import HTTPFound
from pyramid.view import view_config
from edbob.pyramid import filters
from edbob.pyramid import forms
from edbob.pyramid import grids
from edbob.pyramid import Session
from edbob.pyramid.filters import filter_ilike
from edbob.pyramid.grids import sorter
from edbob.pyramid.views import GridView
from edbob.pyramid.views.crud import Crud
import rattail
from rattail.batches import next_batch_id
from rattail.pyramid import util
from rattail.pyramid.forms import UpcFieldRenderer
from rattail.pyramid.forms import (
UpcFieldRenderer, RegularPriceFieldRenderer, SalePriceFieldRenderer)
def filter_map():
return filters.get_filter_map(
rattail.Product,
exact=['upc'],
ilike=['description', 'size'],
department=filters.filter_ilike(rattail.Department.name),
brand=filters.filter_ilike(rattail.Brand.name))
class ProductGrid(GridView):
def search_config(request, fmap):
return filters.get_search_config(
'products.list', request, fmap,
include_filter_brand=True,
filter_type_brand='lk',
include_filter_description=True,
filter_type_description='lk',
include_filter_department=True,
filter_type_department='lk')
mapped_class = rattail.Product
route_name = 'products.list'
route_prefix = 'product'
def search_form(config):
return filters.get_search_form(
config, upc="UPC")
def join_map(self):
return {
'brand':
lambda q: q.outerjoin(rattail.Brand),
'department':
lambda q: q.outerjoin(rattail.Department),
}
def grid_config(request, search, fmap):
return grids.get_grid_config(
'products.list', request, search,
filter_map=fmap, sort='description')
def filter_map(self):
return self.make_filter_map(
exact=['upc'],
ilike=['description', 'size'],
brand=filter_ilike(rattail.Brand.name),
department=filter_ilike(rattail.Department.name))
def sort_map():
return grids.get_sort_map(
rattail.Product,
['upc', 'description', 'size'],
department=grids.sorter(rattail.Department.name),
brand=grids.sorter(rattail.Brand.name))
def search_config(self, fmap):
return self.make_search_config(
fmap,
include_filter_upc=True,
filter_type_upc='eq',
include_filter_brand=True,
filter_type_brand='lk',
include_filter_description=True,
filter_type_description='lk',
include_filter_department=True,
filter_type_department='lk')
def query(config):
jmap = {
'department': lambda q: q.outerjoin(rattail.Department),
'brand': lambda q: q.outerjoin(rattail.Brand),
}
smap = sort_map()
q = Session.query(rattail.Product)
q = q.options(joinedload(rattail.Product.department))
q = q.options(joinedload(rattail.Product.brand))
q = filters.filter_query(q, config, jmap)
q = grids.sort_query(q, config, smap, jmap)
return q
def search_form(self, config):
return self.make_search_form(
config, upc="UPC")
def grid_config(self, search, fmap):
kwargs = {}
if self.request.has_perm('products.delete'):
kwargs['deletable'] = True
return self.make_grid_config(
search, fmap, sort='description', **kwargs)
def sort_map(self):
return self.make_sort_map(
'upc', 'description', 'size', 'regular_price', 'sale_price',
brand=sorter(rattail.Brand.name),
department=sorter(rattail.Department.name))
def grid(self, data, config):
g = self.make_grid(data, config)
g.upc.set(renderer=UpcFieldRenderer)
g.regular_price.set(renderer=RegularPriceFieldRenderer)
g.sale_price.set(renderer=SalePriceFieldRenderer)
g.configure(
include=[
g.upc.label("UPC"),
g.brand,
g.description,
g.size,
g.department,
g.regular_price.label("Reg Price"),
g.sale_price.label("Sale Price"),
],
readonly=True)
return g
@view_config(route_name='products.list', renderer='/products/index.mako')
def products(context, request):
class ProductCrud(Crud):
fmap = filter_map()
config = search_config(request, fmap)
search = search_form(config)
config = grid_config(request, search, fmap)
products = grids.get_pager(query, config)
mapped_class = rattail.Product
home_route = 'products.list'
g = forms.AlchemyGrid(
rattail.Product, products, config,
gridurl=request.route_url('products.list'))
g.configure(
include=[
g.upc.with_renderer(UpcFieldRenderer).label("UPC"),
g.brand,
g.description,
g.size,
g.department,
],
readonly=True)
grid = g.render(class_='clickable products')
return grids.render_grid(request, grid, search)
@view_config(route_name='products.batch')
def batch(context, request):
fmap = filter_map()
config = search_config(request, fmap)
search = search_form(config)
config = grid_config(request, search, fmap)
products = query(config)
home = HTTPFound(location=request.route_url('products.list'))
source = util.get_terminal('rattail')
if not source:
return home
dct = util.get_dictionary('ITEM_DCT')
if not dct:
return home
with transaction.manager:
batch = rattail.Batch()
Session.add(batch)
batch.source = source
batch.source_description = source.description
batch.batch_id = next_batch_id(source.sil_id, consume=True,
session=Session())
batch.name = '%s.%08u' % (source.sil_id, batch.batch_id)
batch.dictionary = dct
batch.action_type = rattail.BATCH_ADD
batch.description = "products from Rattail"
for i, col in enumerate(source.source_columns(dct), 1):
batch.columns.append(rattail.BatchColumn(
ordinal=i,
sil_column=col.sil_column,
source=source,
targeted=True,
))
batch.create_table()
batch.add_rows(source, dct, query=products)
batch.rowcount = products.count()
url = request.route_url('batch.edit', uuid=batch.uuid)
return HTTPFound(location=url)
def fieldset(self, obj):
fs = self.make_fieldset(obj)
fs.configure(
include=[
fs.description,
])
return fs
def includeme(config):
config.add_route('products.list', '/products')
config.add_route('products.batch', '/products/batch')
config.scan(__name__)
ProductGrid.add_route(config, 'products.list', '/products')
ProductCrud.add_routes(config)
# from sqlalchemy.orm import joinedload
# import transaction
# from pyramid.httpexceptions import HTTPFound
# from pyramid.view import view_config
# from edbob.pyramid import filters
# from edbob.pyramid import forms
# from edbob.pyramid import grids
# from edbob.pyramid import Session
# import rattail
# from rattail.batches import next_batch_id
# from rattail.pyramid import util
# from rattail.pyramid.forms import UpcFieldRenderer
# def filter_map():
# return filters.get_filter_map(
# rattail.Product,
# exact=['upc'],
# ilike=['description', 'size'],
# department=filters.filter_ilike(rattail.Department.name),
# brand=filters.filter_ilike(rattail.Brand.name))
# def search_config(request, fmap):
# return filters.get_search_config(
# 'products.list', request, fmap,
# include_filter_brand=True,
# filter_type_brand='lk',
# include_filter_description=True,
# filter_type_description='lk',
# include_filter_department=True,
# filter_type_department='lk')
# def search_form(config):
# return filters.get_search_form(
# config, upc="UPC")
# def grid_config(request, search, fmap):
# return grids.get_grid_config(
# 'products.list', request, search,
# filter_map=fmap, sort='description')
# def sort_map():
# return grids.get_sort_map(
# rattail.Product,
# ['upc', 'description', 'size'],
# department=grids.sorter(rattail.Department.name),
# brand=grids.sorter(rattail.Brand.name))
# def query(config):
# jmap = {
# 'department': lambda q: q.outerjoin(rattail.Department),
# 'brand': lambda q: q.outerjoin(rattail.Brand),
# }
# smap = sort_map()
# q = Session.query(rattail.Product)
# q = q.options(joinedload(rattail.Product.department))
# q = q.options(joinedload(rattail.Product.brand))
# q = filters.filter_query(q, config, jmap)
# q = grids.sort_query(q, config, smap, jmap)
# return q
# @view_config(route_name='products.list', renderer='/products/index.mako')
# def products(context, request):
# fmap = filter_map()
# config = search_config(request, fmap)
# search = search_form(config)
# config = grid_config(request, search, fmap)
# products = grids.get_pager(query, config)
# g = forms.AlchemyGrid(
# rattail.Product, products, config,
# gridurl=request.route_url('products.list'))
# g.configure(
# include=[
# g.upc.with_renderer(UpcFieldRenderer).label("UPC"),
# g.brand,
# g.description,
# g.size,
# g.department,
# ],
# readonly=True)
# grid = g.render(class_='clickable products')
# return grids.render_grid(request, grid, search)
# @view_config(route_name='products.batch')
# def batch(context, request):
# fmap = filter_map()
# config = search_config(request, fmap)
# search = search_form(config)
# config = grid_config(request, search, fmap)
# products = query(config)
# home = HTTPFound(location=request.route_url('products.list'))
# source = util.get_terminal('rattail')
# if not source:
# return home
# dct = util.get_dictionary('ITEM_DCT')
# if not dct:
# return home
# with transaction.manager:
# batch = rattail.Batch()
# Session.add(batch)
# batch.source = source
# batch.source_description = source.description
# batch.batch_id = next_batch_id(source.sil_id, consume=True,
# session=Session())
# batch.name = '%s.%08u' % (source.sil_id, batch.batch_id)
# batch.dictionary = dct
# batch.action_type = rattail.BATCH_ADD
# batch.description = "products from Rattail"
# for i, col in enumerate(source.source_columns(dct), 1):
# batch.columns.append(rattail.BatchColumn(
# ordinal=i,
# sil_column=col.sil_column,
# source=source,
# targeted=True,
# ))
# batch.create_table()
# batch.add_rows(source, dct, query=products)
# batch.rowcount = products.count()
# url = request.route_url('batch.edit', uuid=batch.uuid)
# return HTTPFound(location=url)
# def includeme(config):
# config.add_route('products.list', '/products')
# config.add_route('products.batch', '/products/batch')
# config.scan(__name__)