-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
81 lines (64 loc) · 2.72 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
File description.
This file is mainly to load some response of the home page
such as the title of the article, the author, fill a few, etc.
encoding: utf-8
@author: Zhang Jiajun
@contact: [email protected]
@software: Pycharm
@time: 2022/1/12
@gituhb: sanxiadaba/pythonBlog
"""
import math
from flask import Blueprint, render_template, abort
from flask.json import jsonify
from common.myLog import logDanger
from constant import howArticleInWeb
from database.article import Article
instanceArticle = Article()
index = Blueprint("index", __name__)
# Loading the home page, filling the sidebar, and articles and other related information
@index.route("/")
@logDanger
def home():
# The main objection here is to the sidebar corresponding to what is filled
last, most, recommended = instanceArticle.searchLastMostRecommended()
result = instanceArticle.searchArticleWithUserByPage(0, howArticleInWeb)
# Display several articles on one page
total = math.ceil((instanceArticle.getAllCountOfArticle() / howArticleInWeb))
return render_template("index.html", result=result, total=total, page=1, last=last, most=most,
recommended=recommended)
# Jump to the first page Just change the parameter
@index.route("/page/<int:page>")
@logDanger
def paginate(page):
start = (page - 1) * howArticleInWeb
article = Article()
result = article.searchArticleWithUserByPage(start, howArticleInWeb)
total = math.ceil((article.getAllCountOfArticle() / howArticleInWeb))
return render_template("index.html", result=result, total=total, page=page)
# Jump to the modules of different categories
@index.route("/type/<int:type>-<int:page>")
@logDanger
def classify(type, page):
start = (page - 1) * 10
result = instanceArticle.searchArticleByType(type, start, howArticleInWeb)
total = math.ceil(instanceArticle.searchCountByType(type) / howArticleInWeb)
return render_template("type.html", result=result, page=page, total=total, type=type)
# The search function for articles is implemented here
@index.route("/search/<int:page>-<string:keyword>")
@logDanger
def search(page, keyword):
keyword = keyword.strip()
if keyword is None or keyword == "" or "%" in keyword or len(keyword) > 10:
abort(404)
start = (page - 1) * 10
result = instanceArticle.searchHeadlineBySearchWord(keyword, start, 10)
total = math.ceil(instanceArticle.searchCountOfArticleBySearchWord(keyword) / 10)
return render_template("search.html", page=page, total=total, result=result, keyword=keyword)
# Filled sidebar recommendations
@index.route("/recommended")
@logDanger
def recommended():
last, most, recommended = instanceArticle.searchLastMostRecommended()
return jsonify(last, most, recommended)