Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override NoteViewSet.update to refresh MongoDB on note edit - fixes #253 #407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions onadata/libs/serializers/note_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down