def create(): """ 创建栏目 """ # curl 'http://127.0.0.1:5000/category/v1/create' form = CategoryForm(request.form) model = Category() form.populate_obj(model) model.save() return render_json({"code": 0})
def create(): """ 创建新的文章 http://127.0.0.1:5000/entry/v1/create?category_id=1 @param category_id: @return: """ try: category_id = int(request.args.get('category_id', 0)) except: return render_json({'code': ResultCode.FAILED, 'message': u'category_id无效'}) category = Category.get_by_id(category_id) if not category: return render_json({'code': ResultCode.FAILED, 'message': u'没有有效的分类/栏目'}) # 处理各类权限 # if category.show_role: # if not g.user or category.show_role > g.user.role: # abort(404) # FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] form = FormClass(request.form, next=request.args.get('next', None), entry_type=category.entry_type) if request.method == 'GET': return render_json({'code': ResultCode.FAILED, 'message': u'仅支持POST请求'}) if form.validate(): # 获取指定的表单数据 entry = ModelClass() form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True # print category.to_dict() # todo user.id if not ModelClass.add_or_update(category, entry, is_draft, True): return render_json({'code': ResultCode.FAILED, 'message': u'add_or_update error'}) return render_json({'code': ResultCode.SUCCESS, 'data': entry}) elif form.errors: if __debug__: print "form errors", form.validate() _list = [] for error_name, error_value in form.errors.iteritems(): if __debug__: print "error: %s %s" % (error_name, error_value) _list.append((error_name, error_value)) return render_json({'code': ResultCode.SYSTEM_ERROR, 'message': _list})
def get_list(category_slug, page=1, page_size=20): """ 获取指定栏目下的内容列表 :param category_slug: :param page: :param page_size: :return: """ # GET /entry/1/1_20.html # curl 'http://127.0.0.1:5000/software/1_20.json?q=www&s=created_at&o=asc' query, sort, order = get_query_info(request) category = Category.get_by_slug(category_slug) if not category: abort(404) # 通过反射获得数据类 ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[ category.entry_type.code]] _total, _pages, _list = ModelClass.get_page_by_category_id( category.id, sort, order, page, page_size) return render_json({ "data_list": _list, "total": _total, "page": page, "page_size": page_size })
def show_list(category_slug, page=1, page_size=20): """ 显示指定栏目下的内容列表 :param category_slug: :param page: :param page_size: :return: """ # GET /software/1/1_20 # curl 'http://127.0.0.1:5000/software/1_20.html?q=www&s=created_at&o=asc' query, sort, order = get_query_info(request) category = Category.get_by_slug(category_slug) if not category: abort(404) ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[ category.entry_type.code]] _total, _pages, _list = ModelClass.get_page_by_category_id( category.id, sort, order, page, page_size) return render_html(category.list_template, data_list=_list, category_slug=category_slug, total=_total, page=page, page_size=page_size)
def get_list(page=1, page_size=20): """ 获取栏目列表 :param page: 指定页 :param page_size: 每页的条目数 :param q: 查询关键词 :param s: 排序字段 :param o: 排序次序(asc、desc) :return: """ # GET /category/v1/1_20.json # curl 'http://127.0.0.1:5000/category/v1/1_20.json?q=www&s=created_at&o=asc' query, sort, order = get_query_info(request) _total, _pages, _list = Category.get_page_with_query( query, sort, order, page, page_size) print _total, _pages print _list return render_json({ "data_list": _list, "total": _total, "page": page, "page_size": page_size })
def show(category_slug, entry_slug): """ 显示内容(文章,etc) @param slug: @return: """ category = Category.get_by_slug(category_slug) if not category: abort(404) FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[ category.entry_type.code]] entry = ModelClass.query.filter_by(slug=entry_slug).first() if entry is None: abort(404) # # todo g.user.is_supervisor # if entry.entry_status != EntryStatus.published: # if g.user and not g.user.is_supervisor: # return render_template('common/entry_pending.html') # # if entry.category.show_role: # if not g.user or entry.category.show_role > g.user.role: # abort(404) # # query, sort, order, page = query_condition() # # # # return render_show(entry, sort, order, page) category = entry.category show_template = category.show_template return render_template( show_template, current_category=entry.category, category_slug=entry.category, entry=entry, page_url=entry.slug, )
def show(category_slug, entry_slug): """ 显示内容(文章,etc) @param slug: @return: """ category = Category.get_by_slug(category_slug) if not category: abort(404) FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] entry = ModelClass.query.filter_by(slug=entry_slug).first() if entry is None: abort(404) # # todo g.user.is_supervisor # if entry.entry_status != EntryStatus.published: # if g.user and not g.user.is_supervisor: # return render_template('common/entry_pending.html') # # if entry.category.show_role: # if not g.user or entry.category.show_role > g.user.role: # abort(404) # # query, sort, order, page = query_condition() # # # # return render_show(entry, sort, order, page) category = entry.category show_template = category.show_template return render_template( show_template, current_category=entry.category, category_slug=entry.category, entry=entry, page_url=entry.slug, )
def create(): """ 创建新的文章 @param category_id: @return: """ new_templates = 'entry/create.html' form = SoftwareForm(request.form, next=request.args.get('next', None), entry_type=1) if request.method == 'POST' and form.validate(): # 获取指定的表单数据 entry = Entry() form.populate_obj(entry) category = Category.get_by_id(int(1)) is_draft = False #if form.draft.data: # is_draft = True print entry.title if not Entry.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 render_json({"success": "true", "id": entry.id}) elif form.errors: print "oooooooooooooooooooooooooooo", form.validate() for error_name, error_value in form.errors.iteritems(): print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") return render_html(new_templates, form=form)
def index(): # redirect("/static/log/index.html") # print request # print request.host_url # # print request.full_path # # print # print dir(request) # current_app.logger.error(u'请注意:出现内部错误') # raise Exception('ERROR123') # raise Exception(u'出现内部错误123456qqq') print current_user.username, current_user.is_authenticated() #todo 获取栏目列表 return render_html('content/index.html', url_base=request.base_url, category_list=Category.get_all())
def get_list(page=1, page_size=20): """ 获取栏目列表 :param page: 指定页 :param page_size: 每页的条目数 :param q: 查询关键词 :param s: 排序字段 :param o: 排序次序(asc、desc) :return: """ # GET /category/v1/1_20.json # curl 'http://127.0.0.1:5000/category/v1/1_20.json?q=www&s=created_at&o=asc' query, sort, order = get_query_info(request) _total, _pages, _list = Category.get_page_with_query(query, sort, order, page, page_size) print _total, _pages print _list return render_json({"data_list": _list, "total": _total, "page": page, "page_size": page_size})
def show_list(category_slug, page=1, page_size=20): """ 显示指定栏目下的内容列表 :param category_slug: :param page: :param page_size: :return: """ # GET /software/1/1_20 # curl 'http://127.0.0.1:5000/software/1_20.html?q=www&s=created_at&o=asc' query, sort, order = get_query_info(request) category = Category.get_by_slug(category_slug) if not category: abort(404) ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] _total, _pages, _list = ModelClass.get_page_by_category_id(category.id, sort, order, page, page_size) return render_html(category.list_template, data_list=_list, category_slug=category_slug, total=_total, page=page, page_size=page_size)
def get_list(category_slug, page=1, page_size=20): """ 获取指定栏目下的内容列表 :param category_slug: :param page: :param page_size: :return: """ # GET /entry/1/1_20.html # curl 'http://127.0.0.1:5000/software/1_20.json?q=www&s=created_at&o=asc' query, sort, order = get_query_info(request) category = Category.get_by_slug(category_slug) if not category: abort(404) # 通过反射获得数据类 ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] _total, _pages, _list = ModelClass.get_page_by_category_id(category.id, sort, order, page, page_size) return render_json({"data_list": _list, "total": _total, "page": page, "page_size": page_size})
def category(self): print 'self.category_id: ', self.category_id return Category.get_by_id(self.category_id)
def create(): """ 创建新的文章 http://127.0.0.1:5000/entry/create?category_id=1 @param category_id: @return: """ try: category_id = int(request.args.get('category_id', 0)) except: category_id = 0 abort(404) category = Category.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 FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[ category.entry_type.code]] form = FormClass(request.form, next=request.args.get('next', None), entry_type=category.entry_type) if request.method == 'POST': if form.validate(): # 获取指定的表单数据 entry = ModelClass() form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True if not ModelClass.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 if not next_url or next_url == request.path: print 'category.slug: ', category.slug print 'entry.slug: ', entry.slug next_url = url_for('frontend.show', category_slug=category.slug, entry_slug=entry.slug) return redirect(next_url) elif form.errors: if __debug__: print "form errors", form.validate() for error_name, error_value in form.errors.iteritems(): if __debug__: print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") return render_html(new_templates, form=form)
def edit(): # http://127.0.0.1:5000/entry/edit?category_id=1&entry_id=1 # int_value_verify(id) try: category_id = int(request.args.get('category_id', 0)) except: category_id = 0 abort(404) category = Category.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) # FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[ category.entry_type.code]] try: entry_id = int(request.args.get('entry_id', 0)) except: entry_id = 0 abort(404) entry = ModelClass.get_by_id(entry_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 = FormClass(next=request.args.get('next', None), id=entry_id, obj=entry) else: form = FormClass(next=request.args.get('next', None), id=entry_id) if form.validate(): form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True if not ModelClass.add_or_update(category, entry, is_draft, True): flash(_("Internal error"), "failed") return render_template(edit_template, form=form, form_id=entry_id, category=category, entry_type=entry.entry_type, current_category=category) next_url = form.next.data if not next_url or next_url == request.path: next_url = url_for('frontend.show', category_slug=category.slug, 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") return render_template(edit_template, form=form, form_id=entry_id, category=category, entry_type=entry.entry_type, current_category=category)
def create(): """ 创建新的文章 http://127.0.0.1:5000/entry/create?category_id=1 @param category_id: @return: """ try: category_id = int(request.args.get('category_id', 0)) except: category_id = 0 abort(404) category = Category.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 FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] form = FormClass(request.form, next=request.args.get('next', None), entry_type=category.entry_type) if request.method == 'POST': if form.validate(): # 获取指定的表单数据 entry = ModelClass() form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True if not ModelClass.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 if not next_url or next_url == request.path: print 'category.slug: ', category.slug print 'entry.slug: ', entry.slug next_url = url_for('frontend.show', category_slug=category.slug, entry_slug=entry.slug) return redirect(next_url) elif form.errors: if __debug__: print "form errors", form.validate() for error_name, error_value in form.errors.iteritems(): if __debug__: print "error: %s %s" % (error_name, error_value) flash(_("Cause an error"), "failed") return render_html(new_templates, form=form)
def category_name(self): return Category.get_by_id(self.category_id).category_name
def edit(): # http://127.0.0.1:5000/entry/edit?category_id=1&entry_id=1 # int_value_verify(id) try: category_id = int(request.args.get('category_id', 0)) except: category_id = 0 abort(404) category = Category.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) # FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] try: entry_id = int(request.args.get('entry_id', 0)) except: entry_id = 0 abort(404) entry = ModelClass.get_by_id(entry_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 = FormClass(next=request.args.get('next', None), id=entry_id, obj=entry) else: form = FormClass(next=request.args.get('next', None), id=entry_id) if form.validate(): form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True if not ModelClass.add_or_update(category, entry, is_draft, True): flash(_("Internal error"), "failed") return render_template(edit_template, form=form, form_id=entry_id, category=category, entry_type=entry.entry_type, current_category=category) next_url = form.next.data if not next_url or next_url == request.path: next_url = url_for('frontend.show', category_slug=category.slug, 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") return render_template(edit_template, form=form, form_id=entry_id, category=category, entry_type=entry.entry_type, current_category=category)
def test_1(self): # user = User(username='******', email='*****@*****.**') category = Category() category.category_name = u'分类名' category.slug = '/cat1' category.logo = '/logo/1.png' category.feature_image = '/img/1.png' # 2012-11-14 加 category.thumbs_width = 1280 category.thumbs_height = 800 category.display_order = 0 # description = Column(Text, doc=u'描述') # 能浏览的角色 # read_role = set() # read_role.add(1) # read_role.add(2) # category.read_role = read_role category.read_permission = 0 # 支持投稿的角色 # post_role = set() # post_role.add(1) # post_role.add(2) # category.post_role = post_role category.post_permission = 0 # 栏目管理员 # managers = set() # managers.add(1) # managers.add(2) # category.managers = managers # 栏目类型:0-普通栏目、1-链接栏目(链接至某个页面) # 普通栏目使用list_template category.category_type = 0 category.category_url = '' category.list_template = 'list.html' category.create_template = 'create.html' category.edit_template = 'edit.html' category.show_template = 'show.html' category.search_template = 'search.html' category.download_template = 'download.html' # 文章URL自动生成规则 # 文章标题: {title_name} # 文章ID: {title_id} # 栏目名: {category_name} # 栏目固定地址: {category_slug} # 时间戳: {timestamp} # 时间: {time} # 日期: {date} category.entry_url_rule = '/archives/1' #META Title(栏目标题) category.meta_title = '' #META Keywords(栏目关键词) category.meta_keywords = '' #META Description(栏目描述) category.meta_description = '' # 移到redis中 # num_entries = Column(Integer, nullable=False, default=0, doc=u'内容数') # num_discuss = Column(Integer, nullable=False, default=0, doc=u'讨论数') # num_attention = Column(Integer, nullable=False, default=0, doc=u'关注数') # 内容的类型 category.entry_type = 0 category.need_audit = 0 category.category_status = 0 category.save() # db.session.add(category) # db.session.commit() print 'category_id: ', category.id print 'category_managers: ', category.managers, 1 in category.managers, len(category.managers) sleep(4) category.category_status = 1 category.save() obj = Category.get_by_id(1) print dict(obj) print obj.to_dict()
def edit(): # http://127.0.0.1:5000/entry/edit?category_id=1&entry_id=1 # int_value_verify(id) try: category_id = int(request.args.get('category_id', 0)) except: return render_json({'code': ResultCode.FAILED, 'message': u'category_id无效'}) category = Category.get_by_id(category_id) if not category: return render_json({'code': ResultCode.FAILED, 'message': u'没有有效的分类/栏目'}) # 处理各类权限 # if category.show_role: # if not g.user or category.show_role > g.user.role: # abort(404) # if request.method == 'GET': return render_json({'code': ResultCode.FAILED, 'message': u'仅支持POST请求'}) FormClass = globals()[ENTRY_TYPE_FORM_CLASSNAME[category.entry_type.code]] ModelClass = globals()[ENTRY_TYPE_MODEL_CLASSNAME[category.entry_type.code]] try: entry_id = int(request.args.get('entry_id', 0)) except: return render_json({'code': ResultCode.DATA_NOT_EXIST, 'message': u'内容数据不存在, entry_id: %d' % request.args.get('entry_id', 0)}) entry = ModelClass.get_by_id(entry_id) if not entry: return render_json({'code': ResultCode.DATA_NOT_EXIST, 'message': u'内容数据不存在, class: %s, entry_id: %d' % (ModelClass.__tablename__, entry_id)}) # 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 form = FormClass(request.form, next=request.args.get('next', None), entry_type=category.entry_type) if form.validate(): form.populate_obj(entry) is_draft = False if form.draft.data: is_draft = True if not ModelClass.add_or_update(category, entry, is_draft, True): return render_json({'code': ResultCode.FAILED, 'message': u'add_or_update error'}) return render_json({'code': ResultCode.SUCCESS, 'data': entry}) elif form.errors: _list = [] for error_name, error_value in form.errors.iteritems(): if __debug__: print "error: %s %s" % (error_name, error_value) _list.append((error_name, error_value)) return render_json({'code': ResultCode.SYSTEM_ERROR, 'message': _list})
def delete(entry): # todo 超过一定时间的也不让删除? count_entry = Comment.count_by_entry_id(entry.id) if count_entry > 0: # 软删除 pass else: db.session.delete(entry) # 删除相关评论等相关数据 Comment.del_by_entry_id(entry.id) # 分类的文章数减少 # 只支持3级分类 if entry.category.current_level == 1: Category.dec_entry_count(entry.category1_id) if entry.category.current_level == 2: Category.dec_entry_count(entry.category1_id) Category.dec_entry_count(entry.category2_id) if entry.category.current_level == 3: Category.dec_entry_count(entry.category1_id) Category.dec_entry_count(entry.category2_id) Category.dec_entry_count(entry.category3_id) # 要删除的标签 del_tags_list = json_decode(entry.tags) if del_tags_list: # 删除 tag_entry_relation (标签的文章关联) TagEntryRelationService.del_relation(entry.id, del_tags_list) TagService.dec_entry_count(del_tags_list) # 更新 tag_category_relation (标签的分类计数) TagCategoryRelationService.del_relation(entry.category, del_tags_list) db.session.delete(entry) db.session.commit() return True