def comment_submitted(comment): message = Message(comment=comment, status='未发送') db.session.add(message) db.session.commit() comment_info = get_comment_show_info(comment) if comment.parent == 0: recipient = '*****@*****.**' body = current_plugin.render_template('message_to_author.html', comment_info=comment_info, author_name=comment.author.name, author_body=comment.body_html) else: parent_comment = Comment.query.get(comment.parent).first() recipient = parent_comment.author.email if recipient is None or recipient == '': return body = current_plugin.render_template( 'message_to_recipient.html', comment_info=comment_info, recipient_name=parent_comment.author.name, author_name=comment.author.name, author_body=comment.body_html, recipient_body=parent_comment.body_html) subject = '[' + comment_info['title'] + '] ' + '一文有新的评论' redis_url = current_app.config['REDIS_URL'] with Connection(redis.from_url(redis_url)): q = Queue() job = q.enqueue(send_mail, recipient, subject, body, message.id) message.job_id = job.id db.session.commit()
def show_article_widget(article): return { 'slug': 'toc', 'name': '文章目录', 'html': current_plugin.render_template('widget_content_toc.html', article=article), 'script': current_plugin.render_template('widget_script_toc.html'), 'style': current_plugin.render_template('widget_style_toc.html') }
def main_widget(request): def get_metas(article): return Signal.send('article_list_item_meta', article=article) page = request.args.get('page', 1, type=int) query = Article.query_published().order_by(Article.timestamp.desc()) query = {'query': query} filter(query, request.args) query = query['query'] pagination = query.paginate(page, per_page=get_setting('items_per_page').value, error_out=False) articles = pagination.items return { 'slug': 'article_list', 'name': '文章列表', 'html': current_plugin.render_template('widget_article_list', 'widget.html', articles=articles, pagination=pagination, request_params=request.args, get_metas=get_metas) }
def get_admin_widget_article_list(params): def get_articles(repository_id): return Article.query.filter_by(repository_id=repository_id).order_by( Article.version_timestamp.desc()).all() page = 1 if 'page' in params: page = int(params['page']) query = db.session.query(Article.repository_id).group_by( Article.repository_id).order_by(Article.version_timestamp.desc()) query = {'query': query} filter(query, request.args) query = query['query'] pagination = query.paginate(page, per_page=get_setting('items_per_page').value, error_out=False) repository_ids = [item[0] for item in pagination.items] custom_columns = Signal.send('custom_list_column') return current_plugin.render_template('list.html', repository_ids=repository_ids, pagination={ 'pagination': pagination, 'fragment': {}, 'url_for': plugin_url_for, 'url_for_params': { 'args': ['list'], 'kwargs': { '_component': 'admin' } } }, get_articles=get_articles, custom_columns=custom_columns)
def list_tags(): if request.method == 'POST': if request.form['action'] == 'delete': result = delete(request.form['id']) return jsonify(result) else: page = request.args.get('page', 1, type=int) pagination = Comment.query.order_by(desc(Comment.timestamp)).paginate( page, per_page=get_setting('items_per_page').value, error_out=False) comments = pagination.items return current_plugin.render_template( 'list.html', comments=comments, get_comment_show_info=get_comment_show_info, pagination={ 'pagination': pagination, 'fragment': {}, 'url_for': plugin_url_for, 'url_for_params': { 'args': ['list'], 'kwargs': { '_component': 'admin' } } })
def dispatch(): if request.method == 'POST': if request.form['action'] == 'delete': result = delete(request.form['id']) return jsonify(result) else: page = request.args.get('page', 1, type=int) pagination = Tag.query.order_by(Tag.name).paginate( page, per_page=get_setting('items_per_page').value, error_out=False) tags = pagination.items return current_plugin.render_template( 'list.html', tags=tags, pagination={ 'pagination': pagination, 'fragment': {}, 'url_for': plugin_url_for, 'url_for_params': { 'args': ['list'], 'kwargs': { '_component': 'admin' } } }, admin_article_list_url=admin_article_list_url)
def edit_page(): if request.method == 'POST': title = request.form['title'] slug = request.form['slug'] body = request.form['body'] timestamp = datetime.utcfromtimestamp(int(request.form['timestamp'])) page = Page.query.get(int(request.form['id'])) if page.repository_id is None: repository_id = str(uuid4()) else: repository_id = page.repository_id new_page = Page(title=title, slug=slug, body=body, timestamp=timestamp, author=page.author, repository_id=repository_id, status='published') Signal.send('duplicate', old_page=page, new_page=new_page) widgets_dict = json.loads(request.form['widgets']) for slug, js_data in widgets_dict.items(): Signal.send('submit_edit_widget', slug=slug, js_data=js_data, page=new_page) db.session.add(new_page) db.session.commit() else: cleanup_temp_page() if 'id' in request.args: page = Page.query.get(int(request.args['id'])) else: page = Page(status='temp') db.session.add(page) db.session.commit() widgets = [] widgets.extend(Signal.send('edit_widget', page=page)) return current_plugin.render_template('edit.html', page=page, widgets=widgets)
def list_messages(): if request.method == 'POST': if request.form['action'] == 'resend': comment_id = request.form['comment_id'] comment_submitted(sender=None, comment=Comment.query.get(comment_id)) elif request.form['action'] == 'rerun': job_id = request.form['job_id'] redis_url = current_app.config['REDIS_URL'] with Connection(redis.from_url(redis_url)): fq = get_failed_queue() fq.requeue(job_id) else: page = request.args.get('page', 1, type=int) pagination = Message.query.paginate( page, per_page=get_setting('items_per_page').value, error_out=False) messages = pagination.items with Connection(redis.from_url(current_app.config['REDIS_URL'])): queue = Queue() return current_plugin.render_template('list.html', messages=messages, queue=queue, pagination={ 'pagination': pagination, 'fragment': {}, 'url_for': plugin_url_for, 'url_for_params': { 'args': ['list'], 'kwargs': { '_component': 'admin' } } })
def show_article_widget(article): def article_url(article): return Signal.send('article_url', 'article', article=article, anchor=None) prev_next_articles = [] prev_article = Article.query_published().filter( Article.timestamp < article.timestamp).order_by( Article.timestamp.desc()).limit(1).first() if prev_article is not None: prev_next_articles.append(prev_article) next_article = Article.query_published().filter( Article.timestamp > article.timestamp).order_by( Article.timestamp).limit(1).first() if next_article is not None: prev_next_articles.append(next_article) return { 'slug': 'prev_next_articles', 'name': '上一篇/下一篇文章', 'html': current_plugin.render_template('widget_content.html', prev_next_articles=prev_next_articles, article_url=article_url), 'is_html_as_list': True }
def show_widget(session, comments, meta): js_str, true_str = confuse_string() session['js_captcha'] = true_str return { 'slug': 'comment', 'html': current_plugin.render_template( 'comment.html', comments=comments, ENABLE_TENCENT_CAPTCHA=ENABLE_TENCENT_CAPTCHA), 'script': current_plugin.render_template( 'comment.js.html', meta=meta, ENABLE_TENCENT_CAPTCHA=ENABLE_TENCENT_CAPTCHA, js_captcha_str=js_str) }
def dynamic_page(): articles = Article.query_published().order_by( Article.timestamp.desc()).all() custom_columns = Signal.send('custom_contents_column') return { 'title': '文章目录', 'slug': 'list', 'html': current_plugin.render_template('dynamic_page_contents', 'contents.html', articles=articles, custom_columns=custom_columns), 'script': current_plugin.render_template('dynamic_page_contents', 'contents.js.html'), 'style': '' }
def show_page(slug): def get_pages(repository_id): return Page.query.filter_by(repository_id=repository_id).order_by(Page.timestamp.desc()).all() page = Page.query.filter_by(slug=slug).order_by(Page.version_timestamp.desc()) if 'version' in request.args: page = page.filter_by(number=request.args['version']) page = page.first() if page is not None: left_widgets = [] right_widgets = [] after_page_widgets = [] cookies_to_set = {} metas = Signal.send('meta', page=page) widgets = Signal.send('show_page_widget', session=session, page=page) for widget in widgets: if widget['slug'] == 'comment': after_page_widgets.append(widget) Signal.send('on_showing_page', page=page, request=request, cookies_to_set=cookies_to_set) Signal.send('modify_page_when_showing', page=page) resp = make_response(current_plugin.render_template('page.html', page=page, after_page_widgets=after_page_widgets, left_widgets=left_widgets, right_widgets=right_widgets, get_pages=get_pages, metas=metas)) for key, value in cookies_to_set.items(): resp.set_cookie(key, value) return resp else: page = None dynamic_pages = Signal.send('dynamic_page') for dynamic_page in dynamic_pages: if dynamic_page['slug'] == slug: page = dynamic_page break if page is None: abort(404) left_widgets = [] right_widgets = [] scripts = [] styles = [] scripts.append(page['script']) styles.append(page['style']) resp = make_response(current_plugin.render_template('dynamic_page.html', page=page, left_widgets=left_widgets, right_widgets=right_widgets, scripts=scripts, styles=styles)) return resp
def test_send_mail(): if request.method == 'GET': return current_plugin.render_template('test_send_mail.html') elif request.method == 'POST': recipient = request.form.get('recipient') subject = request.form.get('subject') body = request.form.get('body') result = Signal.send('send_mail', recipient=recipient, subject=subject, body=body) return jsonify(result)
def me(): if is_authorized(): api_base_url = get_setting('api_base_url').value try: f = opener.open(api_base_url + '/me') me = json.loads(f.read().decode()) except urllib.error.HTTPError as e: me = None else: me = None return current_plugin.render_template('me.html', me=me, login_url=plugin_url_for( 'login', _component='admin'))
def main_widget(): comments = Comment.query.order_by(Comment.timestamp.desc()).limit(10).all() return { 'slug': 'latest_comments', 'name': '最近回复', 'html': current_plugin.render_template( 'widget_latest_comments', 'widget.html', comments=comments, get_comment_show_info=get_comment_show_info), 'is_html_as_list': True }
def main_widget(): def index_url(**kwargs): return Signal.send('index_url', 'main', **kwargs) all_category = Category.query.order_by(Category.name).all() return { 'slug': 'category', 'name': '分类', 'html': current_plugin.render_template('widget_list', 'widget.html', all_category=all_category, index_url=index_url), 'is_html_as_list': True }
def edit_tag(): if request.method == 'GET': id = request.args.get('id', type=int) tag = None if id is not None: tag = Tag.query.get(id) return current_plugin.render_template('edit.html', tag=tag) else: id = request.form.get('id', type=int) if id is None: tag = Tag() else: tag = Tag.query.get(id) tag.name = request.form['name'] tag.slug = request.form['slug'] tag.description = request.form['description'] if tag.id is None: db.session.add(tag) db.session.commit() return redirect(plugin_url_for('list', _component='admin'))
def show_article(number): def get_articles(repository_id): return Article.query.filter_by(repository_id=repository_id).order_by( Article.timestamp.desc()).all() article = Article.query.filter_by(number=number) if 'version' in request.args: article = article.filter_by(number=request.args['version']) article = article.first_or_404() cookies_to_set = {} metas = Signal.send('meta', article=article) header_keywords = Signal.send('header_keyword', article=article) widgets = Signal.send('show_article_widget', session=session, article=article) right_widgets = widgets['right'] left_widgets = widgets['left'] after_article_widgets = widgets['bottom'] Signal.send('on_showing_article', article=article, request=request, cookies_to_set=cookies_to_set) Signal.send('modify_article_when_showing', article=article) resp = make_response( current_plugin.render_template( 'article.html', article=article, after_article_widgets=after_article_widgets, left_widgets=left_widgets, right_widgets=right_widgets, get_articles=get_articles, metas=metas, header_keywords=header_keywords)) for key, value in cookies_to_set.items(): resp.set_cookie(key, value) return resp
def get_widget(attachments, meta): return current_plugin.render_template('widget_edit.html', attachments=attachments, meta=meta)
def _article_meta(article): return current_plugin.render_template('num_comments.html', comments=article.comments)
def get_rendered_tag_items(comments): return current_plugin.render_template('num_comments.html', comments=comments)
def page_meta(page): return current_plugin.render_template('num_comments.html', comments=page.comments)
def content_func(article): return current_plugin.render_template('article_contents_item.html', comments=article.comments)
def get_rendered_view_count(repository_id): view_count = ViewCount.query.filter_by(repository_id=repository_id).first() if view_count is not None: return current_plugin.render_template('view_count.html', view_count=view_count.count)
def content_func(article): return current_plugin.render_template( 'admin_tag_items.html', article=article, admin_article_list_url=admin_article_list_url)
def article_edit_widget(article): all_tag_name = [tag.name for tag in Tag.query.all()] tag_names = [tag.name for tag in article.tags] return current_plugin.render_template('widget_edit_article.html', all_tag_name=all_tag_name, tag_names=tag_names)
def _article_meta(article): return current_plugin.render_template('tag_items.html', tags=article.tags)
def _article_meta(article): return current_plugin.render_template('category_items.html', categories=article.categories)
def content_func(article): return current_plugin.render_template( 'article_contents_item.html', view_count=ViewCount.query.filter_by( repository_id=article.repository_id).first().count)
def article_edit_widget(article): all_category = Category.query.all() category_ids = [category.id for category in article.categories] return current_plugin.render_template('widget_edit_article.html', all_category=all_category, category_ids=category_ids)