WIP: Note editing, markdown to html
[oweals/karmaworld.git] / karmaworld / apps / notes / views.py
index b6cdaf46057075b82a5a4e6a02955c1ca9cac799..281c781b5fb571157e876bd3ce51604eb6d022a3 100644 (file)
@@ -43,8 +43,13 @@ def note_page_context_helper(note, request, context):
         context['note_edit_form'] = NoteForm(request.POST)
     else:
         tags_string = ','.join([str(tag) for tag in note.tags.all()])
-        context['note_edit_form'] = NoteForm(initial={'name': note.name,
-                                                      'tags': tags_string})
+        initial = {"name": note.name, "tags": tags_string}
+        try:
+            initial["html"] = note.notemarkdown.html
+        except NoteMarkdown.DoesNotExist:
+            pass
+        print initial
+        context['note_edit_form'] = NoteForm(initial=initial)
 
     context['note_delete_form'] = NoteDeleteForm(initial={'note': note.id})
 
@@ -107,6 +112,22 @@ class NoteSaveView(FormView, SingleObjectMixin):
         }
         return super(NoteSaveView, self).get_context_data(**context)
 
+    def get_form_kwargs(self):
+        """
+        Include related notemarkdown.html in form.
+        """
+        kwargs = {"initial": self.get_initial()}
+        try:
+            kwargs["initial"]["html"] = self.object.notemarkdown.html
+        except NoteMarkdown.DoesNotExist:
+            pass
+        if self.request.method in ("POST", "PUT"):
+            kwargs.update({
+                "data": self.request.POST,
+                "files": self.request.FILES,
+            })
+        return kwargs
+
     def form_valid(self, form):
         """ Actions to take if the submitted form is valid
             namely, saving the new data to the existing note object