def dog(): ''' View dog ''' form = CommentForm() arguments = request.args if 'id' in arguments: post_id = int(arguments['id']) else: return redirect(url_for('index')) query = Post.query.filter_by(id=post_id).first() if query is None: return redirect(url_for('index')) if form.validate_on_submit(): gigi = Comment(text=form.text.data, user_id=current_user.id, post_id=post_id) query.comments.append(gigi) DB.session.commit() comments = Comment.query.filter_by(post_id=post_id) print(comments) return render_template('dog.html', dog=query, comments=comments, form=form)
def _copyCommentToForm(self, comment, article_key=None, author=None): """Copy relevant fields from Comment to CommentForm.""" cf = CommentForm() if not author: author = Author.query()\ .filter(Author.authorID==comment.authorID)\ .get() if not article_key: article_key = comment.key.parent() for field in cf.all_fields(): if hasattr(comment, field.name): # convert Date to date string if field.name.startswith('date'): setattr(cf, field.name, str(getattr(comment, field.name))) else: setattr(cf, field.name, getattr(comment, field.name)) # add the fields that are not part of the Comment model elif field.name == "websafeCommentKey": setattr(cf, field.name, comment.key.urlsafe()) elif field.name == "websafeArticleKey": setattr(cf, field.name, article_key.urlsafe()) elif field.name == "websafeAuthorKey": setattr(cf, field.name, author.key.urlsafe()) cf.check_initialized() return cf
def post(post_id, page): form = CommentForm() if (current_user.is_authenticated() and form.validate_on_submit()): form.comment.author_id = current_user.id form.comment.post_id = post_id db.session.add(form.comment) db.session.commit() fragment.reset(posts_list) fragment.reset(comments_list, post_id) fragment.reset(user_info, current_user.id) flash('Your comment has saved successfully.', 'info') return render_template('post.html', form=form, post_id=post_id, page=page)
def post(req,post_id): is_ajax = req.GET.has_key('xhr') post = Post.objects.get(pk=post_id) if req.method == 'POST': comment_form = CommentForm(req.POST) comment = comment_form.save(commit=False) comment.post = post comment.save() payload = {'post':post, 'comments':Comment.objects.filter(post__id=post_id), 'comment_form':CommentForm()} return render_to_response('post.html',payload, RequestContext(req))
def show(aid): res = Article.query.filter(Article.id == aid) if res.count() > 0: article = res.one() cmform = CommentForm(request.form) fbfm = FeedbackForm(request.form) return render_template('article/show.html', article=article, cmform=cmform, fbfm=fbfm) else: abort(404)
def show(mid): res = db.query(Movie).filter(Movie.id==mid) if res.count()==0: abort(404) else: movie = res.one() ip = request.remote_addr res = db.query(MovieRead).filter(MovieRead.ip==ip, MovieRead.movie_id==mid) if res.count()==0: mr = MovieRead( ip=ip, movie_id=mid, created_at=now_datetime() ) db.add(mr) db.commit() if mr.id>0: movie.read_count+=1 db.commit() form = LoginForm(request.form) cmform = CommentForm(request.form) fbfm = FeedbackForm(request.form) followers = movie.followers().all() sql = "select c.*,mc.celebrity_type from movie_celebrity mc LEFT JOIN celebrity c on c.id=mc.celebrity_id WHERE mc.movie_id=:movie_id" celebrities = conn.execute(text(sql), {'movie_id':mid}) actors = [] directors = [] for cel in celebrities: if cel.celebrity_type==u'演员': actors.append(cel) elif cel.celebrity_type==u'导演': directors.append(cel) sql = "select u.* from movie_favorite mf left join users u on u.id=mf.user_id where mf.movie_id=:mid limit 10" favorites = conn.execute(text(sql),{'mid':mid}) subjects = movie.subjects.all() articles = movie.articles.all() return render_template('movie/detail.html', movie=movie, form=form, cmform=cmform,favorites=favorites,subjects=subjects, followers=followers, fbfm=fbfm, actors=actors, directors=directors, articles=articles)
def comment(request, article_id): if request.method == "POST": if request.POST['verify_message'].strip() == "sleepycat.org": f = CommentForm(request.POST) if f.is_valid(): cmt = f.save(commit=False) cmt.ip = get_client_ip(request) cmt.datetime = datetime.datetime.now() cmt.to_article = Article.objects.get(id=article_id) if request.POST['to_comment_id'] != "": cmt.to_comment = Comment.objects.get(id=request.POST['to_comment_id']) cmt.save() return HttpResponseRedirect('/blog/%s' % article_id) else: articles = Article.objects.order_by('datetime').reverse() tags = Tag.objects.order_by('weight') tag_articles = {} for tag in tags: tag_articles[tag.title] = 0 for a in articles: for tag in a.tags.all(): tag_articles[tag.title] += 1 for tag in tags: tag.articles = tag_articles[tag.title] context = { 'tags': tags, 'recentcmts': Comment.objects.order_by('datetime').reverse()[:3], 'f': f, } return render(request, 'blog/error.html', context) #not "sleepycat.org" else: errmsg = u"Verification Message Error. Please Input: sleepycat.org" articles = Article.objects.order_by('datetime').reverse() tags = Tag.objects.order_by('weight') tag_articles = {} for tag in tags: tag_articles[tag.title] = 0 for a in articles: for tag in a.tags.all(): tag_articles[tag.title] += 1 for tag in tags: tag.articles = tag_articles[tag.title] context = { 'tags': tags, 'recentcmts': Comment.objects.order_by('datetime').reverse()[:3], 'errmsg': errmsg, } return render(request, 'blog/error.html', context) #not POST else: return HttpResponseRedirect('/blog/%s' % article_id)