Initial support for adding new PersonNote from profile view

This commit is contained in:
Lance Edgar 2019-06-04 19:23:27 -05:00
parent c24098a117
commit 40ab3cda9c

View file

@ -32,10 +32,11 @@ from sqlalchemy import orm
from rattail.db import model, api
import colander
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from webhelpers2.html import HTML, tags
from tailbone import grids
from tailbone import forms, grids
from tailbone.views import MasterView, AutocompleteView
@ -48,6 +49,7 @@ class PeopleView(MasterView):
route_prefix = 'people'
has_versions = True
supports_mobile = True
manage_notes_from_profile_view = False
grid_columns = [
'display_name',
@ -250,6 +252,36 @@ 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 profile_make_note(self, person, form):
note = model.PersonNote()
note.type = form.validated['note_type']
note.subject = form.validated['note_subject']
note.text = form.validated['note_text']
note.created_by = self.request.user
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 make_user(self):
uuid = self.request.POST['person_uuid']
person = self.Session.query(model.Person).get(uuid)
@ -288,6 +320,33 @@ class PeopleView(MasterView):
config.add_view(cls, attr='view_profile', route_name='{}.view_profile'.format(route_prefix),
permission='{}.view_profile'.format(permission_prefix))
# manage notes from profile view
if cls.manage_notes_from_profile_view:
# 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),
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))
# # 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))
# make user for person
config.add_route('{}.make_user'.format(route_prefix), '{}/make-user'.format(url_prefix),
request_method='POST')
@ -367,6 +426,26 @@ class PersonNoteView(MasterView):
f.set_renderer('person', self.render_person)
@colander.deferred
def valid_note_type(node, kw):
note_types = kw['note_types']
def validate(node, value):
if value not in note_types:
raise colander.Invalid(node, "Invalid note type")
return value
return validate
class AddNote(colander.Schema):
note_type = colander.SchemaNode(colander.String(),
validator=valid_note_type)
note_subject = colander.SchemaNode(colander.String(), missing='')
note_text = colander.SchemaNode(colander.String(), missing='')
def includeme(config):
# autocomplete