def partial_update(self, request, org_pk, pk): org = self._get_object_or_404(Organization, {'pk': org_pk}) self.check_object_permissions(self.request, org) post = self._get_object_or_404(Post, {'org': org, 'pk': pk,}) serializer = PostSerializer(post, data=request.data, partial=True) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.save() return Response(serializer.data, status=200)
def create(self, request, org_pk, pk=None): org = self._get_object_or_404(Organization, {'pk': org_pk}) self.check_object_permissions(self.request, org) post_data = request.data.copy() post_data.update({'author': request.user.pk, 'org': org.pk}) serializer = PostSerializer(data=post_data) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.save() return Response(serializer.data, status=200)
def partial_update(self, request, org_pk, post_pk, pk): org = self._get_object_or_404(Organization, {'pk': org_pk}) self.check_object_permissions(self.request, org) post = self._get_object_or_404(Post, {'org': org, 'pk': post_pk}) fragment = self._get_object_or_404(Fragment, {'post': post, 'pk': pk}) serializer = PostSerializer(fragment, data=request.data, partial=True) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.save() return Response(serializer.data, status=200)
def create(self, request, org_pk, pk=None): org = self._get_object_or_404(Organization, {'pk': org_pk}) self.check_object_permissions(self.request, org) post_data = request.data.copy() post_data.update({ 'author': request.user.pk, 'org': org.pk }) serializer = PostSerializer(data=post_data) if not serializer.is_valid(): return Response(serializer.errors, status=400) serializer.save() return Response(serializer.data, status=200)
def list(self, request, org_pk, pk=None): org = self._get_object_or_404(Organization, {'pk': org_pk}) posts = org.posts.all() if not org.is_member(request.user): posts = posts.filter(state=constants.POST_STATE_PUBLISHED) serializer = PostSerializer(posts, many=True) return Response(serializer.data)
def retrieve(self, request, org_pk, pk): org = self._get_object_or_404(Organization, {'pk': org_pk}) params = {'org': org, 'pk': pk} if not org.is_member(request.user): params.update({'state': constants.POST_STATE_PUBLISHED}) post = self._get_object_or_404(Post, params) serializer = PostSerializer(post) return Response(serializer.data)