def blog_add(request): if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] for taglist in tagname.split(): Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['caption'] author = Author.objects.get(id=1) content = cd['content'] blog = Blog(caption=title, author=author, content=content) blog.save() for taglist in tagname.split(): blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() id = Blog.objects.order_by('-publish_time')[0].id return HttpResponseRedirect('/myblog/blog/%s' % id) #return HttpResponseRedirect(reverse('detailblog',args=[id])) else: form = BlogForm() tag = TagForm(initial={'tag_name': 'notags'}) return render(request,'blog_add.html', {'form': form, 'tag': tag}, context_instance=RequestContext(request))
def test_tag_form(self): """Test adding and editing tag form""" form = TagForm(data={}) self.assertFalse(form.is_valid()) form = TagForm(data={'name': 'foo'}) self.assertTrue(form.is_valid()) form.save() tag = Tag.objects.get() self.assertEqual(tag.name, 'foo') form = TagForm(instance=tag, data={'name': 'bar'}) self.assertTrue(form.is_valid()) form.save() tag = Tag.objects.get() self.assertEqual(tag.name, 'bar')
def blog_add(request): if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] for taglist in tagname.split(): Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['caption'] author = Author.objects.get(id=1) content = cd['content'] blog = Blog(caption=title, author=author, content=content) blog.save() for taglist in tagname.split(): blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() id = Blog.objects.order_by('-publish_time')[0].id return HttpResponseRedirect('/myblog/blog/%s' % id) #return HttpResponseRedirect(reverse('detailblog',args=[id])) else: form = BlogForm() tag = TagForm(initial={'tag_name': 'notags'}) return render(request, 'blog_add.html', { 'form': form, 'tag': tag }, context_instance=RequestContext(request))
def post(self, request): bound_form = TagForm(request.POST) if bound_form.is_valid(): new_tag = bound_form.save() return redirect('tags_list') return render(request, 'blog/tag_create.html', {'form': bound_form})
def tag_update(request, pk): tag = get_object_or_404(Tag, pk=pk) if request.method == 'POST': f = TagForm(request.POST, instance=tag) if f.is_valid(): if request.POST.get('author') == '' and request.user.is_superuser: updated_tag = f.save(commit=False) updated_tag.author = Author.objects.get(user__username='******') updated_tag.save() f.save_m2m() elif request.POST.get('author') and request.user.is_superuser: updated_tag = f.save() else: updated_tag = f.save(commit=False) updated_tag.author = Author.objects.get( user__username=request.user.username) updated_tag.save() f.save_m2m() messages.add_message(request, messages.INFO, 'Tag updated') return redirect('tag_update', pk=pk) else: print(f.errors) else: f = TagForm(instance=tag) return render(request, 'cadmin/tag_update.html', {'form': f})
def tag_add(request): if request.method == 'POST': f = TagForm(request.POST) if f.is_valid(): if request.POST.get('author') == '' and request.user.is_superuser: new_tag = f.save(commit=False) new_tag.author = Author.objects.get(user__username='******') new_tag.save() f.save_m2m() elif request.POST.get('author') and request.user.is_superuser: new_tag = f.save() else: new_tag = f.save(commit=False) new_tag.author = Author.objects.get( user__username=request.user.username) new_tag.save() f.save_m2m() messages.add_message(request, messages.INFO, 'Tag added') return redirect('tag_add') else: print(f.errors) else: f = TagForm() return render(request, 'cadmin/tag_add.html', {'form': f})
def article_add(request): if request.method == 'POST': form = ArticleForm(request.POST) tag = TagForm(request.POST) classification = ClassificationForm(request.POST) if form.is_valid() and tag.is_valid() and classification.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data cdclassification = classification.cleaned_data tagname = cdtag['tag_name'] classification_name = cdclassification["classification_name"] for taglist in tagname.split(): Tag.objects.get_or_create(tag_name=taglist.strip()) if classification_name != "": Classification.objects.get_or_create(name=classification_name.strip()) title = cd['caption'] author = Author.objects.get(id=1) content = cd['content'] article = Article(caption=title, author=author, content=content) article.classification = Classification.objects.get(name=classification_name.strip()) article.save() for taglist in tagname.split(): article.tags.add(Tag.objects.get(tag_name=taglist.strip())) article.save() id = Article.objects.order_by('-publish_time')[0].id return HttpResponseRedirect('/blog/article/%s/%s/' % ('/'.join(str(article.publish_time).split()[0].split('-')), id)) else: form = ArticleForm() tag = TagForm(initial={'tag_name': 'notags'}) classification = ClassificationForm() return render_to_response('article_add.html', {}, context_instance=RequestContext(request))
def test_tag_form(self): """Test adding and editing tag form""" form = TagForm(data={}) self.assertFalse(form.is_valid()) form = TagForm(data={"name": "foo"}) self.assertTrue(form.is_valid()) form.save() tag = Tag.objects.get() self.assertEqual(tag.name, "foo") form = TagForm(instance=tag, data={"name": "bar"}) self.assertTrue(form.is_valid()) form.save() tag = Tag.objects.get() self.assertEqual(tag.name, "bar")
def tag_update(request, pk): tag = get_object_or_404(Tag, pk=pk) # If request is POST, create a bound form(form with data) if request.method == "POST": # If request is POST, create a bound form f = TagForm(request.POST, instance=tag) if f.is_valid(): # updated_tag = f.save() if request.POST.get('author') == "" and request.user.is_superuser: # if author is not supplied and user is superuser updated_tag = f.save(commit=False) author = Author.objects.get(user__username='******') updated_tag.author = author updated_tag.save() elif request.POST.get('author') and request.user.is_superuser: # if author is supplied and user is superuser updated_tag = f.save() else: # if author not a superuser updated_tag = f.save(commit=False) updated_tag.author = Author.objects.get( user__username=request.user.username) updated_tag.save() messages.add_message(request, messages.INFO, 'Tag updated') return redirect(reverse('tag_update', args=[tag.id])) # if request is GET the show unbound form to the user else: f = TagForm(instance=tag) return render(request, 'cadmin/tag_update.html', {'form': f, 'tag': tag})
def tag_add(request): # If request is POST, create a bound form(form with data) if request.method == "POST": f = TagForm(request.POST) if f.is_valid(): # new_category = f.save() # new_category = f.save(commit=False) # new_category.author = get_user(request) # new_category.save() if request.POST.get('author') == "" and request.user.is_superuser: # if author is not supplied and user is superuser new_tag = f.save(commit=False) author = Author.objects.get(user__username='******') new_tag.author = author new_tag.save() elif request.POST.get('author') and request.user.is_superuser: # if author is supplied and user is superuser new_tag = f.save() else: # if author not a superuser new_tag = f.save(commit=False) new_tag.author = Author.objects.get( user__username=request.user.username) new_tag.save() messages.add_message(request, messages.INFO, 'Tag added') return redirect('tag_add') # if request is GET the show unbound form to the user else: f = TagForm() return render(request, 'cadmin/tag_add.html', {'form': f})
def giveout(request, id=''): ''' 发表 or 编辑 ''' context = RequestContext(request) if request.method == "POST": giveout_form = GiveoutForm(request.POST) tags_form = TagForm(request.POST) if giveout_form.is_valid() and tags_form.is_valid(): giveout = giveout_form.cleaned_data tags = tags_form.cleaned_data if id: blog = Blog.objects.get(id=id) blog.title = giveout['title'] blog.content = content = giveout['content'] blog.tags.clear() else: blog = Blog(title = giveout['title'], content = giveout['content'], author = User.objects.get(username=request.user.username), views = giveout['views'], likes = giveout['likes'], ) blog.save() # 存储 for t in tags['tag_name']: Tag.objects.get_or_create(tag_name=t) for t in tags['tag_name']: blog.tags.add(Tag.objects.get(tag_name=t)) blog.save() return HttpResponseRedirect(reverse('index')) else: print tags_form.errors, giveout_form.errors else: if id: # 编辑 blog = get_object_or_404(Blog, pk=id) tags = blog.tags.all() giveout_form = GiveoutForm(initial={'title':blog.title, 'content':blog.content}, ) # auto_id=False tag_str_list = [] for t in tags: tag_str_list.append(str(t)) tag_str_list = ','.join(tag_str_list) tags_form = TagForm(initial={'tag_name':tag_str_list}) else: giveout_form = GiveoutForm() tags_form = TagForm() context_dict = {'giveout_form':giveout_form, 'tags_form':tags_form} context_dict['base_tags'] = get_tags() context_dict['base_views'] = get_views() return render_to_response('blog/giveout.html', context_dict, context)
def blog_update(request, id=""): id = id if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] tagnamelist = tagname.split() for taglist in tagnamelist: Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['caption'] content = cd['content'] blog = Blog.objects.get(id=id) if blog: blog.caption = title blog.content = content blog.save() for taglist in tagnamelist: blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() tags = blog.tags.all() for tagname in tags: tagname = unicode(str(tagname), "utf-8") if tagname not in tagnamelist: notag = blog.tags.get(tag_name=tagname) blog.tags.remove(notag) else: blog = Blog(caption=blog.caption, content=blog.content) blog.save() return HttpResponseRedirect('/myblog/blog/%s' % id) else: try: blog = Blog.objects.get(id=id) except Exception: raise Http404 form = BlogForm(initial={ 'caption': blog.caption, 'content': blog.content }, auto_id=False) tags = blog.tags.all() if tags: taginit = '' for x in tags: taginit += str(x) + ' ' tag = TagForm(initial={'tag_name': taginit}) else: tag = TagForm() return render_to_response('blog_add.html', { 'blog': blog, 'form': form, 'id': id, 'tag': tag }, context_instance=RequestContext(request))
def post(self, request, slug): tag = Tag.objects.get(slug__iexact=slug) bound_form = TagForm(request.POST, instance=tag) if bound_form.is_valid(): new_tag = bound_form.save() return redirect('tags_list') return render(request, 'blog/tag_update.html', { 'form': bound_form, 'tag': tag })
def article_update(request, year='', month='', day='', id=""): id = id if request.method == 'POST': form = ArticleForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] tagnamelist = tagname.split() for taglist in tagnamelist: Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['caption'] content = cd['content'] article = Article.objects.get(id=id) if article: article.caption = title article.content = content article.save() for taglist in tagnamelist: article.tags.add(Tag.objects.get(tag_name=taglist.strip())) article.save() tags = article.tags.all() for tagname in tags: tagname = unicode(str(tagname), "utf-8") if tagname not in tagnamelist: notag = article.tags.get(tag_name=tagname) article.tags.remove(notag) else: article = Article(caption=article.caption, content=article.content) article.save() return HttpResponseRedirect('/blog/article/%s/%s/' % ('/'.join(str(article.publish_time).split()[0].split('-')), id)) else: try: article = Article.objects.get(id=id) if [year, month, day] != str(article.publish_time).split()[0].split('-'): raise Article.DoesNotExist except Exception: raise Http404 tags = article.tags.all() if tags: taginit = '' for x in tags: taginit += str(x) + ' ' tag = taginit else: tag = '' return render_to_response('article_add.html', {'article': article, 'id': id, 'tag': tag}, context_instance=RequestContext(request))
def blog_update(request, id=""): id = id if request.method == 'POST': form = BlogForm(request.POST) tag = TagForm(request.POST) if form.is_valid() and tag.is_valid(): cd = form.cleaned_data cdtag = tag.cleaned_data tagname = cdtag['tag_name'] tagnamelist = tagname.split() for taglist in tagnamelist: Tag.objects.get_or_create(tag_name=taglist.strip()) title = cd['caption'] content = cd['content'] blog = Blog.objects.get(id=id) if blog: blog.caption = title blog.content = content blog.save() for taglist in tagnamelist: blog.tags.add(Tag.objects.get(tag_name=taglist.strip())) blog.save() tags = blog.tags.all() for tagname in tags: tagname = unicode(str(tagname), "utf-8") if tagname not in tagnamelist: notag = blog.tags.get(tag_name=tagname) blog.tags.remove(notag) else: blog = Blog(caption=blog.caption,content=blog.content) blog.save() return HttpResponseRedirect('/newblog/%s' % id) else: try: blog = Blog.objects.get(id=id) except Exception: raise Http404 form = BlogForm(initial={'caption': blog.caption,'content': blog.content}, auto_id=False) tags = blog.tags.all() if tags: taginit = '' for x in tags: taginit += str(x) + ' ' tag = TagForm(initial={'tag_name': taginit}) else: tag = TagForm() return render_to_response('blog_add.html',{'blog': blog, 'form': form, 'id': id, 'tag': tag}, context_instance=RequestContext(request))
def TagCreate(request): tag = Tag.objects.all() form = TagForm if request.method == 'POST': form = TagForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse("blog:tag_add")) return render(request, 'blog/tag_create_form.html', { 'tag': tag, 'form': form })
def tag_logic(request): if request.method == "POST": form = TagForm(request.POST) if form.is_valid(): # Get the instance of the form filled with the submitted data name = form.cleaned_data['name'] Tag.objects.create(name=name) return redirect("/tags") # Else if the user is looking at the form page else: form = TagForm() tags = Tag.objects.all() data = {'tag_form': form} return render(request, "tag_form.html", data)
def create_view(request): if request.method == 'POST': if request.POST['action'] == 'post': post_form = PostForm(request.POST) if post_form.is_valid(): post = post_form.save(commit=False) post.poster = request.user post.save() return redirect(reverse('blog:index_view')) else: print('Looks like post was not valid.') print(post_form.errors) if request.POST['action'] == 'tag': tag_form = TagForm(request.POST) if tag_form.is_valid(): tag_creator = request.user print(tag_creator) return redirect(reverse('blog:index_view')) else: return ( HttpResponse('<h1>LOOKS LIKE TAG WAS INCOMPLETE.</h1>')) else: post_form = PostForm() tag_form = TagForm() return render(request, 'blog/create.html', context={ 'post_form': post_form, 'tag_form': tag_form })
def tag_add(request): # if request is POST, create a bound form(form with data if request.method == "POST": f = TagForm(request.POST) # check whether form is valid or not # if the form is valid, save the data to the database # and redirect the user back to the add post form # if form is invalid show form with errors again if f.is_valid(): # new_tag = f.save() # new_tag = f.save(commit=False) # new_tag.author = get_user(request) # new_tag.save() if request.POST.get('author') == "" and request.user.is_superuser: # if author is not supplied and user is superuser new_tag = f.save(commit=False) author = Author.objects.get(user__username='******') new_tag.author = author new_tag.save() elif request.POST.get('author') and request.user.is_superuser: # if author is supplied and user is superuser new_tag = f.save() else: # if author not a superuser new_tag = f.save(commit=False) new_tag.author = Author.objects.get( user__username=request.user.username) new_tag.save() messages.add_message(request, messages.INFO, _('Tag added')) return redirect('tag_add') # if request is GET the show unbound form to the user else: f = TagForm() return render(request, 'cadmin/tag_add.html', {'form': f})
def tag_add(request): # if request is POST, create bound form if request.method == "POST": f = TagForm(request.POST) # check whether form is valid # if valid, save data to database # redirect user to add post form # if form is invalid, show with errors if f.is_valid(): # new_category = f.save() # new_category = f.save(commit=False) # new_category.author = get_user(request) # new_category.save() if request.POST.get('author') == "" and request.user.is_superuser: # if author is not supplied and user is superuser new_tag = f.save(commit=False) author = Author.objects.get(user__username='******') new_tag.author = author new_tag.save() elif request.POST.get('author') and request.user.is_superuser: # if author is supplied and user is superuser new_tag = f.save() else: # if author is not a superuser new_tag = f.save(commit=False) new_tag.author = Author.objects.get( user__username=request.user.username) new_tag.save() messages.add_message(request, messages.INFO, 'Tag Added') return redirect('tag_add') # if request is GET show unbound form else: f = TagForm() return render(request, 'cadmin/tag_add.html', {'form': f})
def admin_add_tag(request): if users.is_current_user_admin(): if request.method == 'GET': form = TagForm() elif request.method == 'POST': form = TagForm(request.POST) if form.is_valid(): tag = form.save(commit=False) tag.title = string.lower(tag.title) tag.put() return HttpResponseRedirect('/admin') return render_to_response('admin/edit.html', dictionary={ 'form':form, 'type': 'Add Tag', }, context_instance=RequestContext(request) ) else: return HttpResponseRedirect('/')
def admin_add_tag(request): if users.is_current_user_admin(): if request.method == 'GET': form = TagForm() elif request.method == 'POST': form = TagForm(request.POST) if form.is_valid(): tag = form.save(commit=False) tag.title = string.lower(tag.title) tag.put() return HttpResponseRedirect('/admin') return render_to_response('admin/edit.html', dictionary={ 'form': form, 'type': 'Add Tag', }, context_instance=RequestContext(request)) else: return HttpResponseRedirect('/')
def tag_update(request, pk): tag = get_object_or_404(Tag, pk=pk) # if request is POST, created bound form if request.method == "POST": f = TagForm(request.POST, instance=tag) # check whether form is valid # if valid, save to database # redirect to tag update form # if form is invalid, show with errors if f.is_valid(): # updated_tag = f.save() if reuqest.POST.get('author') == "" and request.user.is_superuser: # is author is not supplied and user is superuser updated_tag = f.save(commit=False) author = Author.objects.get(user__username='******') udated_tag.author = author updated_tag.save() elif request.POST.get('author') and request.user.is_superuser: # author is supplied and user is superuser updated_category = f.save() else: # author is not a superuser updated_tag = f.save(commit=False) updated_tag.author = Author.objects.get( user__username=request.user.username) updated_tag.save() messages.add_message(request, messages.INFO, 'Tag Updated') return redirect(reverse('tag_update', args=[tag.id])) #if request is GET show unbound form else: f = TagForm(instance=tag) return render(request, 'cadmin/tag_update.html', {'form': f, 'tag': tag})
def tag_update(request, pk): tag = get_object_or_404(Tag, pk=pk) if request.method == "POST": f = TagForm(request.POST, instance=tag) if f.is_valid(): if request.POST.get('author') == "" and request.user.is_superuser: updated_tag = f.save(commit=False) author = Author.objects.get(user__username='******') updated_tag.author = author updated_tag.save() elif request.POST.get('author') and request.user.is_superuser: updated_tag = f.save() else: updated_tag = f.save(commit=False) updated_tag.author = Author.objects.get( user__username=request.user.username) updated_tag.save() messages.add_message(request, messages.INFO, 'Tag updated') return redirect(reverse('tag_update', args=[tag.id])) else: f = TagForm(instance=tag) return render(request, 'cadmin/tag_update.html', {'form': f, 'tag': tag})
def add_post(request): if request.method == 'POST': form1Post = PostForm(request.POST) form2Tags = TagForm(request.POST) if form1Post.is_valid() and form2Tags.is_valid(): post = form1Post.save() #post_instance_for_tag = post.save() tag_title_from_form = form2Tags['tag_title'].value().strip().lower() tag_title_from_form = tag_title_from_form.rstrip(',') striped_tags = tag_title_from_form.split(',') for t in striped_tags: received_tag = t.strip() tag, created = Tag.objects.get_or_create(tag_title=received_tag) post.tag_set.add(tag) return index(request) else: print (form1Post.errors) print (form2Tags.errors) else: form1Post = PostForm() form2Tags = TagForm() return render(request, 'blog/add_post.html', {'form1':form1Post,'form2':form2Tags})
def blog_post(request, blog_post_slug): post_detail = Post.objects.get(slug=blog_post_slug) current_user = request.user comments = Comment.objects.filter(post=post_detail.id) tags = Tag.objects.filter(post=post_detail.id) form = CommentForm(request.POST or None) tagform = TagForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.post = post_detail comment.commenter = current_user comment.save() return redirect(request.path) if tagform.is_valid(): post_id = post_detail.id post_for_tag = Post.objects.get(id=post_id) tag_title_from_form = tagform['tag_title'].value().strip().lower() tag_title_from_form = tag_title_from_form.rstrip(',') striped_tags = tag_title_from_form.split(',') for t in striped_tags: print (t) try: nt = t.strip() existing_tag = Tag.objects.get(tag_title__iexact=nt) post_for_tag.tag_set.add(existing_tag) except Tag.DoesNotExist: nt = t.strip() new_tag = Tag(tag_title=nt) new_tag.save() post_for_tag.tag_set.add(new_tag) continue return redirect(request.path) context_dict = {'post': post_detail, 'slug': blog_post_slug, 'comments':comments, 'tags':tags, 'form':form, 'tagform':tagform} response = render(request,'blog/blog_post.html', context_dict) return response
def post(self, request, *args, **kwargs): form = TagForm(request.POST) if not form.is_valid(): self.form_error = True return super(UpdateTagView, self).get(request, *args, **kwargs) return super(UpdateTagView, self).post(request, *args, **kwargs)
def post(self, request): form = TagForm(request.POST) if form.is_valid(): form.save() return redirect('create_post') return render(request, 'blog/create_tag.html', context={'form': form})