"""
open/dulcinea/lib/ui/note.qpy
"""
from dulcinea.common import format_date_time, format_user
from dulcinea.note import Note, Notable
from dulcinea.ui.attachment import AttachmentUI
from dulcinea.ui.attachment import format_attachments
from dulcinea.ui.util import popup
from dulcinea.util import format_text, SAFE_TAGS
from qp.fill.directory import Directory
from qp.fill.form import Form
from qp.fill.html import nlnl2brbr
from qp.fill.widget import TextWidget, SubmitWidget
from qp.lib.spec import require
from qp.pub.common import redirect, page
def default_decorate:xml(note, body, title='Note'):
page(title, '
', body, '
')
def format_note:xml(note, path='file/'):
if note:
if note.get_timestamp() and note.get_user():
'%s %s
' % (
format_user(note.get_user(), name=False, email=False),
'(%s)' % (
format_date_time(note.get_timestamp())))
if note.get_text():
'%s
' % (
format_text(nlnl2brbr(note.get_text())))
format_attachments(note, path)
class NotableDirectory(Directory):
def __init__(self, notable, label='admin note', decorate=None):
require(notable, Notable)
self.notable = notable
self.set_note = notable.set_note
self.get_note = notable.get_note
self.note = self.get_note() or Note()
self.label = label
self.decorate = decorate or default_decorate
def get_exports(self):
yield ('', '_q_index', self.label.capitalize(), None)
if self.get_note():
yield ('edit', 'edit', 'Edit ' + self.label, None)
attachment_count = len(self.note.get_attachments())
if attachment_count:
yield ('file', None, 'Files %d' % attachment_count,
'%d File(s) attached to %s' % (attachment_count,
self.label))
else:
yield ('file', None, 'Files',
'No Files attached to %s' % self.label)
yield ('delete', 'delete', 'Delete ' + self.label,
'Delete this note')
else:
yield ('edit', 'edit', 'Add ' + self.label, 'Create new note')
def _q_index(self):
if not self.get_note():
redirect('edit')
return self.decorate(self.note, popup(format_note(self.note)),
title='View ' + self.label)
def edit(self):
form = Form()
form.add(TextWidget, 'text', value=self.note.get_text(),
hint=("Enter or add the text of your note. Although not "
"required, you may also wish to include the "
"following HTML markup tags to improve "
"readability: %s. All other HTML markup will "
"be displayed 'as is'. Finally, Things that "
"look like email addresses and URLs will be "
"turned into hyperlinks (e.g. things that begin "
"with 'http://' or 'mailto:'."
% ', '.join(SAFE_TAGS)),
rows=30, cols=100)
form.add(SubmitWidget, 'save', 'Save')
form.add(SubmitWidget, 'cancel', 'Cancel')
if form.get('cancel'):
redirect('.')
if not form.is_submitted():
return self.decorate(self.notable, form.render(),
'Edit ' + self.label)
self.note.set_text(form['text'])
self.note.set_timestamp()
self.note.set_user()
if self.get_note() is None and not self.note.is_empty():
self.set_note(self.note)
elif self.get_note() is not None and self.note.is_empty():
self.set_note(None)
redirect('.')
def delete:xml(self):
form = Form()
form.add(SubmitWidget, 'delete', 'Delete')
form.add(SubmitWidget, 'cancel', 'Cancel')
if form.get('cancel'):
redirect('.')
if not form.is_submitted():
return page('Confirm Delete',
'' % self.label,
form.render())
if form.get('delete'):
self.set_note(None)
redirect('.')
def _q_lookup(self, component):
def _note_file_decorate(attachable, body, title):
return self.decorate(attachable, body, title + ' for ' + self.label)
if component == 'file':
return AttachmentUI(self.note, decorate=_note_file_decorate)