Working on note HTML edits
svn: r19685
This commit is contained in:
@@ -161,7 +161,10 @@ class EventForm(forms.ModelForm):
|
||||
class NoteForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Note
|
||||
exclude = ["handle"]
|
||||
exclude = ["handle", "text"]
|
||||
|
||||
notetext = forms.CharField(label="Text",
|
||||
widget=forms.widgets.Textarea(attrs={'rows':'10', 'cols': '80', 'class':'wysiwyg'}))
|
||||
|
||||
class MediaForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
||||
@@ -51,16 +51,17 @@ def process_note(request, context, handle, action, add_to=None): # view, edit, s
|
||||
# Handle: edit, view, add, create, save, delete
|
||||
if action == "add":
|
||||
note = Note(gramps_id=dji.get_next_id(Note, "N"))
|
||||
noteform = NoteForm(instance=note)
|
||||
noteform = NoteForm(instance=note, initial={"notetext": note.text})
|
||||
noteform.model = note
|
||||
elif action in ["view", "edit"]:
|
||||
note = Note.objects.get(handle=handle)
|
||||
noteform = NoteForm(instance=note)
|
||||
noteform = NoteForm(instance=note, initial={"notetext": note.text})
|
||||
noteform.model = note
|
||||
elif action == "save":
|
||||
note = Note.objects.get(handle=handle)
|
||||
noteform = NoteForm(request.POST, instance=note)
|
||||
noteform = NoteForm(request.POST, instance=note, initial={"notetext": note.text})
|
||||
noteform.model = note
|
||||
note.text = noteform.data["notetext"]
|
||||
if noteform.is_valid():
|
||||
update_last_changed(note, request.user.username)
|
||||
note = noteform.save()
|
||||
@@ -70,8 +71,9 @@ def process_note(request, context, handle, action, add_to=None): # view, edit, s
|
||||
action = "edit"
|
||||
elif action == "create":
|
||||
note = Note(handle=create_id())
|
||||
noteform = NoteForm(request.POST, instance=note)
|
||||
noteform = NoteForm(request.POST, instance=note, initial={"notetext": note.text})
|
||||
noteform.model = note
|
||||
note.text = noteform.data["notetext"]
|
||||
if noteform.is_valid():
|
||||
update_last_changed(note, request.user.username)
|
||||
note = noteform.save()
|
||||
@@ -94,6 +96,7 @@ def process_note(request, context, handle, action, add_to=None): # view, edit, s
|
||||
|
||||
context["noteform"] = noteform
|
||||
context["object"] = note
|
||||
context["notetext"] = note.text
|
||||
context["note"] = note
|
||||
context["action"] = action
|
||||
|
||||
|
||||
@@ -29,3 +29,9 @@ dp = parser.parse
|
||||
#import_file(db,
|
||||
# "/home/dblank/gramps/trunk/example/gramps/data.gramps",
|
||||
# cli.user.User())
|
||||
|
||||
#from webapp.utils import StyledNoteFormatter
|
||||
#snf = StyledNoteFormatter(db)
|
||||
#for n in Note.objects.all():
|
||||
# note = db.get_note_from_handle(n.handle)
|
||||
# print snf.get_note_format(note)
|
||||
|
||||
+83
-1
@@ -733,6 +733,88 @@ register_plugins()
|
||||
|
||||
# works after registering plugins:
|
||||
import HtmlDoc
|
||||
from libhtmlbackend import HtmlBackend
|
||||
from libhtmlbackend import HtmlBackend, process_spaces
|
||||
from libhtml import Html
|
||||
|
||||
class StyledNoteFormatter(object):
|
||||
def __init__(self, database):
|
||||
self.database = database
|
||||
self._backend = HtmlBackend()
|
||||
self._backend.build_link = self.build_link
|
||||
#self.report = report
|
||||
|
||||
def get_note_format(self, note):
|
||||
"""
|
||||
will get the note from the database, and will return either the
|
||||
styled text or plain note
|
||||
"""
|
||||
# retrieve the body of the note
|
||||
note_text = note.get()
|
||||
# styled notes
|
||||
htmlnotetext = self.styled_note(note.get_styledtext(),
|
||||
note.get_format(), contains_html =
|
||||
note.get_type() == gen.lib.NoteType.HTML_CODE)
|
||||
text = htmlnotetext or Html("p", note_text)
|
||||
# return text of the note to its callers
|
||||
return text
|
||||
|
||||
def styled_note(self, styledtext, format, contains_html=False):
|
||||
"""
|
||||
styledtext : assumed a StyledText object to write
|
||||
format : = 0 : Flowed, = 1 : Preformatted
|
||||
style_name : name of the style to use for default presentation
|
||||
"""
|
||||
text = str(styledtext)
|
||||
if not text:
|
||||
return ''
|
||||
s_tags = styledtext.get_tags()
|
||||
markuptext = self._backend.add_markup_from_styled(text, s_tags,
|
||||
split='\n')
|
||||
htmllist = Html("div", class_="grampsstylednote")
|
||||
if contains_html:
|
||||
htmllist += text
|
||||
else:
|
||||
linelist = []
|
||||
linenb = 1
|
||||
for line in markuptext.split('\n'):
|
||||
[line, sigcount] = process_spaces(line, format)
|
||||
if sigcount == 0:
|
||||
# The rendering of an empty paragraph '<p></p>'
|
||||
# is undefined so we use a non-breaking space
|
||||
if linenb == 1:
|
||||
linelist.append(' ')
|
||||
htmllist.extend(Html('p') + linelist)
|
||||
linelist = []
|
||||
linenb = 1
|
||||
else:
|
||||
if linenb > 1:
|
||||
linelist[-1] += '<br />'
|
||||
linelist.append(line)
|
||||
linenb += 1
|
||||
if linenb > 1:
|
||||
htmllist.extend(Html('p') + linelist)
|
||||
# if the last line was blank, then as well as outputting the previous para,
|
||||
# which we have just done,
|
||||
# we also output a new blank para
|
||||
if sigcount == 0:
|
||||
linelist = [" "]
|
||||
htmllist.extend(Html('p') + linelist)
|
||||
return htmllist
|
||||
|
||||
def build_link(self, prop, handle, obj_class):
|
||||
"""
|
||||
Build a link to an item.
|
||||
"""
|
||||
if prop == "gramps_id":
|
||||
if obj_class in self.database.get_table_names():
|
||||
obj = self.database.get_table_metadata(obj_class)["gramps_id_func"](handle)
|
||||
if obj:
|
||||
handle = obj.handle
|
||||
else:
|
||||
raise AttributeError("gramps_id '%s' not found in '%s'" %
|
||||
handle, obj_class)
|
||||
else:
|
||||
raise AttributeError("invalid gramps_id lookup "
|
||||
"in table name '%s'" % obj_class)
|
||||
# handle, ppl
|
||||
return "/%s/%s" % (obj_class.lower(), handle)
|
||||
|
||||
Reference in New Issue
Block a user