搜索功能是一個(gè)項(xiàng)目個(gè)性化需求最強(qiáng)烈的部分,用戶想要不同的報(bào)表,可以通過(guò)搜索不同的字段來(lái)實(shí)現(xiàn)。
項(xiàng)目描述
- 管理員用戶可以搜索本部門(mén)下所有用戶的各個(gè)月份的相關(guān)信息拗秘。
- 一般用戶只能搜索各個(gè)月份自己的相關(guān)信息。
實(shí)現(xiàn)搜索頁(yè)面
- 創(chuàng)建forms 類(lèi),使用 flask-wtf 祈惶。
- 創(chuàng)建搜索頁(yè)面的 html 模板雕旨。
- 不同的權(quán)限搜索頁(yè)面是不同的。
此處使用flask的基本功能捧请,不再贅述奸腺,詳情請(qǐng)參見(jiàn)代碼。
在試圖函數(shù)中實(shí)現(xiàn)相關(guān)搜索功能
views.py
中的內(nèi)容如下:
@show.route('/01', methods = ['GET', 'POST'])
@login_required
def _01():
search_form = SearchForm(prefix='search')
...
if current_user.role == 'admin':
...
...
if search_form.validate_on_submit():
database = db.session.query(OusiStaff.department, OusiStaff.name.label('staff_name'), OusiStaff.phone,
OusiStaff.role,
g1.name.label('guest_name'), g1.month, g1.balance,
func.nvl(db.session.query(g2.balance).filter(
g1.name==g2.name,
func.to_date(g2.month, 'yyyy-mm')==func.add_months(
func.to_date(g1.month, 'yyyy-mm'), -1)
), 0).label('last_balance')
).filter(
OusiStaff.phone == g1.staff_phone, current_user.department==OusiStaff.department,
and_(g1.month.between(search_form.start_time.data.strip(), search_form.end_time.data.strip()),
OusiStaff.name.like('%{}%'.format(search_form.name.data.strip())), OusiStaff.phone.like('%{}%'.format(search_form.phone.data.strip())))
).order_by(g1.name).group_by(OusiStaff.department, OusiStaff.name.label('staff_name'), OusiStaff.phone,
OusiStaff.role,
g1.name.label('guest_name'), g1.month, g1.balance)
...
else:
...
...
if search_form.validate_on_submit():
database = db.session.query(OusiStaff.department, OusiStaff.name.label('staff_name'), OusiStaff.phone,
OusiStaff.role,
g1.name.label('guest_name'), g1.month, g1.balance,
func.nvl(db.session.query(g2.balance).filter(
g1.name == g2.name,
func.to_date(g2.month, 'yyyy-mm') == func.add_months(
func.to_date(g1.month, 'yyyy-mm'), -1)
), 0).label('last_balance')
).filter(
OusiStaff.phone == g1.staff_phone, current_user.phone == OusiStaff.phone,
g1.month.between(search_form.start_time.data.strip(), search_form.end_time.data.strip())
).order_by(g1.name).group_by(OusiStaff.department, OusiStaff.name.label('staff_name'), OusiStaff.phone,
OusiStaff.role,
g1.name.label('guest_name'), g1.month, g1.balance)
...
...
return render_template('show/01.html', data=data, searchForm=search_form)
這是第一個(gè)頁(yè)面的的搜索功能實(shí)現(xiàn)方法血久,使用了 if current_user.role == 'admin' else ...
來(lái)區(qū)分權(quán)限突照。
@show.route('/02', methods = ['GET', 'POST'])
@login_required
def _02():
search_form = SearchForm(prefix='search')
...
...
if current_user.role == 'admin':
...
...
if search_form.validate_on_submit():
database = db.session.query(sbq.c.department, sbq.c.role, sbq.c.staff_name, sbq.c.staff_phone,
sbq.c.month, func.count(sbq.c.guest_name).label('members'),
func.sum(sbq.c.balance).label('balance'),
func.sum(sbq.c.last_balance).label('last_balance')). \
filter(and_(sbq.c.month.between(search_form.start_time.data.strip(), search_form.end_time.data.strip()),
sbq.c.staff_name.like('%{}%'.format(search_form.name.data.strip())),
sbq.c.staff_phone.like('%{}%'.format(search_form.phone.data.strip())))
).group_by(sbq.c.department, sbq.c.role,
sbq.c.staff_name, sbq.c.staff_phone,
sbq.c.month)
else:
...
...
if search_form.validate_on_submit():
database = db.session.query(sbq.c.department, sbq.c.role, sbq.c.staff_name, sbq.c.staff_phone,
sbq.c.month, func.count(sbq.c.guest_name).label('members'),
func.sum(sbq.c.balance).label('balance'),
func.sum(sbq.c.last_balance).label('last_balance')). \
filter(sbq.c.month.between(search_form.start_time.data.strip(), search_form.end_time.data.strip())).group_by(sbq.c.department, sbq.c.role,
sbq.c.staff_name, sbq.c.staff_phone,
sbq.c.month)
...
...
return render_template('show/02.html', data=data, searchForm=search_form)
這是第二頁(yè)面的實(shí)現(xiàn)方法。
結(jié)果展示
第一個(gè)頁(yè)面
016.gif
第二個(gè)頁(yè)面
017.gif
這兩個(gè)動(dòng)圖展示的是管理員權(quán)限的用戶的搜索查詢界面氧吐。
總結(jié):
本實(shí)現(xiàn)方法還是很繁瑣的讹蘑,大家如果對(duì) sql 語(yǔ)句很熟悉的話,在區(qū)分權(quán)限的時(shí)候筑舅,應(yīng)該知道case ... when ... else ... end
這個(gè)語(yǔ)句座慰,如何把這樣的 sql 語(yǔ)句 sqlalchemy 化,還得精進(jìn)自己的技能翠拣,如果你恰好會(huì)這樣的技能版仔,請(qǐng)通知我一聲。