Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ok] now the fulltext search(FTS) of blog article works well. support chinese FTS(新增全文搜索功能) #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def create_app():
app.config.from_object(Config)
Config.init_app(app)
CsrfProtect(app)

db.init_app(app)
bootstrap.init_app(app)
moment.init_app(app)
Expand All @@ -34,5 +33,5 @@ def create_app():

from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth')

return app

7 changes: 7 additions & 0 deletions app/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ class ChangePasswordForm(Form):
class EditUserInfoForm(Form):
username = StringField(u'昵称', validators=[DataRequired()])
email = StringField(u'电子邮件', validators=[DataRequired(), Length(1, 64), Email()])
#userlevel = SelectField(u'权限', coerce=int, validators=[DataRequired()])
password = PasswordField(u'密码确认', validators=[DataRequired()])

class AddAccountInfoForm(Form):
username = StringField(u'昵称', validators=[DataRequired()])
email = StringField(u'电子邮件', validators=[DataRequired(), Length(1, 64), Email()])
userlevel = SelectField(u'权限', coerce=int, validators=[DataRequired()])
password = PasswordField(u'密码', validators=[DataRequired()])
50 changes: 46 additions & 4 deletions app/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .forms import SubmitArticlesForm, ManageArticlesForm, DeleteArticleForm, \
DeleteArticlesForm, AdminCommentForm, DeleteCommentsForm, AddArticleTypeForm, \
EditArticleTypeForm, AddArticleTypeNavForm, EditArticleNavTypeForm, SortArticleNavTypeForm, \
CustomBlogInfoForm, AddBlogPluginForm, ChangePasswordForm, EditUserInfoForm
CustomBlogInfoForm, AddBlogPluginForm, ChangePasswordForm, EditUserInfoForm, AddAccountInfoForm
from .. import db


Expand All @@ -23,6 +23,42 @@
def manager():
return redirect(url_for('admin.custom_blog_info'))

@admin.route('/account/add', methods=['GET', 'POST'])
@login_required
def add_account():
form = AddAccountInfoForm()
levels = [(1,u'admin'), (2,u'user')]
form.userlevel.choices = levels

if form.validate_on_submit():
#username = form.username.data
email = form.email.data
#password = form.password.data
account = User.query.filter_by(email=email).first()
if account:
form = AddAccountInfoForm(username=username, email=form.email.data,
password=form.password.data)
flash(u'添加失败!该帐号已经存在。', 'danger')
return render_template('admin/blog_account_add.html', form=form)
else:
username = form.username.data
email = form.email.data
password = form.password.data
level = form.userlevel.data
if level==1:
userlevel = "admin"
elif level ==2:
userlevel = "user"

#account_count = User.query.count()
User.insert_admin(username=username, email=email,password=password, userlevel=userlevel)
#db.session.add(plugin)
#db.session.commit()
flash(u'添加帐号成功!', 'success')
return redirect(url_for('admin.account'))

return render_template('admin/blog_account_add.html', form=form)
#form =

@admin.route('/submit-articles', methods=['GET', 'POST'])
@login_required
Expand Down Expand Up @@ -50,8 +86,9 @@ def submitArticles():
db.session.add(article)
db.session.commit()
flash(u'发表博文成功!', 'success')
article_id = Article.query.filter_by(title=title).first().id
return redirect(url_for('main.articleDetails', id=article_id))
#article_id = Article.query.filter_by(title=title).first().id
#return redirect(url_for('main.articleDetails', id=article_id))
return redirect(url_for('admin.manage_articles'))
if form.errors:
flash(u'发表博文失败', 'danger')

Expand Down Expand Up @@ -856,6 +893,11 @@ def edit_user_info():
if current_user.verify_password(form2.password.data):
current_user.username = form2.username.data
current_user.email = form2.email.data
# lvl = form2.userlevel.data
# if lvl ==1:
# current_user.userlevel = "admin"
# elif lvl ==2:
# current_user.userlevel = "user"
db.session.add(current_user)
db.session.commit()
flash(u'修改用户信息成功!', 'success')
Expand All @@ -869,4 +911,4 @@ def edit_user_info():
@login_required
def help():

return render_template('admin/help_page.html')
return render_template('admin/help_page.html')
3 changes: 3 additions & 0 deletions app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ class CommentForm(Form):
Email()])
content = TextAreaField(u'内容', validators=[DataRequired(), Length(1, 1024)])
follow = StringField(validators=[DataRequired()])

class SearchForm(Form):
search = StringField(u'搜索', validators = [DataRequired()], render_kw={"placeholder": u"请输入要查询的"})
43 changes: 40 additions & 3 deletions app/main/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
#coding:utf-8
from flask import render_template, request, current_app, redirect,\
url_for, flash
url_for, flash, g
from flask.ext.login import login_required, current_user
from . import main
from ..models import Article, ArticleType, article_types, Comment, \
Follow, User, Source, BlogView
from .forms import CommentForm
from .forms import CommentForm, SearchForm
from .. import db

