def delete(id): """ 删除 entry 返回 json """ int_value_verify(id) old_entry = EntryService.get_by_id(id) if not old_entry: return jsonify(success=False, redirect_url='/', data_id=id) # 防止删除别人的帖子 if old_entry.author_id != g.user.id and not g.user.is_supervisor: abort(403) next = request.args.get('next', None) if next is None: next = url_for('portal.category', category=old_entry.category.slug) flash(_("The %(name)s has been deleted", name = old_entry.title), "success") return jsonify(success=EntryService.delete(old_entry), redirect_url=next, data_id=id)
def feed_with_type(entry_type, title): feed = PostsFeed(u"%s - %s" % (app.config['SITE_NAME'], title), feed_url=request.url, url=request.url_root) if entry_type: entries_list = EntryService.get_latest_entries_by_type(entry_type, 1, 20) else: entries_list = EntryService.get_latest_entries(1, 20) for entry in entries_list: feed.add_posts(entry) return feed.get_response()
def feed_with_type(entry_type, title): feed = PostsFeed(u"%s - %s" % (app.config['SITE_NAME'], title), feed_url=request.url, url=request.url_root) if entry_type: entries_list = EntryService.get_latest_entries_by_type( entry_type, 1, 20) else: entries_list = EntryService.get_latest_entries(1, 20) for entry in entries_list: feed.add_posts(entry) return feed.get_response()
def get_people_entries(people_id, page, entry_type): """ 渲染内容列表页 :param people_id: :param page: :param entry_type: :return: """ # 内容类型检查 if entry_type is not None: check_entry_type(entry_type) int_value_verify(people_id) int_value_verify(page) query, sort, order, page = query_condition() tag = None entries_total, page_total, entries_list = EntryService.get_page_by_user( people_id, tag, entry_type, query, sort, order, page) return dict( entries_list=entries_list, record_total=entries_total, page_total=page_total, )
def favorites_comment(username): query, sort, order, page = query_condition() user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) _total, _pages, _list = CommentService.getlist_by_author_id(user.id, page) current_entry_type = 'comment' return render_template("favorites/%s.html" % current_entry_type, current_entry_type=current_entry_type, comments_list=_list, record_total=_total, page_total=_pages, current_page=page, page_url='/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def comment(username): query, sort, order, page = query_condition() user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) record_total, page_total, comments_list = CommentService.getlist_by_author_id(user.id, page, sort, order) # for comment in comments_list: # entry = EntryService.get_by_id(comment.entry_id) # comment.entry_title = entry.title # # http://127.0.0.1:5000/article/5.html#comment-90 # comment.entry_url = url_for('portal.entry', slug=entry.slug)+ "#comment-%d" % comment.id current_entry_type = 'comment' return render_template("people/%s.html" % current_entry_type, current_entry_type=current_entry_type, comments_list=comments_list, record_total=record_total, page_total=page_total, current_page=page, page_url = '/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def comment(username): query, sort, order, page = query_condition() user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) record_total, page_total, comments_list = CommentService.getlist_by_author_id( user.id, page, sort, order) # for comment in comments_list: # entry = EntryService.get_by_id(comment.entry_id) # comment.entry_title = entry.title # # http://127.0.0.1:5000/article/5.html#comment-90 # comment.entry_url = url_for('portal.entry', slug=entry.slug)+ "#comment-%d" % comment.id current_entry_type = 'comment' return render_template("people/%s.html" % current_entry_type, current_entry_type=current_entry_type, comments_list=comments_list, record_total=record_total, page_total=page_total, current_page=page, page_url='/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def message(username): """ 显示个人消息 """ query, sort, order, page = query_condition() user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) record_total, page_total, messages_list = MessageService( ).getlist_by_receiver_id(user.id, page, sort, order) current_entry_type = 'message' return render_template("people/%s.html" % current_entry_type, current_entry_type=current_entry_type, messages_list=messages_list, record_total=record_total, page_total=page_total, current_page=page, page_url='/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def render_entry(username, page, entry_type=None): user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) data = get_people_entries(user.id, page, entry_type) if entry_type is not None: current_entry_type = entry_type_str[entry_type] else: current_entry_type = 'latest' return render_template("people/%s.html" % 'latest', current_entry_type=current_entry_type, entries_list=data['entries_list'], record_total=data['record_total'], page_total=data['page_total'], current_page=page, page_url = '/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def entrytype(type): #用于显示所有文章类型的内容 if type is None: abort(404) if type >= 17: abort(404) template_list = ['article_list.html', 'news_list.html', 'code_list.html', 'software_list.html', 'forum_list.html', 'question_list.html', 'tips_list.html', 'gallery_list.html', 'video_list.html', 'audio_list.html', 'link_list.html', 'quote_list.html', 'status_list.html', 'document_list.html', 'chat_list.html', 'aside_list.html', 'special_list.html', 'list.html'] query, sort, order, page = query_condition() if sort == 'id': sort = 'updated_time' entries_total, page_total, entries_list = EntryService.get_page_by_entryType(sort, order, page, type) return render_template("entrytype/%s" % template_list[type], type=type, record_total=entries_total, page_total=page_total, current_page=page, entries_list=entries_list)
def get_people_entries(people_id, page, entry_type): """ 渲染内容列表页 :param people_id: :param page: :param entry_type: :return: """ # 内容类型检查 if entry_type is not None: check_entry_type(entry_type) int_value_verify(people_id) int_value_verify(page) query, sort, order, page = query_condition() tag = None entries_total, page_total, entries_list = EntryService.get_page_by_user(people_id, tag, entry_type, query, sort, order, page) return dict( entries_list=entries_list, record_total=entries_total, page_total=page_total, )
def entrytype(type): #用于显示所有文章类型的内容 if type is None: abort(404) if type >= 17: abort(404) template_list = [ 'article_list.html', 'news_list.html', 'code_list.html', 'software_list.html', 'forum_list.html', 'question_list.html', 'tips_list.html', 'gallery_list.html', 'video_list.html', 'audio_list.html', 'link_list.html', 'quote_list.html', 'status_list.html', 'document_list.html', 'chat_list.html', 'aside_list.html', 'special_list.html', 'list.html' ] query, sort, order, page = query_condition() if sort == 'id': sort = 'updated_time' entries_total, page_total, entries_list = EntryService.get_page_by_entryType( sort, order, page, type) return render_template("entrytype/%s" % template_list[type], type=type, record_total=entries_total, page_total=page_total, current_page=page, entries_list=entries_list)
def edit(username): """ 编辑 users """ user = UserService.get_by_username(username) if not user: abort(404) if request.method == 'GET': form = PeopleEditForm(next=request.args.get('next', None), id=id, obj=user) else: form = PeopleEditForm(next=request.args.get('next', None), id=id) if form.validate_on_submit(): # 获取指定的表单数据 form.populate_obj(user) # 保存数据 UserService.update(user) flash(_("Modify success"), "success") next_url = form.next.data if not next_url or next_url == request.path: next_url = url_for('people.show', username=username) return redirect(next_url) elif form.errors: for error_name, error_value in form.errors.iteritems(): print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") statistic = EntryService.get_statistic_by_author_id(user.id) return render_template("people/edit.html", form=form, people=user, statistic=statistic, form_id=user.id)
def render_entry(username, page, entry_type=None): user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) data = get_people_entries(user.id, page, entry_type) if entry_type is not None: current_entry_type = entry_type_str[entry_type] else: current_entry_type = 'latest' return render_template("people/%s.html" % 'latest', current_entry_type=current_entry_type, entries_list=data['entries_list'], record_total=data['record_total'], page_total=data['page_total'], current_page=page, page_url='/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def message(username): """ 显示个人消息 """ query, sort, order, page = query_condition() user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) record_total, page_total, messages_list = MessageService().getlist_by_receiver_id(user.id, page, sort, order) current_entry_type = 'message' return render_template("people/%s.html" % current_entry_type, current_entry_type=current_entry_type, messages_list=messages_list, record_total=record_total, page_total=page_total, current_page=page, page_url = '/%s/%s/%s/index' % (URL_BASE, username, current_entry_type), statistic=statistic, notices=NoticeService.get_list_for_show(), people=user)
def edit(id): int_value_verify(id) entry = EntryService.get_by_id(id) if not entry: abort(404) if entry.author.id != g.user.id and not g.user.is_supervisor: abort(404) if entry.entry_status != EntryStatus.published and not g.user.is_supervisor: abort(404) if entry.category.show_role: if not g.user or entry.category.show_role > g.user.role: abort(404) category = entry.category edit_template = category.edit_template if request.method == 'GET': form = PostEditForm(next=request.args.get('next', None), id=id, obj=entry) else: form = PostEditForm(next=request.args.get('next', None), id=id) if form.validate_on_submit(): form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True if not EntryService.add_or_update(category, entry, is_draft, True): flash(_("Internal error"), "failed") return render_template(edit_template, form=form, form_id=id, category=category, entry_type=entry.entry_type, current_category=category) next_url = url_for('portal.entry', slug=entry.slug) return redirect(next_url) elif form.errors: for error_name, error_value in form.errors.iteritems(): print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") print "====================================================================", category.id return render_template(edit_template, form=form, form_id=id, category=category, entry_type=entry.entry_type, current_category=category)
def rander_category(category, tag=None, query=None, sort=None, order=None, page=1, entryType=17): """ 渲染栏目 @param category: @return: """ template_list = ['article_list.html', 'news_list.html', 'code_list.html', 'software_list.html', 'forum_list.html', 'question_list.html', 'tips_list.html', 'gallery_list.html', 'video_list.html', 'audio_list.html', 'link_list.html', 'quote_list.html', 'status_list.html', 'document_list.html', 'chat_list.html', 'aside_list.html', 'special_list.html', 'list.html'] et = entryType if int(entryType) >= 17: entryType = 17 et = 0 # http://www.oschina.net/code/search?q=mysql&catalog=7 # http://www.oschina.net/code/list?show=reply # http://stackoverflow.com/search?q=[python]+mysql+using+sqlalchemy # 内容类型检查 if not category: abort(404) entries_total, page_total, entries_list = EntryService.get_page_by_category_and_entryType(category, tag, query, sort, order, page, et) # 侧边栏的数据 #hot_tags = TagService.get_hot_tags(category) #weekly_entry = EntryService.get_weekly_entries(category) #monthly_entry = EntryService.get_monthly_entry(category) #latest_comments = CommentService.get_latest_comments(category) if query: # 有查询,将使用搜索结果显示模板 template = category.search_template else: # 使用列表页显示模板 template = category.list_template + template_list[int(entryType)] return render_template( template, entries_list=entries_list, current_category=category, current_tag=tag, record_total=entries_total, page_total=page_total, current_page=page, sub_category_list=CategoryService.get_sub_category_by_id(category.id), #hot_tags=hot_tags, #weekly_entry=weekly_entry, #monthly_entry=monthly_entry,) #latest_comments=latest_comments) )
def oppositions(id): """ 反对 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.oppositions(id, user_id), data_id=id)
def supports(id): """ 支持 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.supports(id, user_id), data_id=id)
def difficulty(id, difficulty): """ 难易度 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.difficulty(id, user_id, difficulty), data_id=id)
def on_top(id, enable): """ 置顶 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.on_top(id, user_id, enable), data_id=id)
def retweet(id): """ 转发 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.retweet(id, user_id), data_id=id)
def uninterested(id): """ 不感兴趣 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.uninterested(id, user_id), data_id=id)
def publish(id): """ 发布 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.publish(id, user_id), data_id=id)
def scores(id, scores): """ 评分 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.scores(id, user_id, scores), data_id=id)
def useless(id): """ 有用 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.useless(id, user_id), data_id=id)
def open_comment(id): """ 打开回复、评论 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.open_comment(id, user_id), data_id=id)
def close_comment(id): """ 关闭回复、评论 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.close_comment(id, user_id), data_id=id)
def open(id): """ 显示 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.open(id, user_id), data_id=id)
def hide(id): """ 隐藏 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.hide(id, user_id), data_id=id)
def thanks(id): """ 感谢 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.thanks(id, user_id), data_id=id)
def complain(id): """ 投诉 """ int_value_verify(id) user_id = g.user.id return jsonify(success=EntryService.complain(id, user_id), data_id=id)
def show(username): """ 显示 users """ user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) return render_template("people/show.html", people=user, statistic=statistic, user=user)
def create(): """ 创建新的文章 @param category_id: @return: """ # int_value_verify(category_id) # # category = CategoryService.get_by_id(category_id) # # if not category: # abort(404) # # if category.show_role: # if not g.user or category.show_role > g.user.role: # abort(404) # # new_templates = category.create_template new_templates = '/create.html' form = PostNewForm(next=request.args.get('next', None), entry_type=1) if form.validate_on_submit(): # 获取指定的表单数据 entry = Entry() form.populate_obj(entry) category = CategoryService.get_by_id(int(entry.category_id)) is_draft = False if form.draft.data: is_draft = True if not EntryService.add_or_update(category, entry, is_draft, True): flash(_("Internal error"), "failed") return render_template(new_templates, form=form, category=category, current_category=category) flash(_("Create success"), "success") next_url = form.next.data # todo wqq: 这里能否不做跳转,送回json数据返回ID号,由前端去做处理 if not next_url or next_url == request.path: next_url = url_for('portal.entry', slug=entry.slug) return jsonify(success="true", next_url=next_url) elif form.errors: for error_name, error_value in form.errors.iteritems(): print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") return render_template(new_templates, form=form)
def change_passwd(username): """ 改变密码 """ user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) if request.method == 'GET': form = ChangePasswordForm(next=request.args.get('next', None), id=user.id, obj=user) else: form = ChangePasswordForm(next=request.args.get('next', None), id=user.id) if form.validate_on_submit(): # 获取指定的表单数据 form.populate_obj(user) # 保存数据 result = UserService.update_pwd_by_id(user.id, user.password) if result: flash(_("Modify success"), "success") else: flash(_("This old password is error"), "failed") return render_template("people/change_passwd.html", people=user, statistic=statistic, form=form, form_id=user.id) return render_template("people/change_passwd_success.html", people=user, statistic=statistic, form=form, form_id=user.id) elif form.errors: for error_name, error_value in form.errors.iteritems(): print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") return render_template("people/change_passwd.html", people=user, statistic=statistic, form=form, form_id=user.id)
def show(username): """ 显示 users """ user = UserService.get_by_username(username) if not user: abort(404) statistic = EntryService.get_statistic_by_author_id(user.id) return render_template("people/show.html", people=user, statistic=statistic, user=user)
def tag(slug): tag = TagService.get_by_slug(slug) if not tag: abort(404) feed = PostsFeed(u"%s - %s" % (app.config['SITE_NAME'], tag.tag_name), feed_url=request.url, url=request.url_root) _total, _pages, _list = EntryService.get_entry_list_by_tag_id(tag.id) for post in _list: feed.add_posts(post) return feed.get_response()
def tag(slug): tag = TagService.get_by_slug(slug) if not tag: abort(404) feed = PostsFeed(u"%s - %s" % (app.config['SITE_NAME'], tag.tag_name), feed_url=request.url, url=request.url_root) _total, _pages, _list = EntryService.get_entry_list_by_tag_id(tag.id) for post in _list: feed.add_posts(post) return feed.get_response()
def sitemap(): sitemap = render_template( 'misc/sitemap.xml', root_url=app.config['SITE_URL'], # todo # 分类 # 标签 # 文章 posts=EntryService.get_all_entries(), tags=TagService.get_all_tags(), categories=CategoryService.get_all_categories(), now=sys_now(), min=min, ) response = make_response(sitemap) response.headers['Content-Type'] = 'application/xml; charset=UTF-8' return response
def favorites(): """ 收藏 内容 """ entry_id = request.values.get('entry_id', None) title = request.values.get('title', None) tags = request.values.get('tags', None) description = request.values.get('description', None) if entry_id: entry_id = int(entry_id) int_value_verify(entry_id) user_id = g.user.id return jsonify(success=EntryService.favorites(user_id, entry_id, title, tags, description), data_id=entry_id)
def tagged(slug, page=1): # todo tag = TagService.get_by_slug(slug) if not tag: abort(404) query, sort, order, page = query_condition() entries_total, page_total, entries_list = EntryService.get_page_by_tag( tag, query, sort, order, page) return render_template( "common/tag_entries_list.html", entries_list=entries_list, current_tag=tag, record_total=entries_total, page_total=page_total, current_page=page, )
def sitemap(): sitemap = render_template( 'misc/sitemap.xml', root_url=app.config['SITE_URL'], # todo # 分类 # 标签 # 文章 posts=EntryService.get_all_entries(), tags=TagService.get_all_tags(), categories=CategoryService.get_all_categories(), now=sys_now(), min=min, ) response = make_response(sitemap) response.headers['Content-Type'] = 'application/xml; charset=UTF-8' return response