def create_post(request): user = request.user blog_id = request.POST.get('blog_id') blog = Blog.objects.get(pk = blog_id) post_id = request.POST.get('post_id') if user not in blog.authors.all(): return HttpResponse("Sorry you are not authorized to post here") else: if post_id: post = Post.objects.get(pk=post_id) if user != post.author: return HttpResponse("Sorry you are not authorized to edit here") else: post = Post() post.author = user post.blog = blog post.title = request.POST.get('title') post.body = request.POST.get('body') tags = request.POST.get('tags') tag_names = tags.split(',') if not tags.strip(): tag_names = [constant.NO_TAG] tag_set = set(tag_names) tag_list = [] for tname in tag_set: tname_s = tname.strip() try: tag = Tag.objects.get(name__iexact=tname_s) except Tag.DoesNotExist: tag = None if not tag: tag = Tag() tag.name = tname_s tag.save() tag_list.append(tag) post.save() post.tags = tag_list; post.save() return HttpResponseRedirect(reverse('myblog:index'))
def post(self): ''' Create new post ''' post_id = None if 'id' in request.json: post_id = request.json['id'] title = request.json['title'] body = request.json['body'] tags = request.json['tags'] tags = [slugify(item) for item in tags] if (not title) or (not body): return jsonify({ 'error': 'title & body is required.', 'data': {'title': title, 'body': body, 'slug': slugify(title) } }) if post_id: post = Post.objects.get(id=post_id) else: post = Post(author=current_user.id) post.title = title post.body = body post.slug = slugify(title) post.tags = tags post.save() return jsonify({ 'success': 'success', 'post' : post, 'edit_url': post.get_edit_url() })