tailbone/rattail/pyramid/views/batches/core.py
Lance Edgar cf10fe19e8 extensive commit (see note)
The following changes are included:

- Added support for GPC data type.

- Added eager import of ``rattail.sil`` in ``before_render`` hook.

- Removed ``rattail.pyramid.util`` module.

- Added initial batch support: views, templates, creation from Product grid.

- Added batch params template for ``PrintLabels`` provider.

- Added support for ``rattail.LabelProfile`` class.

- Improved Product grid to include filter/sort on Vendor.
2012-08-29 11:37:17 -07:00

175 lines
5.6 KiB
Python

#!/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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``rattail.pyramid.views.batches.core`` -- Core Batch Views
"""
import threading
from pyramid.httpexceptions import HTTPFound
from pyramid.renderers import render_to_response
from webhelpers.html import tags
import edbob
from edbob.pyramid import Session
from edbob.pyramid.forms import EnumFieldRenderer
from edbob.pyramid.progress import SessionProgress
from edbob.pyramid.views import SearchableAlchemyGridView, CrudView, View
import rattail
from rattail import batches
class BatchesGrid(SearchableAlchemyGridView):
mapped_class = rattail.Batch
config_prefix = 'batches'
sort = 'id'
def filter_map(self):
return self.make_filter_map(
exact=['id'],
ilike=['source', 'destination', 'description'])
def filter_config(self):
return self.make_filter_config(
filter_label_id="ID")
def sort_map(self):
return self.make_sort_map('source', 'id', 'destination', 'description')
def query(self):
q = self.make_query()
q = q.filter(rattail.Batch.executed == None)
return q
def grid(self):
g = self.make_grid()
g.configure(
include=[
g.source,
g.id.label("ID"),
g.destination,
g.description,
g.rowcount.label("Row Count"),
],
readonly=True)
if self.request.has_perm('batches.read'):
def rows(row):
return tags.link_to("View Rows", self.request.route_url(
'batch.rows', uuid=row.uuid))
g.add_column('rows', "", rows)
g.clickable = True
g.click_route_name = 'batch'
if self.request.has_perm('batches.update'):
g.editable = True
g.edit_route_name = 'batch.update'
if self.request.has_perm('batches.delete'):
g.deletable = True
g.delete_route_name = 'batch.delete'
return g
class BatchCrud(CrudView):
mapped_class = rattail.Batch
home_route = 'batches'
def fieldset(self, model):
fs = self.make_fieldset(model)
fs.action_type.set(renderer=EnumFieldRenderer(rattail.BATCH_ACTION))
fs.configure(
include=[
fs.source,
fs.id.label("ID"),
fs.destination,
fs.action_type,
fs.description,
fs.rowcount.label("Row Count").readonly(),
])
return fs
def post_delete(self, batch):
batch.drop_table()
class ExecuteBatch(View):
def execute_batch(self, batch):
session = edbob.Session()
batch = session.merge(batch)
progress = SessionProgress(self.request.session, 'batch.execute')
progress.session['success_msg'] = "Batch \"%s\" has been executed." % batch.description
progress.session['success_url'] = self.request.route_url('batches')
if batch.execute(progress):
session.commit()
else:
session.rollback()
session.close()
def __call__(self):
uuid = self.request.matchdict['uuid']
batch = Session.query(rattail.Batch).get(uuid) if uuid else None
if not batch:
return HTTPFound(location=self.request.route_url('batches'))
thread = threading.Thread(target=self.execute_batch, args=(batch,))
thread.start()
kwargs = {
'key': 'batch.execute',
'cancel_url': self.request.route_url('batch.rows', uuid=batch.uuid),
'cancel_msg': "Batch execution was cancelled.",
}
return render_to_response('/progress.mako', kwargs, request=self.request)
def includeme(config):
config.add_route('batches', '/batches')
config.add_view(BatchesGrid, route_name='batches',
renderer='/batches/index.mako',
permission='batches.list')
config.add_route('batch', '/batches/{uuid}')
config.add_view(BatchCrud, attr='read', route_name='batch',
renderer='/batches/read.mako',
permission='batches.read')
config.add_route('batch.update', '/batches/{uuid}/edit')
config.add_view(BatchCrud, attr='update', route_name='batch.update',
renderer='/batches/crud.mako',
permission='batches.update')
config.add_route('batch.delete', '/batches/{uuid}/delete')
config.add_view(BatchCrud, attr='delete', route_name='batch.delete',
permission='batches.delete')
config.add_route('batch.execute', '/batches/{uuid}/execute')
config.add_view(ExecuteBatch, route_name='batch.execute',
permission='batches.execute')