def reply(post_id, comment_id): """ 回复. """ post = Post.find_one({'_id': post_id}) if not post: return jsonify(success=False, message=_('The post does not exist!')) content = request.form.get('content', None) if not content or not content.strip(): return jsonify(success=False, message=_('Reply content can not be blank!')) cmt = next((c for c in post.comments if c.id == comment_id), -1) if cmt == -1: return jsonify( success=False, message=_('The comment you would like to reply does not exist!')) now = datetime.now() reply = { 'uid': current_user._id, 'rid': ObjectId(request.form.get('rid', None)), 'content': content, 'time': now } cmt.replys.append(reply) post.save() send_support_email('reply()', u'New reply %s on post %s.' % (content, post._id)) return jsonify(success=True, message=_('Save reply successfully.'))
def comment(post_id): """ 评论博文. """ post = Post.find_one({'_id': post_id}) if not post: return jsonify(success=False, message=_('The post does not exist!')) content = request.form.get('content', None) if not content or not content.strip(): return jsonify(success=False, message=_('Comment content can not be blank!')) max = -1 for c in post.comments: if max < c.id: max = c.id now = datetime.now() cmt = { 'id': max + 1, 'uid': current_user._id, 'content': content, 'time': now } post.comments.insert(0, cmt) post.save() send_support_email('comment()', u'New comment %s on post %s.' % (content, post._id)) return jsonify(success=True, message=_('Save comment successfully.'))
def signup(): """ Signup. """ form = SignupForm() if form.validate_on_submit(): if not form.password.data == form.repassword.data: return render_template('public/signup.html', form=form, error=_('Password dismatch!')) em = form.email.data.strip().lower() u = User.find_one({'email': em}) if u: return render_template('public/signup.html', form=form, error=_('This email has been registered!')) u = User() u.email = em u.password = unicode(generate_password_hash(form.password.data.strip())) u.name = u.email.split('@')[0] u.save() current_app.logger.info('A new user created, %s' % u) send_support_email('signup()', u'New user %s with id %s.' % (u.email, u._id)) # Keep the user info in the session using Flask-Login login_user(u) # Tell Flask-Principal the identity changed identity_changed.send(current_app._get_current_object(), identity=Identity(u.get_id())) return redirect('/') return render_template('public/signup.html', form=form)
def signup(): """ Signup. """ form = SignupForm() if form.validate_on_submit(): em = form.email.data.strip().lower() pwd = form.password.data.strip() u = User.find_one({'email': em}) if u: form.email.errors.append(_('This email has been registered!')) return render_template('public/signup.html', form=form) # Create user u = User() u.email = em u.password = generate_password_hash(pwd) u.name = u.email.split('@')[0] u.avatar = url_for('static', filename='img/avatar.jpg') count = User.count({}) # Set first signup user to admin if count == 0: u.roles = [UserRole.MEMBER, UserRole.ADMIN] current_app.logger.info('First user, set it to admin') else: current_app.logger.info('Current number of users is %s' % count) # u.save() current_app.logger.info('A new user created, %s' % u) send_support_email('signup()', 'New user %s with id %s.' % (u.email, u._id)) # Keep the user info in the session using Flask-Login login_user(u) return redirect('/') # return render_template('public/signup.html', form=form)
def reply(post_id, comment_id): """ 回复. """ post = Post.find_one({'_id': post_id}) if not post: return jsonify(success=False, message=_('The post does not exist!')) content = request.form.get('content', None) if not content or not content.strip(): return jsonify(success=False, message=_('Reply content can not be blank!')) cmt = next((c for c in post.comments if c.id == comment_id), -1) if cmt == -1: return jsonify(success=False, message=_('The comment you would like to reply does not exist!')) now = datetime.now() reply = { 'uid': current_user._id, 'rid': ObjectId(request.form.get('rid', None)), 'content': content, 'time': now } cmt.replys.append(reply) post.save() send_support_email('reply()', u'New reply %s on post %s.' % (content, post._id)) return jsonify(success=True, message=_('Save reply successfully.'))