@main.before_request
def before_request():
g.user = current_user
# if g.user.is_authenticated():
# g.user.last_seen = datetime.utcnow()
# db.session.add(g.user)
# db.session.commit()
# g.search_form = SearchForm()
g.search_form = SearchForm()
#g.locale = get_locale()

@main.route('/search', methods = ['POST'])
def search():
if not g.search_form.validate_on_submit():
return redirect(url_for('index'))
return redirect(url_for('main.search_results', query = g.search_form.search.data))

@main.route('/search_results/<query>')
def search_results(query):
BlogView.add_view(db)
per_page = current_app.config['ARTICLES_PER_PAGE']
max_search = current_app.config['MAX_SEARCH_RESULTS']
# pagination = Article.query.whoosh_search(query, max_search).paginate(
# page=1, per_page=current_app.config['ARTICLES_PER_PAGE'],
# error_out=False)
# print query, pagination
# articles = pagination.items
# return render_template('search_results.html', articles=articles,
# query = query,pagination=pagination, endpoint='.search_results')

articles = Article.query.whoosh_search(query, max_search).all()
print query, articles
#articles = pagination.items
return render_template('search_results.html', articles=articles,
query = query, endpoint='.search_results')


@main.route('/')
def index():
Expand Down Expand Up @@ -86,6 +123,6 @@ def articleDetails(id):
article.add_view(article, db)
return render_template('article_detials.html', User=User, article=article,
comments=comments, pagination=pagination, page=page,
form=form, endpoint='.articleDetails', id=article.id)
form=form, endpoint='main.articleDetails', id=article.id)
# page=page, this is used to return the current page args to the
# disable comment or enable comment endpoint to pass it to the articleDetails endpoint
8 changes: 6 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from werkzeug.security import generate_password_hash, check_password_hash
from flask.ext.login import UserMixin
from . import db, login_manager
from jieba.analyse import ChineseAnalyzer

article_types = {u'开发语言': ['Python', 'Java', 'JavaScript'],
'Linux': [u'Linux成长之路', u'Linux运维实战', 'CentOS', 'Ubuntu'],
Expand All @@ -19,10 +20,11 @@ class User(UserMixin, db.Model):
username = db.Column(db.String(64), unique=True, index=True)
password_hash = db.Column(db.String(128))
avatar_hash = db.Column(db.String(32))
userlevel = db.Column(db.String(32))

@staticmethod
def insert_admin(email, username, password):
user = User(email=email, username=username, password=password)
def insert_admin(email, username, password, userlevel):
user = User(email=email, username=username, password=password, userlevel=userlevel)
db.session.add(user)
db.session.commit()

Expand Down Expand Up @@ -298,7 +300,9 @@ def followed_name(self):
return self.followed.first().followed.author_name

class Article(db.Model):
__searchable__ = ['id','title', 'content', 'summary']
__tablename__ = 'articles'
__analyzer__ = ChineseAnalyzer()
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64), unique=True)
content = db.Column(db.Text)
Expand Down
5 changes: 5 additions & 0 deletions app/templates/_search_article_entry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% for article in articles %}
<div id='article-entry' class="entry-box">
{% include '_article_info.html' %}
</div>
{% endfor %}
8 changes: 8 additions & 0 deletions app/templates/_search_article_info.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="article-entry-header">
<h3 class="article-entry-title">
<a href="{{ url_for('.articleDetails', id=article.id) }}">{{ article.title }}</a>
</h3>
</div>
<div class="article-entry-sum">
<p>{{ article.summary }}</p>
</div>
36 changes: 36 additions & 0 deletions app/templates/admin/blog_account_add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends 'admin/admin_base.html' %}

{% block title %}
Blog_mini -添加帐号
{% endblock %}

{% block admin_content %}
<div class="entry-box submit-article">
<h4><strong>添加帐号</strong></h4>
<hr/>
<div>
<form class="add-blog-account" method="post" action="">
{{ form.csrf_token }}
<div class="form-group">
{{ form.username.label }}
{{ form.username(class='form-control', required='') }}
{{ form.email.label }}
{{ form.email(class='form-control') }}
{{ form.password.label }}
{{ form.password(class='form-control', required='') }}
<strong>权限:</strong>
{{ form.userlevel(class='form-control',required='',id='userlevel') }}
</div>
<div class="add-account-button">
<button type="submit" class="btn btn-success">提交</button>
</div>
</form>
</div>
</div>
{% endblock %}

{% block script %}
{{ super() }}
<script src="{{ url_for('static', filename='tinymce/js/tinymce/tinymce.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/tinymce_setup.js') }}"></script>
{% endblock %}
Loading