From 1ee76878d9853cae1723e8bd5f51230fa1df2107 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 6 Jun 2019 13:49:59 -0500 Subject: [PATCH] Various things to support "notes management" from person profile view --- tailbone/grids/core.py | 4 +- tailbone/templates/grids/b-table.mako | 8 +- tailbone/templates/grids/buefy.mako | 1 + tailbone/views/people.py | 127 ++++++++++++++++++-------- 4 files changed, 100 insertions(+), 40 deletions(-) diff --git a/tailbone/grids/core.py b/tailbone/grids/core.py index c44c56cf..439e4ea9 100644 --- a/tailbone/grids/core.py +++ b/tailbone/grids/core.py @@ -1272,12 +1272,14 @@ class GridAction(object): 'actions' column when rendering the grid. """ - def __init__(self, key, label=None, url='#', icon=None, target=None): + def __init__(self, key, label=None, url='#', icon=None, target=None, + click_handler=None): self.key = key self.label = label or prettify(key) self.icon = icon self.url = url self.target = target + self.click_handler = click_handler def get_url(self, row, i): """ diff --git a/tailbone/templates/grids/b-table.mako b/tailbone/templates/grids/b-table.mako index 90f4fdb2..269154aa 100644 --- a/tailbone/templates/grids/b-table.mako +++ b/tailbone/templates/grids/b-table.mako @@ -20,9 +20,15 @@ % if grid.main_actions or grid.more_actions: % for action in grid.main_actions: - + + ${action.label} +   % endfor % endif diff --git a/tailbone/templates/grids/buefy.mako b/tailbone/templates/grids/buefy.mako index 1f20d1dd..1cc12910 100644 --- a/tailbone/templates/grids/buefy.mako +++ b/tailbone/templates/grids/buefy.mako @@ -148,6 +148,7 @@ ${action.label} +   % endfor % endif diff --git a/tailbone/views/people.py b/tailbone/views/people.py index 748cf0a6..7f0c9826 100644 --- a/tailbone/views/people.py +++ b/tailbone/views/people.py @@ -252,11 +252,25 @@ class PeopleView(MasterView): template = 'view_profile_buefy' if use_buefy else 'view_profile' return self.render_to_response(template, context) - def valid_note_types(self): - # TODO: should return something by default? - return {} + def make_note_form(self, mode, person): + schema = NoteSchema().bind(session=self.Session(), + person_uuid=person.uuid) + if mode == 'create': + del schema['uuid'] + form = forms.Form(schema=schema, request=self.request) + return form - def profile_make_note(self, person, form): + def profile_add_note(self): + person = self.get_instance() + form = self.make_note_form('create', person) + if form.validate(newstyle=True): + note = self.create_note(person, form) + self.Session.flush() + return self.profile_add_note_success(note) + else: + return self.profile_add_note_failure(person, form) + + def create_note(self, person, form): note = model.PersonNote() note.type = form.validated['note_type'] note.subject = form.validated['note_subject'] @@ -265,23 +279,54 @@ class PeopleView(MasterView): person.notes.append(note) return note - def profile_add_note(self): - person = self.get_instance() - schema = AddNote().bind(note_types=self.valid_note_types()) - form = forms.Form(schema=schema, request=self.request) - if form.validate(newstyle=True): - note = self.profile_make_note(person, form) - self.Session.flush() - return self.profile_add_note_success(note) - else: - return self.profile_add_note_failure(person, form) - def profile_add_note_success(self, note): return self.redirect(self.get_action_url('view_profile', person)) def profile_add_note_failure(self, person, form): return self.redirect(self.get_action_url('view_profile', person)) + def profile_edit_note(self): + person = self.get_instance() + form = self.make_note_form('edit', person) + if form.validate(newstyle=True): + note = self.update_note(person, form) + self.Session.flush() + return self.profile_edit_note_success(note) + else: + return self.profile_edit_note_failure(person, form) + + def update_note(self, person, form): + note = self.Session.query(model.PersonNote).get(form.validated['uuid']) + note.subject = form.validated['note_subject'] + note.text = form.validated['note_text'] + return note + + def profile_edit_note_success(self, note): + return self.redirect(self.get_action_url('view_profile', person)) + + def profile_edit_note_failure(self, person, form): + return self.redirect(self.get_action_url('view_profile', person)) + + def profile_delete_note(self): + person = self.get_instance() + form = self.make_note_form('delete', person) + if form.validate(newstyle=True): + self.delete_note(person, form) + self.Session.flush() + return self.profile_delete_note_success(person) + else: + return self.profile_delete_note_failure(person, form) + + def delete_note(self, person, form): + note = self.Session.query(model.PersonNote).get(form.validated['uuid']) + self.Session.delete(note) + + def profile_delete_note_success(self, person): + return self.redirect(self.get_action_url('view_profile', person)) + + def profile_delete_note_failure(self, person, form): + return self.redirect(self.get_action_url('view_profile', person)) + def make_user(self): uuid = self.request.POST['person_uuid'] person = self.Session.query(model.Person).get(uuid) @@ -326,26 +371,26 @@ class PeopleView(MasterView): # add note config.add_tailbone_permission(permission_prefix, '{}.profile_add_note'.format(permission_prefix), "Add new {} note from profile view".format(model_title)) - config.add_route('{}.profile_add_note'.format(route_prefix), '{}/{{{}}}/profile/add-note'.format(url_prefix, model_key), + config.add_route('{}.profile_add_note'.format(route_prefix), '{}/{{{}}}/profile/new-note'.format(url_prefix, model_key), request_method='POST') config.add_view(cls, attr='profile_add_note', route_name='{}.profile_add_note'.format(route_prefix), permission='{}.profile_add_note'.format(permission_prefix)) - # # edit note - # config.add_tailbone_permission(permission_prefix, '{}.profile_edit_note'.format(permission_prefix), - # "Edit existing {} note from profile view".format(model_title)) - # config.add_route('{}.profile_edit_note'.format(route_prefix), '{}/{{{}}}/profile/edit-note/{{note_uuid}}'.format(url_prefix, model_key), - # request_method='POST') - # config.add_view(cls, 'profile_edit_note', route_name='{}.profile_edit_note'.format(route_prefix), - # permission='{}.profile_edit_note'.format(permission_prefix)) + # edit note + config.add_tailbone_permission(permission_prefix, '{}.profile_edit_note'.format(permission_prefix), + "Edit existing {} note from profile view".format(model_title)) + config.add_route('{}.profile_edit_note'.format(route_prefix), '{}/{{{}}}/profile/edit-note'.format(url_prefix, model_key), + request_method='POST') + config.add_view(cls, attr='profile_edit_note', route_name='{}.profile_edit_note'.format(route_prefix), + permission='{}.profile_edit_note'.format(permission_prefix)) - # # delete note - # config.add_tailbone_permission(permission_prefix, '{}.profile_delete_note'.format(permission_prefix), - # "Delete existing {} note from profile view".format(model_title)) - # config.add_route('{}.profile_delete_note'.format(route_prefix), '{}/{{{}}}/profile/delete-note/{{note_uuid}}'.format(url_prefix, model_key), - # request_method='POST') - # config.add_view(cls, 'profile_delete_note', route_name='{}.profile_delete_note'.format(route_prefix), - # permission='{}.profile_delete_note'.format(permission_prefix)) + # delete note + config.add_tailbone_permission(permission_prefix, '{}.profile_delete_note'.format(permission_prefix), + "Delete existing {} note from profile view".format(model_title)) + config.add_route('{}.profile_delete_note'.format(route_prefix), '{}/{{{}}}/profile/delete-note'.format(url_prefix, model_key), + request_method='POST') + config.add_view(cls, attr='profile_delete_note', route_name='{}.profile_delete_note'.format(route_prefix), + permission='{}.profile_delete_note'.format(permission_prefix)) # make user for person config.add_route('{}.make_user'.format(route_prefix), '{}/make-user'.format(url_prefix), @@ -428,19 +473,25 @@ class PersonNoteView(MasterView): @colander.deferred -def valid_note_type(node, kw): - note_types = kw['note_types'] +def valid_note_uuid(node, kw): + session = kw['session'] + person_uuid = kw['person_uuid'] def validate(node, value): - if value not in note_types: - raise colander.Invalid(node, "Invalid note type") - return value + note = session.query(model.PersonNote).get(value) + if not note: + raise colander.Invalid(node, "Note not found") + if note.person.uuid != person_uuid: + raise colander.Invalid(node, "Note is for the wrong person") + return note.uuid return validate -class AddNote(colander.Schema): +class NoteSchema(colander.Schema): - note_type = colander.SchemaNode(colander.String(), - validator=valid_note_type) + uuid = colander.SchemaNode(colander.String(), + validator=valid_note_uuid) + + note_type = colander.SchemaNode(colander.String()) note_subject = colander.SchemaNode(colander.String(), missing='')