Skip to content

Commit

Permalink
Override NoteViewSet.update to refresh MongoDB on note edit - fixes k…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeverling committed Jan 5, 2018
1 parent 11dbcd0 commit 9b70358
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion onadata/apps/api/viewsets/note_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 9b70358

Please sign in to comment.