def paginate_with_search(page_num=1): site = get_current_site() keywords = request.args.get('keywords', '').strip(',') if not keywords: return redirect( url_for("post_page.paginate_with_all")) # TODO:提醒用户输入关键字 post_query = Post.query.search(site=site, keywords=keywords) # TODO!!!!当前搜索多个关键字有bug if post_query.count() == 1: # only one result posts = post_query.all() post = posts[0] return redirect(url_for('post_page.one_with_id', post_id=post.id)) paginate = { 'title': keywords, 'key': keywords, 'desc': 'Search results for', 'curr_num': page_num, 'view_name': 'post_page.paginate_with_search', 'keywords': keywords, } return _render_paginate_post_page(post_query, paginate)
def list(post_type): publish_status_display_list = [ PUBLISH_FSM_DEFINE.STATE.DRAFT, PUBLISH_FSM_DEFINE.STATE.UNPUBLISHED, PUBLISH_FSM_DEFINE.STATE.PUBLISHED, PUBLISH_FSM_DEFINE.STATE.TRASH ] # for pre-sort content = {} if not is_valid_post_type(post_type): return redirect(url_for('.list', post_type='blog')) site = get_current_site() queryset = Post.query.filter_by(site=site, type=post_type) for publish_status in publish_status_display_list: count = queryset.filter_by(publish_status=publish_status).count() if count == 0: continue content[publish_status] = { 'count': count, 'list': queryset.filter_by(publish_status=publish_status).order_by( desc('published_time')).all(), } set_navbar(level1=post_type) return render_template("rpadmin/post/list.html", post_type=post_type, content=content)
def _sidebar(): site = get_current_site() # TODO!!!应该传进来 categories = [] terms = Term.query.filter_by(site=site, type='category').order_by('name').all() for term in terms: categories.append({ 'name': term.name, 'desc': term.desc, 'link': url_for('post_page.paginate_with_term_category', term=term.name), }) tags = [] terms = Term.query.filter_by(site=site, type='tag').order_by('name').all() for term in terms: tags.append({ 'name': term.name, 'desc': term.desc, 'link': url_for('post_page.paginate_with_term_tag', term=term.name), }) widgets = { 'categories': categories, 'tags': tags, 'date_years': _widget_date_year(), } return widgets
def import_wordpress_xml(): """导入 wordpress xml 文件""" site = get_current_site() if site is None: return 'site binding error' if request.method != 'POST': return render_template('rp/site_tools/upload_wordpress_xml.html') file = request.files['file'] if file is None or not allowed_file(file.filename): return 'upload file error!' filename = os.path.join(UPLOAD_FOLDER, secure_filename(file.filename)) file.save(filename) response_msg = import_data_from_wordpress_xml( db_session=db.session, site=site, disable_convert_code_tag=False, filename=filename, is_cli_mode=False, is_skip_unknow_author=False ) db.session.commit() return response_msg
def one_with_name(post_name): site = get_current_site() post = Post.query.filter_by(site=site, name=post_name, published=True, type='page').first_or_404() return _render_one_post(post)
def _post_term(term, paginate): site = get_current_site() query = Post.query.filter_by(site=site, type=POST.TYPE.BLOG, published=True).filter( Post.terms.any(name=term)).order_by( desc('published_time')) paginate['key'] = term paginate['title'] = term # TODO!!!diaplay name? return _render_paginate_post_page(query, paginate)
def list(term_type): if term_type not in ['category', 'tag']: return # !!! site = get_current_site() terms = Term.query.filter_by(site=site, type=term_type).order_by(desc('name')).all() set_navbar(level1=term_type) return render_template('rpadmin/term/list.html', terms=terms, term_type=term_type)
def paginate_with_all(page_num=1): site = get_current_site() query = Post.query.filter_by(site=site, type='blog', published=True).order_by( desc('published_time')) paginate = { 'title': 'Home', # TODO需要改为站点相关信息 'curr_num': page_num, 'view_name': 'post_page.paginate_with_all', } return _render_paginate_post_page(query, paginate)
def paginate_with_author(author, page_num=1): site = get_current_site() query = Post.query.filter_by( site=site, type='blog', published=True).filter(Post.author.has(User.name == author)).order_by( desc('published_time')) paginate = { 'title': author, # TODO!!!display name 'curr_num': page_num, 'view_name': 'post_page.paginate_with_author', } return _render_paginate_post_page(query, paginate)
def new(post_type): """""" if not is_valid_post_type(post_type): return user = User.query.filter_by(id=current_user.id).first() if user is None: return site = get_current_site() post = Post(author=user, site=site, type=post_type) db.session.add(post) db.session.commit() return redirect(url_for('.edit', post_id=post.id))
def paginate_with_year(year, page_num=1): site = get_current_site() query = Post.query.filter_by( site=site, type='blog', published=True).filter( Post.published_time >= datetime(year, 1, 1), Post.published_time < datetime(year + 1, 1, 1)).order_by( desc('published_time')) paginate = { 'title': year, 'key': year, 'desc': 'Viewing the date archives', 'curr_num': page_num, 'view_name': 'post_page.paginate_with_year', } return _render_paginate_post_page(query, paginate)
def edit(name): site = get_current_site() term = Term.query.filter_by(site=site, name=name).first_or_404() # !!! form = TermEditFrom(obj=term) if form.validate_on_submit(): form.populate_obj(term) db.session.add(term) db.session.commit() flash("term updated", "success") # return redirect(url_for('.blog')) else: flash('term edit error') pass set_navbar(level1=term.type) return render_template("rpadmin/term/edit.html", form=form, term=term)
def edit(key): site = get_current_site() site_setting = SiteSetting.query.filter_by(site=site, key=key).order_by('created_time').first() if site_setting is None: site_setting = SiteSetting( site_id=site.id, key=key, value=None, ) form = SettingsForm(obj=site_setting) if form.validate_on_submit(): form.populate_obj(site_setting) db.session.add(site_setting) db.session.commit() flash("settings updated", "success") else: flash('settings edit error') return render_template("rpadmin/settings/edit.html", form=form, site_setting=site_setting)
def one_with_id(post_id): site = get_current_site() post = Post.query.filter_by(site=site, id=post_id, published=True).first_or_404() return _render_one_post(post)