def create_paste(): if request.method == 'GET': # missing csrf form = PasteForm( data={'codes': [{ 'title': '', 'content': '', 'syntax': 'text' }]}) return render_template('pastes/create.html', form=form) else: form = PasteForm.from_json(data=request.json) if form.validate(): user = current_user.user paste = save_paste_and_codes(form) if not paste.is_private: followers = User.objects(followings=user) content = NEW_PASTE.format( user_username=user.username, user_url=url_for('user_app.view', username=user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id)) for follower in followers: message = Message(user=follower, who=user, content=content) message.save() return jsonify(success=True, hash_id=paste.hash_id) else: errors = form.errors errors['codes'] = [code.errors for code in form.codes] logger.info('Failed saving paste for reason: %s', errors) return jsonify(success=False, errors=errors)
def add_paste(): if 'paste_hash_id' not in request.form or 'bookmark_id' not in request.form: abort(404) bookmark = Bookmark.objects(hash_id=request.form['bookmark_id']).get_or_404() paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404() if paste in bookmark.pastes: return render_template('error.html', title=u'该代码集合已经在收藏夹中', message=u'该代码集合已经在收藏夹中') if len(bookmark.pastes) >= 10: return render_template('error.html', title=u'一个收藏夹最多只能有10个代码集合', message=u'一个收藏夹最多只能有10个代码集合') if paste not in bookmark.pastes: bookmark.pastes.append(paste) bookmark.save() if bookmark.user != paste.user and not bookmark.is_private: content = BOOKMARK.format(user_username=current_user.user.username, user_url=url_for('user_app.view', username=current_user.user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id), bookmark_title=bookmark.title, bookmark_url=url_for('bookmark_app.view', hash_id=bookmark.hash_id)) message = Message(user=paste.user, who=bookmark.user, content=content) message.save() return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
def create_paste(): if request.method == 'GET': # missing csrf form = PasteForm(data={'codes': [{'title': '', 'content': ''}]}) return render_template('pastes/create.html', form=form) else: form = PasteForm(request.form) if form.validate(): user = current_user.user paste = save_paste_and_codes(form) if not paste.is_private: followers = User.objects(followings=user) content = NEW_PASTE.format(user_username=user.username, user_url=url_for('user_app.view', username=user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id)) for follower in followers: message = Message(user=follower, who=user, content=content) message.save() return jsonify(success=True, hash_id=paste.hash_id) else: errors = form.errors errors['codes'] = [code.errors for code in form.codes] logger.info('Failed saving paste for reason: %s', errors) return jsonify(success=False, errors=errors)
def watch_user(username): following_user = User.objects(username=username).first_or_404() if not current_user.user.is_following(following_user): current_user.user.followings.append(following_user) current_user.user.save() content = WATCH.format(user_username=current_user.user.username, user_url=url_for('user_app.view', username=current_user.user.username)) message = Message(user=following_user, who=current_user.user, content=content) message.save() return jsonify(watchedStatus=current_user.user.is_following(following_user))
def messages(): page = get_page() pagination = Message.objects(user=current_user.user).order_by('-created_at').paginate(page, per_page=20) for item in pagination.items: item.is_read = True item.save() return render_template('users/messages.html', pagination=pagination)
def create_message(user, paste): user.messages.append( Message(category=NEW_PASTE, content=u"您关注的用户 [%s](%s) 发表了新的代码集合 [%s](%s)" % (paste.user.username, url_for('user_app.view_user', username=paste.user.username), paste.title, url_for('paste_app.view_paste', hash_id=paste.hash_id)))) user.save()
def messages(): page = get_page() pagination = Message.objects( user=current_user.user).order_by('-created_at').paginate(page, per_page=20) for item in pagination.items: item.is_read = True item.save() return render_template('users/messages.html', pagination=pagination)
def like(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) user = current_user.user is_user_liked = paste in user.likes if not is_user_liked: user.likes.append(paste) user.save() if user != paste.user: content = LIKE.format(user_username=user.username, user_url=url_for('user_app.view', username=user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id)) message = Message(user=paste.user, who=user, content=content) message.save() return jsonify(dict(paste_id=hash_id, user_like=len(user.likes), paste_likes=len(user.likes), liked=True))
def add_paste(): if 'paste_hash_id' not in request.form or 'bookmark_id' not in request.form: abort(404) bookmark = Bookmark.objects( hash_id=request.form['bookmark_id']).get_or_404() paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404() if paste.is_private and not bookmark.is_private: return render_template('error.html', title=u'公开的收藏夹不能添加私有的代码集合', message=u'公开的收藏夹不能添加私有的代码集合') if paste in bookmark.pastes: return render_template('error.html', title=u'该代码集合已经在收藏夹中', message=u'该代码集合已经在收藏夹中') if len(bookmark.pastes) >= 10: return render_template('error.html', title=u'一个收藏夹最多只能有10个代码集合', message=u'一个收藏夹最多只能有10个代码集合') if paste not in bookmark.pastes: bookmark.pastes.append(paste) bookmark.save() if bookmark.user != paste.user and not bookmark.is_private: content = BOOKMARK.format( user_username=current_user.user.username, user_url=url_for('user_app.view', username=current_user.user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id), bookmark_title=bookmark.title, bookmark_url=url_for('bookmark_app.view', hash_id=bookmark.hash_id)) message = Message(user=paste.user, who=bookmark.user, content=content) message.save() return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
def like(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) user = current_user.user is_user_liked = paste in user.likes if not is_user_liked: user.likes.append(paste) user.save() if user != paste.user: content = LIKE.format(user_username=user.username, user_url=url_for('user_app.view', username=user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id)) message = Message(user=paste.user, who=user, content=content) message.save() return jsonify( dict(paste_id=hash_id, user_like=len(user.likes), paste_likes=len(user.likes), liked=True))
def comments(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) form = CommentForm(request.form) if form.validate(): comment = Comment(user=current_user.user, paste=paste, content=form.content.data) comment.save() if comment.user != paste.user: content = NEW_COMMENT.format(user_username=current_user.user.username, user_url=url_for('user_app.view', username=current_user.user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id)) message = Message(user=paste.user, who=current_user.user, content=content) message.save() return redirect(url_for('paste_app.view_paste', hash_id=hash_id))
def comments(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) form = CommentForm(request.form) if form.validate(): comment = Comment(user=current_user.user, paste=paste, content=form.content.data) comment.save() if comment.user != paste.user: content = NEW_COMMENT.format( user_username=current_user.user.username, user_url=url_for('user_app.view', username=current_user.user.username), paste_title=paste.title, paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id)) message = Message(user=paste.user, who=current_user.user, content=content) message.save() return redirect(url_for('paste_app.view_paste', hash_id=hash_id))
def unread_messages_count(self): return Message.objects(user=self, is_read=False).count()
def messages(): page = get_page() pagination = Message.objects(user=current_user.user).order_by('-created_at').paginate(page, per_page=20) return render_template('users/messages.html', pagination=pagination)
def messages(): page = get_page() pagination = Message.objects( user=current_user.user).order_by('-created_at').paginate(page, per_page=20) return render_template('users/messages.html', pagination=pagination)