[Patchew-devel] [PATCH] Text to json convertor for mbox
Paolo Bonzini
pbonzini at redhat.com
Tue Apr 24 16:49:36 UTC 2018
On 24/04/2018 18:04, Shubham Jain wrote:
> class MessagesViewSet(ProjectMessagesViewSetMixin,
> - BaseMessageViewSet):
> + BaseMessageViewSet, mixins.CreateModelMixin):
> serializer_class = MessageSerializer
> +
> + def create(self, request, *args, **kwargs):
> + def find_message_projects(m):
> + return [p for p in Project.objects.all() if p.recognizes(m)]
Note that this is returning a list. Here you do have a project however,
because MessagesViewSet has a projects_pk field in kwargs (it's
registered under projects_router in api/urls.py). So
find_message_projects is something that you'd add later, when you add
/api/v1/messages/. For now you're only dealing with
/api/v1/projects/{id}/messages/.
> + msg = MboxMessage(request.data)
Later you would check the request.content_type, and go through
MboxMessage and msg.get_json() only if it's text/plain.
> + project = find_message_projects(msg)
> + msg.project = project
MboxMessage doesn't have a project field. I think you should first
retrieve the JSON with msg.get_json(), and then set the project URI into
the JSON, for example using Django REST Framework's reverse lookup function.
> + serializer = self.get_serializer(data=msg.get_json())
> + serializer.is_valid(raise_exception=True)
> + self.perform_create(serializer)
> + headers = self.get_success_headers(serializer.data)
> + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
> +
> + def perform_create(self, serializer):
> + serializer.save()
>
perform_create is the same as in CreateModelMixin so you can remove it.
I think you are a little confused as to what you are trying to achieve.
As a first step you should:
1) write a test case for MboxMessage.get_json()
2) just add CreateModelMixin and see what happens in the API browser.
Write a test case that POSTs a new message with JSON format.
3) only once you have the above you can start looking at adding
/api/v1/messages/.
Thanks,
Paolo
More information about the Patchew-devel
mailing list