From f72adac8626d45a4ea65f2abf9d4a80af1c355eb 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 ++++++++++++++- onadata/libs/serializers/note_serializer.py | 1 + 2 files changed, 15 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 diff --git a/onadata/libs/serializers/note_serializer.py b/onadata/libs/serializers/note_serializer.py index 0532f49d0..690823cb6 100644 --- a/onadata/libs/serializers/note_serializer.py +++ b/onadata/libs/serializers/note_serializer.py @@ -12,6 +12,7 @@ class NoteSerializer(serializers.ModelSerializer): class Meta: model = Note + fields = '__all__' def save(self, user=None): # This used to be in note_viewset