def index(): page = get_page() pagination = Bookmark.objects(is_private=False).order_by('-updated_at').paginate(page, per_page=20) return render_template('bookmarks/index.html', hot_bookmarks=Bookmark.objects(is_private=False).order_by('-views')[:10], pagination=pagination)
def index(): page = get_page() pagination = Bookmark.objects( is_private=False).order_by('-updated_at').paginate(page, per_page=20) return render_template('bookmarks/index.html', hot_bookmarks=Bookmark.objects( is_private=False).order_by('-views')[:10], pagination=pagination)
def create(): form = BookmarkForm() if request.method == 'GET': return render_template('bookmarks/create.html', form=form) else: if form.validate_on_submit(): bookmark = Bookmark(title=form.title.data, is_private=form.is_private.data, description=form.description.data, user=current_user.user) bookmark.save() return redirect( url_for('bookmark_app.view', hash_id=bookmark.hash_id)) return render_template('bookmarks/create.html', form=form)
def view_paste(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) paste.increase_views() sig = message = timestamp = None paste_lists = [] if current_user.is_authenticated: # create a JSON packet of our data attributes data = json.dumps({'id': str(current_user.id), 'username': current_user.username, 'email': current_user.email, 'avatar': current_user.user.gravatar_url()}) # encode the data to base64 message = base64.b64encode(data) # generate a timestamp for signing the message timestamp = int(time.time()) # generate our hmac signature sig = hmac.HMAC(current_app.config['DISQUS']['secret_key'], '%s %s' % (message, timestamp), hashlib.sha1).hexdigest() paste_lists = Bookmark.objects(user=current_user.user) return render_template('pastes/view.html', paste=paste, paste_lists=paste_lists, message=message, timestamp=timestamp, sig=sig)
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 view_paste(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) paste.increase_views() sig = message = timestamp = None paste_lists = [] if current_user.is_authenticated: # create a JSON packet of our data attributes data = json.dumps({ 'id': str(current_user.id), 'username': current_user.username, 'email': current_user.email, 'avatar': current_user.user.gravatar_url() }) # encode the data to base64 message = base64.b64encode(data) # generate a timestamp for signing the message timestamp = int(time.time()) # generate our hmac signature sig = hmac.HMAC(current_app.config['DISQUS']['secret_key'], '%s %s' % (message, timestamp), hashlib.sha1).hexdigest() paste_lists = Bookmark.objects(user=current_user.user) return render_template('pastes/view.html', paste=paste, paste_lists=paste_lists, message=message, timestamp=timestamp, sig=sig)
def my_bookmark(): page = get_page() pagination = Bookmark.objects(user=current_user.user).order_by('-updated_at').paginate(page, per_page=20) return render_template('bookmarks/index.html', pagination=pagination)
def delete_list(hash_id): paste_list = Bookmark.objects(hash_id=hash_id).get_or_404() if paste_list.user != current_user.user: abort(401) paste_list.delete() return redirect(url_for('bookmark_app.index'))
def my_list(): page = get_page() pagination = Bookmark.objects(user=current_user.user).order_by('-updated_at').paginate(page, per_page=20) return render_template('bookmarks/index.html', pagination=pagination)
def create(): form = BookmarkForm() if request.method == 'GET': return render_template('bookmarks/create.html', form=form) else: if form.validate_on_submit(): bookmark = Bookmark(title=form.title.data, is_private=form.is_private.data, description=form.description.data, user=current_user.user) bookmark.save() return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id)) return render_template('bookmarks/create.html', form=form)
def view_bookmarks(username): page = get_page() user = User.objects(username=username).get_or_404() pagination = Bookmark.objects(user=user).order_by('-updated_at').paginate( page, per_page=20) return render_template('bookmarks/index.html', pagination=pagination)
def view_bookmarks(username): page = get_page() user = User.objects(username=username).get_or_404() pagination = Bookmark.objects(user=user).order_by('-updated_at').paginate(page, per_page=20) return render_template('bookmarks/index.html', pagination=pagination)
def signup(): form = SignupForm() if request.method == 'GET': return render_template('users/signup.html', form=form) else: if form.validate_on_submit(): user = User() form.populate_obj(user) user.save() bookmark = Bookmark(user=user, title=u"%s 的收藏夹" % user.username, is_default=True) bookmark.save() user_mixin = LoginManagerUser(user) login_user(user_mixin) send_confirm_email(current_app.config, user.email) return redirect(url_for('site_app.index')) return render_template('users/signup.html', form=form)
def view(hash_id): bookmark = Bookmark.objects(hash_id=hash_id).get_or_404() bookmark_hash_id = None if current_user.is_authenticated and current_user.user == bookmark.user: bookmark_hash_id = bookmark.hash_id return render_template('bookmarks/view.html', bookmark=bookmark, bookmark_hash_id=bookmark_hash_id)
def index(): page = get_page() pagination = Paste.objects(is_private=False).order_by('-updated_at').paginate(page=page, per_page=20) print datetime.today() return render_template('index.html', pagination=pagination, hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10], pastes_count=Paste.objects().count(), comments_count=Comment.objects().count(), users_count=User.objects().count(), syntax_count=Syntax.objects().count(), bookmarks_count=Bookmark.objects().count(), users_increased=User.objects(created_at__gt=date.today()).count(), pastes_increased=Paste.objects(created_at__gt=date.today()).count(), comments_increased=Comment.objects(created_at__gt=date.today()).count(), bookmarks_increased=Bookmark.objects(created_at__gt=date.today()).count(), tags=Tag.objects().order_by('-popularity')[:10])
def view_paste(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) paste.increase_views() paste_lists = [] if current_user.is_authenticated: paste_lists = Bookmark.objects(user=current_user.user) return render_template('pastes/view.html', paste=paste, paste_lists=paste_lists)
def delete(hash_id): bookmark = Bookmark.objects(hash_id=hash_id).get_or_404() if bookmark.user != current_user.user: abort(401) if bookmark.is_default: abort(500, u'不能删除默认收藏夹') bookmark.delete() return redirect(url_for('bookmark_app.index'))
def manage(): form = UserInfoForm(request.form) if form.validate(): if current_user.is_authenticated: current_user.user.username = form.username.data return redirect('/') else: user = User(email=form.email.data, username=form.username.data, is_email_confirmed=True) user.save() bookmark = Bookmark(user=user, title=u"%s 的收藏夹" % user.username, is_default=True) bookmark.save() user_mixin = LoginManagerUser(user) login_user(user_mixin) if 'email' in session: del(session['email']) return redirect('/') return render_template('users/manage.html', form=form)
def index(): page = get_page() pagination = Paste.objects( is_private=False).order_by('-updated_at').paginate(page=page, per_page=20) print datetime.today() return render_template( 'index.html', pagination=pagination, hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10], pastes_count=Paste.objects().count(), comments_count=Comment.objects().count(), users_count=User.objects().count(), syntax_count=Syntax.objects().count(), bookmarks_count=Bookmark.objects().count(), users_increased=User.objects(created_at__gt=date.today()).count(), pastes_increased=Paste.objects(created_at__gt=date.today()).count(), comments_increased=Comment.objects( created_at__gt=date.today()).count(), bookmarks_increased=Bookmark.objects( created_at__gt=date.today()).count(), tags=Tag.objects().order_by('-popularity')[:10])
def view_paste(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) paste.increase_views() paste_lists = [] if current_user.is_authenticated: paste_lists = Bookmark.objects(user=current_user.user) syntax_list = [code.syntax for code in paste.codes] related_pastes = Paste.objects(codes__syntax__in=syntax_list).order_by('-created_at')[:10] return render_template('pastes/view.html', paste=paste, related_pastes=related_pastes, paste_lists=paste_lists)
def remove_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 not in bookmark.pastes: return render_template('error.html', title=u'该代码集合已经收藏夹中移除', message=u'该代码集合已经在收藏夹中移除') bookmark.pastes.remove(paste) bookmark.save() return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
def view_paste(hash_id): paste = Paste.objects.get_or_404(hash_id=hash_id) paste.increase_views() paste_lists = [] if current_user.is_authenticated: paste_lists = Bookmark.objects(user=current_user.user) syntax_list = [code.syntax for code in paste.codes] related_pastes = Paste.objects( codes__syntax__in=syntax_list).order_by('-created_at')[:10] return render_template('pastes/view.html', paste=paste, related_pastes=related_pastes, paste_lists=paste_lists)
def remove_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 not in bookmark.pastes: return render_template('error.html', title=u'该代码集合已经收藏夹中移除', message=u'该代码集合已经在收藏夹中移除') bookmark.pastes.remove(paste) bookmark.save() return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
def add_paste(): if 'paste_hash_id' not in request.form or 'paste_list_id' not in request.form: abort(404) paste_list = Bookmark.objects(hash_id=request.form['paste_list_id']).get_or_404() paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404() if paste in paste_list.pastes: return render_template('error.html', title=u'该代码集合已经在收藏夹中', message=u'该代码集合已经在收藏夹中') if len(paste_list.pastes) >= 10: return render_template('error.html', title=u'一个收藏夹最多只能有10个代码集合', message=u'一个收藏夹最多只能有10个代码集合') if paste not in paste_list.pastes: paste_list.pastes.append(paste) paste_list.save() return redirect(url_for('bookmark_app.view_list', hash_id=paste_list.hash_id))
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 view_list(hash_id): paste_list = Bookmark.objects(hash_id=hash_id).get_or_404() return render_template('bookmarks/view.html', paste_list=paste_list)
def status(): return jsonify(version=current_app.config['VERSION'], pastes=Paste.objects().count(), bookmarks=Bookmark.objects().count(), tags=Tag.objects().count(), users=User.objects().count())
def public_bookmarks_count(self): return Bookmark.objects(user=self, is_private=False).count()