Various things to support "notes management" from person profile view

This commit is contained in:
Lance Edgar 2019-06-06 13:49:59 -05:00
parent 6749604210
commit 1ee76878d9
4 changed files with 100 additions and 40 deletions

View file

@ -1272,12 +1272,14 @@ class GridAction(object):
'actions' column when rendering the grid. '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.key = key
self.label = label or prettify(key) self.label = label or prettify(key)
self.icon = icon self.icon = icon
self.url = url self.url = url
self.target = target self.target = target
self.click_handler = click_handler
def get_url(self, row, i): def get_url(self, row, i):
""" """

View file

@ -20,9 +20,15 @@
% if grid.main_actions or grid.more_actions: % if grid.main_actions or grid.more_actions:
<b-table-column field="actions" label="Actions"> <b-table-column field="actions" label="Actions">
% for action in grid.main_actions: % for action in grid.main_actions:
<a :href="props.row._action_url_${action.key}"><i class="fas fa-${action.icon}"></i> <a :href="props.row._action_url_${action.key}"
% if action.click_handler:
@click.prevent="${action.click_handler}"
% endif
>
<i class="fas fa-${action.icon}"></i>
${action.label} ${action.label}
</a> </a>
&nbsp;
% endfor % endfor
</b-table-column> </b-table-column>
% endif % endif

View file

@ -148,6 +148,7 @@
<a :href="props.row._action_url_${action.key}"><i class="fas fa-${action.icon}"></i> <a :href="props.row._action_url_${action.key}"><i class="fas fa-${action.icon}"></i>
${action.label} ${action.label}
</a> </a>
&nbsp;
% endfor % endfor
</b-table-column> </b-table-column>
% endif % endif

View file

@ -252,11 +252,25 @@ class PeopleView(MasterView):
template = 'view_profile_buefy' if use_buefy else 'view_profile' template = 'view_profile_buefy' if use_buefy else 'view_profile'
return self.render_to_response(template, context) return self.render_to_response(template, context)
def valid_note_types(self): def make_note_form(self, mode, person):
# TODO: should return something by default? schema = NoteSchema().bind(session=self.Session(),
return {} 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 = model.PersonNote()
note.type = form.validated['note_type'] note.type = form.validated['note_type']
note.subject = form.validated['note_subject'] note.subject = form.validated['note_subject']
@ -265,23 +279,54 @@ class PeopleView(MasterView):
person.notes.append(note) person.notes.append(note)
return 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): def profile_add_note_success(self, note):
return self.redirect(self.get_action_url('view_profile', person)) return self.redirect(self.get_action_url('view_profile', person))
def profile_add_note_failure(self, person, form): def profile_add_note_failure(self, person, form):
return self.redirect(self.get_action_url('view_profile', person)) 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): def make_user(self):
uuid = self.request.POST['person_uuid'] uuid = self.request.POST['person_uuid']
person = self.Session.query(model.Person).get(uuid) person = self.Session.query(model.Person).get(uuid)
@ -326,26 +371,26 @@ class PeopleView(MasterView):
# add note # add note
config.add_tailbone_permission(permission_prefix, '{}.profile_add_note'.format(permission_prefix), config.add_tailbone_permission(permission_prefix, '{}.profile_add_note'.format(permission_prefix),
"Add new {} note from profile view".format(model_title)) "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') request_method='POST')
config.add_view(cls, attr='profile_add_note', route_name='{}.profile_add_note'.format(route_prefix), config.add_view(cls, attr='profile_add_note', route_name='{}.profile_add_note'.format(route_prefix),
permission='{}.profile_add_note'.format(permission_prefix)) permission='{}.profile_add_note'.format(permission_prefix))
# # edit note # edit note
# config.add_tailbone_permission(permission_prefix, '{}.profile_edit_note'.format(permission_prefix), config.add_tailbone_permission(permission_prefix, '{}.profile_edit_note'.format(permission_prefix),
# "Edit existing {} note from profile view".format(model_title)) "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), config.add_route('{}.profile_edit_note'.format(route_prefix), '{}/{{{}}}/profile/edit-note'.format(url_prefix, model_key),
# request_method='POST') request_method='POST')
# config.add_view(cls, 'profile_edit_note', route_name='{}.profile_edit_note'.format(route_prefix), config.add_view(cls, attr='profile_edit_note', route_name='{}.profile_edit_note'.format(route_prefix),
# permission='{}.profile_edit_note'.format(permission_prefix)) permission='{}.profile_edit_note'.format(permission_prefix))
# # delete note # delete note
# config.add_tailbone_permission(permission_prefix, '{}.profile_delete_note'.format(permission_prefix), config.add_tailbone_permission(permission_prefix, '{}.profile_delete_note'.format(permission_prefix),
# "Delete existing {} note from profile view".format(model_title)) "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), config.add_route('{}.profile_delete_note'.format(route_prefix), '{}/{{{}}}/profile/delete-note'.format(url_prefix, model_key),
# request_method='POST') request_method='POST')
# config.add_view(cls, 'profile_delete_note', route_name='{}.profile_delete_note'.format(route_prefix), config.add_view(cls, attr='profile_delete_note', route_name='{}.profile_delete_note'.format(route_prefix),
# permission='{}.profile_delete_note'.format(permission_prefix)) permission='{}.profile_delete_note'.format(permission_prefix))
# make user for person # make user for person
config.add_route('{}.make_user'.format(route_prefix), '{}/make-user'.format(url_prefix), config.add_route('{}.make_user'.format(route_prefix), '{}/make-user'.format(url_prefix),
@ -428,19 +473,25 @@ class PersonNoteView(MasterView):
@colander.deferred @colander.deferred
def valid_note_type(node, kw): def valid_note_uuid(node, kw):
note_types = kw['note_types'] session = kw['session']
person_uuid = kw['person_uuid']
def validate(node, value): def validate(node, value):
if value not in note_types: note = session.query(model.PersonNote).get(value)
raise colander.Invalid(node, "Invalid note type") if not note:
return value 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 return validate
class AddNote(colander.Schema): class NoteSchema(colander.Schema):
note_type = colander.SchemaNode(colander.String(), uuid = colander.SchemaNode(colander.String(),
validator=valid_note_type) validator=valid_note_uuid)
note_type = colander.SchemaNode(colander.String())
note_subject = colander.SchemaNode(colander.String(), missing='') note_subject = colander.SchemaNode(colander.String(), missing='')