tailbone/tailbone/views/departments.py

165 lines
5.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-04-10 22:28:02 -05:00
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2015 Lance Edgar
2012-04-10 22:28:02 -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
Department Views
2012-04-10 22:28:02 -05:00
"""
from __future__ import unicode_literals
2012-04-10 22:28:02 -05:00
from rattail.db import model
from rattail.db.model import Department, Product, ProductCost, Vendor
2012-04-10 22:28:02 -05:00
from . import SearchableAlchemyGridView, CrudView, AlchemyGridView, AutocompleteView
from .continuum import VersionView, version_defaults
2012-04-10 22:28:02 -05:00
class DepartmentsGrid(SearchableAlchemyGridView):
mapped_class = Department
config_prefix = 'departments'
sort = 'name'
def filter_map(self):
return self.make_filter_map(ilike=['name'])
def filter_config(self):
return self.make_filter_config(
include_filter_name=True,
filter_type_name='lk')
def sort_map(self):
return self.make_sort_map('number', 'name')
def grid(self):
g = self.make_grid()
g.configure(
include=[
g.number,
g.name,
],
readonly=True)
2012-11-18 10:38:11 -06:00
if self.request.has_perm('departments.read'):
g.viewable = True
g.view_route_name = 'department.read'
2012-11-18 10:38:11 -06:00
if self.request.has_perm('departments.update'):
g.editable = True
g.edit_route_name = 'department.update'
if self.request.has_perm('departments.delete'):
g.deletable = True
g.delete_route_name = 'department.delete'
return g
2012-11-18 10:38:11 -06:00
class DepartmentCrud(CrudView):
mapped_class = Department
2012-11-18 10:38:11 -06:00
home_route = 'departments'
def fieldset(self, model):
fs = self.make_fieldset(model)
fs.configure(
include=[
fs.number,
fs.name,
])
return fs
class DepartmentVersionView(VersionView):
"""
View which shows version history for a department.
"""
parent_class = model.Department
route_model_view = 'department.read'
class DepartmentsByVendorGrid(AlchemyGridView):
mapped_class = Department
config_prefix = 'departments.by_vendor'
checkboxes = True
partial_only = True
def query(self):
q = self.make_query()
q = q.outerjoin(Product)
q = q.join(ProductCost)
q = q.join(Vendor)
q = q.filter(Vendor.uuid == self.request.params['uuid'])
q = q.distinct()
q = q.order_by(Department.name)
2012-04-10 22:28:02 -05:00
return q
def grid(self):
g = self.make_grid()
g.configure(
include=[
g.name,
],
readonly=True)
return g
2012-04-16 00:51:12 -05:00
2012-10-04 10:40:27 -05:00
class DepartmentsAutocomplete(AutocompleteView):
mapped_class = Department
2012-10-04 10:40:27 -05:00
fieldname = 'name'
def add_routes(config):
config.add_route('departments', '/departments')
config.add_route('departments.autocomplete', '/departments/autocomplete')
config.add_route('departments.by_vendor', '/departments/by-vendor')
config.add_route('department.create', '/departments/new')
config.add_route('department.read', '/departments/{uuid}')
config.add_route('department.update', '/departments/{uuid}/edit')
config.add_route('department.delete', '/departments/{uuid}/delete')
2012-04-10 22:28:02 -05:00
def includeme(config):
add_routes(config)
# list
config.add_view(DepartmentsGrid, route_name='departments',
renderer='/departments/index.mako', permission='departments.list')
# autocomplete
config.add_view(DepartmentsAutocomplete, route_name='departments.autocomplete',
renderer='json', permission='departments.list')
2012-10-04 10:40:27 -05:00
# departments by vendor list
config.add_view(DepartmentsByVendorGrid,route_name='departments.by_vendor',
permission='departments.list')
2012-11-18 10:38:11 -06:00
# crud
config.add_view(DepartmentCrud, attr='create', route_name='department.create',
renderer='/departments/crud.mako', permission='departments.create')
config.add_view(DepartmentCrud, attr='read', route_name='department.read',
renderer='/departments/crud.mako', permission='departments.read')
config.add_view(DepartmentCrud, attr='update', route_name='department.update',
renderer='/departments/crud.mako', permission='departments.update')
config.add_view(DepartmentCrud, attr='delete', route_name='department.delete',
2012-11-18 10:38:11 -06:00
permission='departments.delete')
version_defaults(config, DepartmentVersionView, 'department')