def edit_post(request, post_id): post = get_object_or_404(Post, pk=post_id) if post.id != int(post_id): raise Http404('Post not exists') if request.POST: form = PostForm(request.POST) if form.is_valid(): tag_list = [] #tags = set(map(lambda x:x.tag_name, post.tags.all())) | set(request.POST.get('tags', '').split(',')) tags = set(request.POST.get('tags', '').split(',')) post.tags.clear() for i in tags: try: j, created = Tag.objects.get_or_create(tag_name=i) except AssertionError: # more than 1 items returned continue tag_list.append(j) if len(tag_list): post.tags.add(*tag_list) post.subject = request.POST.get('subject', '') post.content = request.POST.get('content', '') post.save() return HttpResponseRedirect('/bbs/post/%d' % post.id) return render_to_response('bbs/edit_post.html', RequestContext(request, {'post': post}))
def new_post(request): ''' ''' if request.POST: form = PostForm(request.POST) tags = request.POST.get('tags', '').split(',') if form.is_valid(): tag_list = [] for i in tags: try: j, created = Tag.objects.get_or_create(tag_name=i) except AssertionError: # more than 1 items returned continue tag_list.append(j) if len(tag_list): post = form.save(commit=False) post.author = request.user post.save() post.tags.add(*tag_list); else: post = form.save() if request.GET.get('ajax', '') == 'true': obj = {'id': post.id, 'subject': post.subject, 'author': post.author.username, 'tags': tags} import json return HttpResponse(json.dumps(obj)) return HttpResponseRedirect('/bbs/post/%d' % post.id) form = PostForm() return render_to_response('bbs/new_post.html', RequestContext(request, {'form': form}))
def edit_post(request, post_id): post = get_object_or_404(Post, pk=post_id) if post.id != int(post_id): raise Http404('Post not exists') if request.POST: form = PostForm(request.POST) if form.is_valid(): tag_list = [] #tags = set(map(lambda x:x.tag_name, post.tags.all())) | set(request.POST.get('tags', '').split(',')) tags = set(request.POST.get('tags', '').split(',')) post.tags.clear() for i in tags: try: j, created = Tag.objects.get_or_create(tag_name=i) except AssertionError: # more than 1 items returned continue tag_list.append(j) if len(tag_list): post.tags.add(*tag_list); post.subject = request.POST.get('subject', '') post.content = request.POST.get('content', '') post.save() return HttpResponseRedirect('/bbs/post/%d' % post.id) return render_to_response('bbs/edit_post.html', RequestContext(request, {'post': post}))
def new_post(request): ''' ''' if request.POST: form = PostForm(request.POST) tags = request.POST.get('tags', '').split(',') if form.is_valid(): tag_list = [] for i in tags: try: j, created = Tag.objects.get_or_create(tag_name=i) except AssertionError: # more than 1 items returned continue tag_list.append(j) if len(tag_list): post = form.save(commit=False) post.author = request.user post.save() post.tags.add(*tag_list) else: post = form.save() if request.GET.get('ajax', '') == 'true': obj = { 'id': post.id, 'subject': post.subject, 'author': post.author.username, 'tags': tags } import json return HttpResponse(json.dumps(obj)) return HttpResponseRedirect('/bbs/post/%d' % post.id) form = PostForm() return render_to_response('bbs/new_post.html', RequestContext(request, {'form': form}))