Expose HarvestUser.person
for editing
This commit is contained in:
parent
b8fd9c3022
commit
a270d1dcc2
|
@ -26,6 +26,9 @@ Harvest User views
|
|||
|
||||
from rattail_harvest.db.model import HarvestUser
|
||||
|
||||
import colander
|
||||
|
||||
from tailbone import forms
|
||||
from .master import HarvestMasterView
|
||||
|
||||
|
||||
|
@ -49,6 +52,10 @@ class HarvestUserView(HarvestMasterView):
|
|||
|
||||
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')
|
||||
|
||||
|
@ -59,10 +66,45 @@ class HarvestUserView(HarvestMasterView):
|
|||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue