Пример #1
0
def api_create_blog_view(request):

	if request.method == 'POST':

		data = request.data
		data._mutable = True
		data['author'] = request.user.pk
		data._mutable = False
		serializer = BlogPostCreateSerializer(data=data)

		data = {}
		if serializer.is_valid():
			blog_post = serializer.save()
			data['response'] = CREATE_SUCCESS
			data['pk'] = blog_post.pk
			data['title'] = blog_post.title
			data['body'] = blog_post.body
			data['slug'] = blog_post.slug
			data['date_updated'] = blog_post.date_updated
			image_url = str(request.build_absolute_uri(blog_post.image.url))
			if "?" in image_url:
				image_url = image_url[:image_url.rfind("?")]
			data['image'] = image_url
			data['username'] = blog_post.author.username
			return Response(data=data)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
def api_create_blog_view(request):

    blog_post = BlogPost(author=request.user)

    if request.method == 'POST':
        try:
            if request.user.customer.membership:
                data = request.data
                data['author'] = request.user.pk
                serializer = BlogPostCreateSerializer(data=data)

                data = {}
                if serializer.is_valid():
                    blog_post = serializer.save()
                    data['response'] = CREATE_SUCCESS
                    data['pk'] = blog_post.pk
                    data['title'] = blog_post.title
                    data['body'] = blog_post.body
                    data['slug'] = blog_post.slug
                    data['date_updated'] = blog_post.date_updated
                    image_url = str(
                        request.build_absolute_uri(blog_post.image.url))
                    if "?" in image_url:
                        image_url = image_url[:image_url.rfind("?")]
                    data['image'] = image_url
                    data['username'] = blog_post.author.username
                    return Response(data=data)
        except Customer.DoesNotExist:
            return Response({'response': "RESPONSE_MUST_BECOME_T2R_MEMBER"})
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)