From 9b70358057e7198cca1a48fede5d576b697fd8b4 Mon Sep 17 00:00:00 2001 From: Jesaja Everling Date: Fri, 5 Jan 2018 04:37:25 +0100 Subject: [PATCH] Override NoteViewSet.update to refresh MongoDB on note edit - fixes #253 --- onadata/apps/api/viewsets/note_viewset.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/onadata/apps/api/viewsets/note_viewset.py b/onadata/apps/api/viewsets/note_viewset.py index f02e70455..86e52ca15 100644 --- a/onadata/apps/api/viewsets/note_viewset.py +++ b/onadata/apps/api/viewsets/note_viewset.py @@ -62,7 +62,7 @@ class NoteViewSet(ViewPermissionMixin, ModelViewSet): permission_classes = [permissions.ViewDjangoObjectPermissions, permissions.IsAuthenticated, ] - #u This used to be post_save. Part of it is here, permissions validation + # This used to be post_save. Part of it is here, permissions validation # has been moved to the note serializer def perform_create(self, serializer): obj = serializer.save(user=self.request.user) @@ -73,6 +73,19 @@ def perform_create(self, serializer): # make sure parsed_instance saves to mongo db obj.instance.parsed_instance.save() + def update(self, request, *args, **kwargs): + """Override update to refresh the MongoDB representation of the instance when a note is edited. + + We call the parent method first, to save the updated note to the DB. It returns a response, which we return + after making sure that MongoDB is updated. + """ + response = super(NoteViewSet, self).update(request, *args, **kwargs) + obj = self.get_object() + instance = obj.instance + # update mongo data + instance.parsed_instance.save() + return response + def destroy(self, request, *args, **kwargs): obj = self.get_object() instance = obj.instance