tailbone-harvest/tailbone_harvest/views/harvest/users.py

111 lines
3.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2022 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 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Harvest User views
"""
from rattail_harvest.db.model import HarvestUser
import colander
from tailbone import forms
from .master import HarvestMasterView
class HarvestUserView(HarvestMasterView):
"""
Master view for Harvest Users
"""
model_class = HarvestUser
url_prefix = '/harvest/users'
route_prefix = 'harvest.users'
grid_columns = [
'id',
'first_name',
'last_name',
'email',
'telephone',
'timezone',
'is_admin',
]
def configure_grid(self, g):
super(HarvestUserView, self).configure_grid(g)
model = self.model
g.set_joiner('person_name', lambda q: q.outerjoin(model.Person))
g.set_filter('person_name', model.Person.display_name)
g.set_sort_defaults('first_name')
g.set_link('id')
g.set_link('first_name')
g.set_link('last_name')
g.set_link('email')
def configure_form(self, f):
super(HarvestUserView, self).configure_form(f)
model = self.model
user = f.model_instance
# person
f.set_renderer('person', self.render_person)
if self.creating or self.editing:
if 'person' in f.fields:
f.remove('person_uuid')
f.replace('person', 'person_uuid')
person_display = ""
if self.request.method == 'POST':
if self.request.POST.get('person_uuid'):
person = self.Session.query(model.Person).get(self.request.POST['person_uuid'])
if person:
person_display = str(person)
elif self.editing:
person_display = str(user.person or '')
people_url = self.request.route_url('people.autocomplete')
f.set_widget('person_uuid', forms.widgets.JQueryAutocompleteWidget(
field_display=person_display, service_url=people_url))
f.set_validator('person_uuid', self.valid_person)
f.set_label('person_uuid', "Person")
# timestamps
if self.creating or self.editing:
f.remove('created_at')
f.remove('updated_at')
# time_entries
# TODO: should add this as child rows/grid instead
f.remove('time_entries')
def valid_person(self, node, value):
model = self.model
if value:
person = self.Session.query(model.Person).get(value)
if not person:
raise colander.Invalid(node, "Person not found (you must *select* a record)")
def includeme(config):
HarvestUserView.defaults(config)