-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
1103 lines (995 loc) · 42.4 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding:utf-8
import datetime
import multiprocessing
import os
import pymysql
import random
import requests
import time
import upyun
from datetime import timedelta
from flask import Flask, render_template, session, request, flash, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
import dw
from MiaoshuFenci import Miaoshu_Fenci
from mysql_connect import SqlCx
from sqlalchemy_packeg import *
from tuijian import FenLei, SouSuo
from usertj import User_Tj as usertj, history_read
from zzb_test import run as Run
pymysql.install_as_MySQLdb()
app = Flask(__name__)
# 数据库配置类
class Config(object):
SQLALCHEMY_DATABASE_URI = "mysql://book:[email protected]:3306/book"
# SQLALCHEMY_DATABASE_URI = "mysql://root:1234@localhost:3306/book"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = 'bvksdcjavcvavskbaleb'
# SQLALCHEMY_ECHO = True
# SQLALCHEMY_POOL_SIZE = 100000
# SQLALCHEMY_POOL_TIMEOUT = 30
# SQLALCHEMY_MAX_OVERFLOW = 100000
# SESSION过期时间设置
PERMANENT_SESSION_LIFETIME = timedelta(days=31)
# 配置信息,初始化相关对象
app.config.from_object(Config)
bootstrap = Bootstrap()
moment = Moment()
bootstrap.init_app(app)
moment.init_app(app)
db = SQLAlchemy(app)
UPLOAD_FOLDER = 'static\\book_img'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'gif', 'GIF'])
image_ext = ['png', 'jpg', 'JPG', 'PNG', 'gif', 'GIF']
def safe():
"""
安全管理函数,处于登录状态返回请求页面,非登录状态返回登录页面
:return: 登录函数
"""
if session.get('user_id') == '':
return redirect(url_for("login"))
@app.route("/login", methods=["POST", "GET"])
def login():
"""
登录
:return:
"""
if request.method == 'POST':
account = request.form.get('account')
password = request.form.get('pwd')
user = Users.query.filter(Users.account == account, Users.password == password).first()
if user:
sql = "select is_administrator from user where account=%s" % account
if SqlCx(sql)[0]['is_administrator'] == 1:
username = Users.query.filter(Users.account == account, Users.password == password).first()
session['account'] = account
session['password'] = password
session['username'] = username.username
session['user_id'] = username.id
session['admin_user'] = account
print('管理员登录--', username.username)
return redirect(url_for("background_system"))
else:
username = Users.query.filter(Users.account == account, Users.password == password).first()
session['account'] = account
session['password'] = password
session['username'] = username.username
session['user_id'] = username.id
print('用户登录--', username.username)
# 延长保存时间
# session.permanent = True
# 账号密码正确,重定向到首页
# import smtplib
# from email.mime.text import MIMEText
# subject = "用户登录"
# sender = "[email protected]"
# content = "sir,用户昵称为%s的用户登录了!"%username.username
# recver = "[email protected]"
# password = "siyi4141"
# message = MIMEText(content, "plain", "utf-8")
# # content 发送内容 "plain"文本格式 utf-8 编码格式
# message['Subject'] = subject
# message['To'] = recver
# message['From'] = sender
# smtp = smtplib.SMTP_SSL("smtp.163.com", 465)
# smtp.login(sender, password)
# smtp.sendmail(sender, [recver], message.as_string())
# smtp.close()
response = redirect(url_for("index"))
response.set_cookie('username', username.username, max_age=7 * 24 * 3600)
return response
else:
flash("账号或密码不正确!")
else:
return render_template("login.html")
return render_template("login.html")
@app.route("/background/management/system", methods=["POST", "GET"])
def background_system():
admin_user = session.get('admin_user')
print(admin_user)
if admin_user:
return render_template("background_system.html")
else:
return redirect(url_for('login'))
@app.route("/class_system", methods=["POST", "GET"])
@app.route("/class_system/<int:page>", methods=["POST", "GET"])
def class_system(page=1):
if session.get('admin_user'):
sql = "select book_class,COUNT(*) AS c from book group by book_class"
book_class = SqlCx(sql)
page_num = len(book_class) / 5
book_class = book_class[(page - 1) * 5:(page * 5)]
return render_template("class_system.html", book_class=book_class, page=page, page_num=int(page_num))
else:
return redirect(url_for('login'))
@app.route('/edit_class', methods=['POST'])
def admin_edit_class():
if session.get('admin_user'):
if request.method == 'POST':
old_book_class = request.form.get('old_book_class')
new_book_class = request.form.get('new_book_class')
sql = f"update book set book_class='{new_book_class}' where book_class='{old_book_class}'"
SqlCx(sql)
return redirect(url_for('class_system'))
else:
return redirect(url_for('login'))
@app.route("/book_system", methods=["POST", "GET"])
@app.route("/book_system/<int:page>", methods=["POST", "GET"])
def book_system(page=1):
if session.get('admin_user'):
sql = "select * from book"
book_class = SqlCx(sql)
page_num = len(book_class) / 5
book_class = book_class[(page - 1) * 5:(page * 5)]
return render_template("book_system.html", book_class=book_class, page=page, page_num=int(page_num))
else:
return redirect(url_for('login'))
@app.route("/background_author_system", methods=["POST", "GET"])
def background_author_system():
if session.get('admin_user'):
sql = "select * from user where user.isauthor=1"
author = SqlCx(sql)
return render_template("author_system.html", author=author)
else:
return redirect(url_for('login'))
@app.route("/member_system", methods=["POST", "GET"])
def member_system():
if session.get('admin_user'):
sql = "SELECT *,user.username,COUNT(*) AS c from user,history where user.is_member=1 AND user.id=history.user_id GROUP BY user.username"
member = SqlCx(sql)
return render_template("member_system.html", member=member)
else:
return redirect(url_for('login'))
@app.route("/publicity_system", methods=["POST", "GET"])
def publicity_system():
if session.get('admin_user'):
sql = "SELECT * from publicity"
publicity = SqlCx(sql)
return render_template("publicity_system.html", publicity=publicity)
else:
return redirect(url_for('login'))
@app.route("/send_publicity", methods=["POST"])
def send_publicity():
if session.get('admin_user'):
if request.method == 'POST':
title = request.form.get('title')
name = request.form.get('administrator_name')
content = request.form.get('content')
da = datetime.datetime.now()
sql = f"insert into publicity value (null ,'{title}', '{name}', '{da}', '{content}')"
SqlCx(sql)
return redirect(url_for('publicity_system'))
else:
return redirect(url_for('login'))
@app.route("/delete_publicity", methods=["POST"])
def delete_publicity():
if session.get('admin_user'):
if request.method == 'POST':
publicity_id = request.form.get('publicity_id')
sql = f"delete from publicity where id='{publicity_id}'"
SqlCx(sql)
return redirect(url_for('publicity_system'))
else:
return redirect(url_for('login'))
@app.route("/data_system", methods=["GET"])
def data_system():
if session.get('admin_user'):
return render_template('data_system.html')
else:
return redirect(url_for('login'))
@app.route('/data/<int:name>', methods=['GET'])
def admin_system_data_profile(name):
# dw.info_count()
# dw.user_count()
dw.class_count()
return render_template(f'{name}.html')
@app.route("/xiugaixinxi", methods=['POST', 'GET'])
def xiugaixinxi():
"""
修改个人信息
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
user_id = session.get('user_id')
if request.method == 'POST':
username = request.form.get('username')
account = request.form.get('account')
password = request.form.get('pwd1')
password2 = request.form.get('pwd2')
like = request.form.get('selest')
# Users.query.filter(Users.account == account, Users.password == password).first()
user = Users.query.filter(Users.account == account).first()
# print(user.password)
if password != user.password:
flash("与原密码不一致")
elif len(password2) > 8:
flash("密码太长")
elif len(str(username)) > 10:
flash("昵称过长")
elif str(account).isdigit():
Users.query.filter(Users.account == account).update({'username': '%s' % username,
'password': '%s' % password2,
'perference': '%s' % like})
db.session.commit()
db.session.close()
session.clear()
session['account'] = account
session['password'] = password2
session['username'] = username
session['user_id'] = user_id
return redirect(url_for("login"))
else:
flash("账号由纯数字组成")
return render_template("xiugaixinxi.html", publicity=publicity)
@app.route("/mycollection")
def mycollection():
"""
我的收藏
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
user_id = session.get('user_id')
sql = "SELECT * FROM book,collection WHERE book.id=collection.book_id AND collection.user_id='%s' order by collection.id desc" % user_id
mycollection = SqlCx(sql)
return render_template("mycollection.html", collection=mycollection, publicity=publicity)
@app.route("/myhistory")
def myhistory():
"""
历史记录
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
user_id = session.get('user_id')
sql = "SELECT * FROM yuedu_history,book where book.id=yuedu_history.book_id and user_id='%s' ORDER BY url DESC" % user_id
myhistory = SqlCx(sql)
return render_template("myhistory.html", history=myhistory, publicity=publicity)
@app.route("/shoucang/<int:book_id>")
def collection(book_id):
"""
收藏操作
:param book_id: 书本id
:return:
"""
username = session.get('username')
user_id = session.get('user_id')
print('收藏--', username, '-----', book_id)
try:
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
collection = Collection.query.filter(Collection.user_id == user_id, Collection.book_id == book_id).first()
if collection:
flash("您已收藏过本书")
else:
new_collection = Collection(user_id=int(user_id), book_id=int(book_id), id=None, data=da)
db.session.add(new_collection)
db.session.commit()
sql = "select id from book_count where book_id=%s" % book_id
d = SqlCx(sql)
if d:
print("data存在")
sql = "update book_count set book_collection=book_collection+1 where book_id=%s" % book_id
SqlCx(sql)
else:
print("data不存在")
sql = "insert into book_count values(null,'" + book_id + "',0,1,0,0)"
SqlCx(sql)
return redirect(url_for("xiangqing", book_id=book_id))
except:
return render_template("loginerror.html")
return redirect(url_for("xiangqing", book_id=book_id))
@app.route("/like/<int:book_id>")
def like(book_id):
"""
点赞操作
:param book_id:
:return:
"""
username = session.get('username')
user_id = session.get('user_id')
print('点赞--', username, '-----', book_id)
try:
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
like = Like.query.filter(Like.user_id == user_id, Like.book_id == book_id).first()
if like:
flash("您已点赞过本书")
else:
new_like = Like(user_id=int(user_id), book_id=int(book_id), id=None, data=da)
db.session.add(new_like)
db.session.commit()
sql = "select id from book_count where book_id=%s" % book_id
d = SqlCx(sql)
if d:
print("data存在")
sql = "update book_count set book_like=book_like+1 where book_id=%s" % book_id
SqlCx(sql)
else:
print("data不存在")
sql = "insert into book_count values(null,'" + book_id + "',0,0,1,0)"
SqlCx(sql)
return redirect(url_for("xiangqing", book_id=book_id))
except:
return render_template("loginerror.html")
return redirect(url_for("xiangqing", book_id=book_id))
@app.route("/userinfo")
def userinfo():
"""
用户主页,包含用户最近浏览推荐操作函数
:return:
"""
# 最近浏览推荐
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
user_id = session.get('user_id')
data = usertj(user_id)
print(data)
print(len(data))
# sql = "select * from book where id='%s' or id='%s' or id='%s' or id='%s' or id='%s'" \
# % (data[4], data[3], data[2], data[1], data[0])
# data = SqlCx(sql)
data_2 = history_read(user_id)
sql = "select * from book where id='%s' or id='%s' or id='%s' or id='%s' or id='%s'" \
% (data_2[4], data_2[3], data_2[2], data_2[1], data_2[0])
data_2 = SqlCx(sql)
sql = "select count(*) as c from history where user_id=%s" % user_id
mypoint = SqlCx(sql)
sql = "select * from user where id=%s" % user_id
info = SqlCx(sql)
return render_template("userinfo.html", zuijin=data, lishi=data_2, mypoint=mypoint, info=info, publicity=publicity)
@app.route("/author_system")
def author_system():
"""
作者系统
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
# 检测是否是在登录状态,如果未登录,则返回登录,如果已登录,直接进入后台
user_id = session.get('user_id')
data = usertj(user_id)
sql = "select count(*) as c from history where user_id=%s" % user_id
mypoint = SqlCx(sql)
sql = "select * from user where id=%s" % user_id
info = SqlCx(sql)
# code = [i for i in random.randint(0,9)]
code = 2525
return render_template("author.html", mypoint=mypoint, info=info, code=code, publicity=publicity)
def jieba_save(book_class):
p2 = multiprocessing.Process(target=Miaoshu_Fenci, args=(book_class,))
p2.start()
p2.join()
def tf_cos(book_id, book_class):
p1 = multiprocessing.Process(target=Run, args=(book_id, 5, book_class))
p1.start()
def allowed_file(filename):
"""
上传文件验证操作
:param filename: 图片名称
:return:
"""
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route("/create_book", methods=["POST", "GET"])
def create_book():
"""
新建图书
:return:
"""
if request.method == "GET":
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
sql = "select book_class from book group by book_class"
data = SqlCx(sql)
return render_template("create_book.html", data=data, publicity=publicity)
elif request.method == "POST":
# 获取前端数据
file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])
book_name = request.form.get("book_name")
book_author = request.form.get("book_author")
book_abstract = request.form.get("book_abstract")
book_class = request.form.get("book_class")
book_img_link = request.form.get("book_img_link")
book_img = request.files.get('book_img')
if "image" in str(book_img):
if book_img and allowed_file(book_img.filename) and book_img_link == '':
now_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S") # 生成当前时间
file_ext = str(book_img.filename).split('.', 1)[-1]
ranint = ''.join(str(random.choice(range(10))) for _ in range(2))
ranint_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
new_book_id = ranint_time + ranint
book_img_path = "/static/book_img/" + str(new_book_id) + '.' + file_ext
new_book_img_name = str(new_book_id) + '.' + file_ext
book_img.save(os.path.join(file_dir, new_book_img_name))
sql = "insert into book values('" + new_book_id + "','" + str(book_name) + "','" + str(
book_author) + "','" + \
str(book_abstract) + "','" + str(book_class) + "','" + str(book_img_path) + "',null,null)"
SqlCx(sql)
user_id = session.get('user_id')
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
sql = "insert into book_author values(null,'" + str(new_book_id) + "','" + str(user_id) + "','" + str(
da) + "')"
SqlCx(sql)
sql = "insert into book_count values(null,'" + str(new_book_id) + "',0,0,0,0)"
SqlCx(sql)
jieba_save(book_class)
tf_cos(new_book_id, book_class)
return redirect(url_for("my_book"))
if book_img_link:
flash("上传图片和输入图片链接只可选择一种操作")
else:
flash("图片格式有误,只支持后缀为png,jpg,gif,JPG,PNG,GIF")
else:
book_img_link_ext = str(book_img_link).split('.', 3)[-1]
if book_img_link_ext in image_ext:
req = requests.get(book_img_link)
if req.status_code == 200:
ranint = ''.join(str(random.choice(range(10))) for _ in range(2))
ranint_time = datetime.datetime.now().strftime("%d%H%M%S")
new_book_id = ranint_time + ranint
sql = "insert into book values(" + new_book_id + ",'" + str(book_name) + "','" + str(
book_author) + "','" + \
str(book_abstract) + "','" + str(book_class) + "','" + str(book_img_link) + "',null,null)"
SqlCx(sql)
user_id = session.get('user_id')
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
sql = "insert into book_author values(null,'" + str(new_book_id) + "','" + str(
user_id) + "','" + str(da) + "')"
SqlCx(sql)
sql = "insert into book_count values(null,'" + str(new_book_id) + "',0,0,0,0)"
SqlCx(sql)
jieba_save(book_class)
tf_cos(new_book_id, book_class)
return redirect(url_for("my_book"))
else:
flash("图片无法访问,请检查图片链接")
else:
flash("图片格式有误,只支持后缀为png,jpg,gif,JPG,PNG,GIF")
return render_template("create_book.html")
return render_template("create_book.html")
@app.route("/delete_my_book", methods=["POST", "GET"])
def delete_my_book():
"""
删除图书
:return:
"""
book_id = request.form.get("book_id")
# 获取前端传来的book_id
safe_code = safe()
if safe_code == 1:
user_id = session.get("user_id")
sql = "delete from book where id=%s" % book_id
SqlCx(sql)
sql = "delete from book_author where book_id=%s" % book_id
SqlCx(sql)
sql = "delete from book_count where id=%s" % book_id
SqlCx(sql)
sql = "delete from collection where book_id=%s" % book_id
SqlCx(sql)
sql = "delete from comment where book_id=%s" % book_id
SqlCx(sql)
sql = "delete from history where book_id=%s" % book_id
SqlCx(sql)
# sql = "delete from like where book_id=%s" % book_id
# SqlCx(sql)
sql = "delete from tf where b_id=%s" % book_id
SqlCx(sql)
sql = "delete from yuedu_history where book_id=%s" % book_id
SqlCx(sql)
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
sql = "insert into author_delete_book values(null,'" + str(user_id) + "','" + str(book_id) + "','" + da + "')"
SqlCx(sql)
else:
return redirect(url_for("login"))
return redirect(url_for("my_book"))
@app.route("/my_book")
def my_book():
"""
我的作品
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
user_id = session.get("user_id")
if user_id:
# "SELECT * FROM book WHERE book_author='司毅'"
sql = "select * from book,book_author,book_count WHERE book.id=book_author.book_id" \
" AND book.id=book_count.book_id and book_author.user_id=%s order by book_author.date desc" % user_id
data = SqlCx(sql)
return render_template("my_book.html", data=data, publicity=publicity)
else:
return redirect(url_for("login"))
return render_template("my_book.html")
@app.route("/edit_my_book", methods=["POST"])
def edit_my_book():
"""
编辑文章
:return:
"""
book_id = request.form.get("book_id")
book_name = request.form.get("book_name")
book_title = request.form.get("book_title")
book_content = request.form.get("book_content")
book_title_content = book_title + "biaotineirong" + book_content + " "
with open("static/book/{}.txt".format(book_name), 'w', encoding='utf-8') as f:
f.write(book_title_content)
f.close()
return redirect(url_for("yuedu", book_id=book_id))
@app.route("/certification", methods=["POST", "GET"])
def certification():
"""
作者身份认证1
:return:
"""
if request.method == "GET":
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
user_id = session.get('user_id')
data = usertj(user_id)
sql = "select count(*) as c from history where user_id=%s" % user_id
mypoint = SqlCx(sql)
sql = "select * from user where id=%s" % user_id
info = SqlCx(sql)
# code = [i for i in random.randint(0,9)]
code = 4589
return render_template("certification.html", mypoint=mypoint, info=info, code=code, publicity=publicity)
@app.route("/certificate", methods=["POST"])
@app.route("/certificate/<cer_code>", methods=["GET"])
def certificate(cer_code=0):
"""
作者身份认证2
:param cer_code:
:return:
"""
if request.method == "POST":
account = request.form.get('account')
realname = request.form.get('realname')
tel = request.form.get('tel')
email = request.form.get('email')
code = request.form.get('code')
code_2 = request.form.get('code_2')
print(code)
print(code_2)
if code != code_2:
flash("验证码输入错误")
elif len(tel) != 11:
flash("手机号码格式错误")
else:
# md5加密
import hashlib
m = hashlib.md5()
m.update(realname.encode(encoding='utf-8'))
md5_code = m.hexdigest()
hash_name = hash(realname)
print(hash_name)
# 信息存入数据库
sql = "update user set realname='%s', tel='%s', email='%s', code='%s' where account=%s" % (str(realname),
str(tel),
str(email),
str(md5_code),
account)
SqlCx(sql)
# 发送认证邮件
import smtplib
from email.mime.text import MIMEText
subject = "作者验证"
sender = "[email protected]"
password = "siyi4141"
content = realname + "我们已收到您的认证申请,请您点击下面的链接完成认证!" \
"http://localhost:5000/certificate/%s" % md5_code
message = MIMEText(content, "plain", "utf-8")
message['Subject'] = subject
message['To'] = email
message['From'] = sender
smtp = smtplib.SMTP_SSL("smtp.163.com", 465)
smtp.login(sender, password)
smtp.sendmail(sender, [email], message.as_string())
smtp.close()
user_id = session.get('user_id')
data = usertj(user_id)
sql = "select count(*) as c from history where user_id=%s" % user_id
mypoint = SqlCx(sql)
sql = "select * from user where id=%s" % user_id
info = SqlCx(sql)
# code = [i for i in random.randint(0,9)]
code = 1234
return "验证邮件已发送,请您注意查收!"
elif request.method == "GET":
return "认证申请已发送至后台管理员,待审核后就成为天使之梦小说网的认证作者啦,审核时间为1~3个工作日,请耐心等待"
user_id = session.get('user_id')
data = usertj(user_id)
sql = "select count(*) as c from history where user_id=%s" % user_id
mypoint = SqlCx(sql)
sql = "select * from user where id=%s" % user_id
info = SqlCx(sql)
# code = [i for i in random.randint(0,9)]
code = 4598
return render_template("certification.html", mypoint=mypoint, info=info, code=code)
@app.route("/yuedu/<book_id>/<int:page>")
@app.route("/yuedu/<book_id>")
def yuedu(book_id, page=0):
"""
阅读页
:param book_id:
:param page:
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
sql = "select book_name from book where id = %s" % book_id
data = SqlCx(sql)
book_name = str([i['book_name'] for i in data]).replace('[', '').replace(']', '').replace("'", '')
username = session.get('username')
user_id = session.get('user_id')
print('正在阅读---', username, '-----', book_name, '----', page)
url = '/yuedu/' + str(book_name) + '/' + str(page)
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
try:
sql = "delete from yuedu_history where book_name='%s' and user_id='%s'" % (book_name, user_id)
SqlCx(sql)
except Exception as e:
print(e)
try:
# new_history = Yueduhistory(user_id=int(user_id), url=str(url), id=None, data=da, book_id=book_id, book_name=str(book_name))
# db.session.add(new_history)
# db.session.commit()
sql = "insert into yuedu_history values(null,%s,'%s',%s,'%s','%s')" % (book_id, book_name, user_id, url, da)
SqlCx(sql)
except Exception as e:
print('未登录用户正在阅读---', book_name, '---', page, e)
try:
up = upyun.UpYun('angel-xiaoshuo', 'angel0041', 'ElilaZy2B8IbvGD7v4Jq4Rv4gAag8uj1', timeout=30,
endpoint=upyun.ED_AUTO)
x = up.get('/txt/%s.txt' % book_name)
zj = x.split(' ')
zjj = zj[page]
if len(zjj) > 60:
return render_template("yuedu.html", x=zj[page].split('biaotineirong'), book_id=book_id, page=page,
page_num=len(zj), publicity=publicity)
else:
return render_template("yuedu.html", x=zj[page + 1].split('biaotineirong'), book_id=book_id,
page=page + 1, page_num=len(zj), publicity=publicity)
except Exception as e:
print(e)
try:
x = open('static/book/%s.txt' % book_name, 'r', encoding='utf-8')
x = x.read()
# print(x)
zj = x.split(' ')
zjj = zj[page]
if len(zjj) > 60:
return render_template("yuedu.html", x=zj[page].split('biaotineirong'), book_id=book_id, page=page,
page_num=len(zj))
else:
return render_template("yuedu.html", x=zj[page + 1].split('biaotineirong'), book_id=book_id,
page=page + 1,
page_num=len(zj), publicity=publicity)
except:
book = Book.query.filter(Book.book_name == book_name).first()
# print(book.id)
return render_template("yueduerror.html", x=book.book_link, book_name=book_name, publicity=publicity)
@app.route('/')
@app.route('/index')
def index():
"""
首页
:return:
"""
# username = request.cookies.get('username', '')
# print('用户进入---', session.get('username'))
# xiaoshuo = Book.query.filter_by(book_class="励志")
# zuire = Book.query.filter_by(book_class="中国文学")
# manhua = Book.query.filter_by(book_class="互联网")
# qingchunwenxue = Book.query.filter_by(book_class="历史小说")
# aiqing = Book.query.filter_by(book_class="网游小说")
qingchunwenxue = "select * from book where book.book_class='政治与法律'"
manhua = "select * from book where book.book_class='互联网'"
zuire = "select * from book where book.book_class='中国文学'"
xiaoshuo = "select * from book where book.book_class='励志'"
aiqing = "select * from book where book.book_class='社会科学总论'"
qingchunwenxue = SqlCx(qingchunwenxue)
manhua = SqlCx(manhua)
zuire = SqlCx(zuire)
xiaoshuo = SqlCx(xiaoshuo)
aiqing = SqlCx(aiqing)
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
sql = "SELECT COUNT(history.id) AS count, book_id, book_name FROM history,book WHERE book.id=history.book_id GROUP BY book_id ORDER BY COUNT(history.id) DESC LIMIT 10"
book_paihang = SqlCx(sql)
sql = "SELECT COUNT(history.id) AS count, user_id, username FROM history,user WHERE user.id=history.user_id GROUP BY user_id ORDER BY COUNT(history.id) DESC LIMIT 10"
user_paihang = SqlCx(sql)
return render_template("index.html", xiaoshuo=xiaoshuo[0:10], zuire=zuire[0:10], manhua=manhua[0:10],
qingchunwenxue=qingchunwenxue[0:10],
aiqing=aiqing[0:10], book_paihang=book_paihang, user_paihang=user_paihang,
publicity=publicity)
@app.route("/xiangqing/<book_id>")
@app.route("/xiangqing")
def xiangqing(book_id):
"""
详情页
:param book_id:
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
book = Book.query.filter(Book.id == book_id).first()
sql = "select * from tf where b_id = '%s'" % str(book_id)
data = SqlCx(sql)
if len(data) == 0:
sql = "select book_class from book where id=%s" % book_id
book_class = SqlCx(sql)
book_class = book_class[0]['book_class']
data = FenLei(book_class)
else:
for i in data:
data = str(i['tf']).replace(' ', '').split(',')
sql = "select * from book where id='%s' or id='%s' or id='%s' or id='%s' or id='%s'" \
% (data[4], data[3], data[2], data[1], data[0])
data = SqlCx(sql)
print('用户点击---', session.get('username'), '-----', book.book_name)
user_id = session.get('user_id')
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
try:
sql = "insert into history values(null,%s,%s,'%s')" % (user_id, book_id, da)
SqlCx(sql)
sql = "select id from book_count where book_id=%s" % book_id
d = SqlCx(sql)
if d:
print("data存在")
sql = "update book_count set book_history=book_history+1 where book_id=%s" % book_id
x = SqlCx(sql)
else:
print("data不存在")
sql = "insert into book_count values(null,'" + book_id + "',1,0,0,0)"
SqlCx(sql)
except Exception as e:
print('未登录用户正在查看----', book.book_name, e)
collection = Collection.query.filter_by(book_id=book_id).count()
history = History.query.filter_by(book_id=book_id).count()
like = Like.query.filter_by(book_id=book_id).count()
comment = Comment.query.filter_by(book_id=book_id).count()
sql = "SELECT comment,data,username FROM comment,user WHERE comment.book_id='%s' AND comment.user_id=user.id" % book_id
comment_comment = SqlCx(sql)
# sql = "SELECT * FROM user, comment WHERE comment.book_id='%s'"%book_id
# comment_selsec = SqlCx(sql)
sql = "SELECT COUNT(history.id) AS count, book_id, book_name FROM history,book WHERE book.id=history.book_id GROUP BY book_id ORDER BY COUNT(history.id) DESC LIMIT 10"
book_paihang = SqlCx(sql)
sql = "SELECT COUNT(history.id) AS count, user_id, username FROM history,user WHERE user.id=history.user_id GROUP BY user_id ORDER BY COUNT(history.id) DESC LIMIT 10"
user_paihang = SqlCx(sql)
return render_template("xiangqing.html", book=book, history=history, collection=collection, like=like,
comment=comment,
book_paihang=book_paihang, user_paihang=user_paihang, comment_comment=comment_comment,
data=data,
publicity=publicity)
@app.route("/comment/<book_id>")
@app.route("/comment")
def comment(book_id):
"""
评论
:param book_id:
:return:
"""
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
book = Book.query.filter(Book.id == book_id).first()
sql = "select * from tf where b_id = '%s'" % str(book_id)
data = SqlCx(sql)
# print(data)
if len(data) == 0:
sql = "select book_class from book where id=%s" % book_id
book_class = SqlCx(sql)
book_class = book_class[0]['book_class']
data = FenLei(book_class)
else:
for i in data:
data = str(i['tf']).replace(' ', '').split(',')
# print(data)
sql = "select * from book where id='%s' or id='%s' or id='%s' or id='%s' or id='%s'" \
% (data[4], data[3], data[2], data[1], data[0])
data = SqlCx(sql)
print('用户正在评论---', session.get('username'), '-----', book.book_name)
user_id = session.get('user_id')
# print(user_id)
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
try:
new_history = History(user_id=int(user_id), book_id=int(book_id), id=None, data=da)
db.session.add(new_history)
db.session.commit()
except:
return render_template("loginerror.html")
collection = Collection.query.filter_by(book_id=book_id).count()
history = History.query.filter_by(book_id=book_id).count()
like = Collection.query.filter_by(book_id=book_id).count()
comment = Comment.query.filter_by(book_id=book_id).count()
sql = "SELECT * FROM user, comment WHERE comment.book_id='%s'" % book_id
comment_selsec = SqlCx(sql)
sql = "SELECT COUNT(history.id) AS count, book_id, book_name FROM history,book WHERE book.id=history.book_id GROUP BY book_id ORDER BY COUNT(history.id) DESC LIMIT 10"
book_paihang = SqlCx(sql)
sql = "SELECT COUNT(history.id) AS count, user_id, username FROM history,user WHERE user.id=history.user_id GROUP BY user_id ORDER BY COUNT(history.id) DESC LIMIT 10"
user_paihang = SqlCx(sql)
return render_template("comment.html", book=book, history=history, collection=collection, like=like,
comment=comment, comment_comment=comment_selsec, publicity=publicity,
book_paihang=book_paihang, user_paihang=user_paihang, data=data)
@app.route("/comment/tj", methods=["POST"])
@app.route("/comment/tj/<int:book_id>", methods=["POST"])
def tijiao(book_id):
"""
评论提交
:param book_id:
:return:
"""
comment = request.form.get('comment')
book_id = book_id
user_id = session.get('user_id')
# print(user_id)
book = Book.query.filter(Book.id == book_id).first()
print('用户正在评论---', session.get('username'), '-----', book.book_name, '----', comment)
da = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
new_comment = Comment(user_id=user_id, book_id=book_id, comment=comment, id=None, data=da)
sql = "select id from book_count where book_id=%s" % book_id
d = SqlCx(sql)
if d:
print("data存在")
sql = "update book_count set book_comment=book_comment+1 where book_id=%s" % book_id
SqlCx(sql)
else:
print("data不存在")
sql = "insert into book_count values(null,'" + book_id + "',0,0,0,1)"
SqlCx(sql)
db.session.add(new_comment)
db.session.commit()
return redirect(url_for("xiangqing", book_id=book_id))
@app.route("/sousuo/<int:page>", methods=["GET"])
@app.route("/sousuo", methods=["POST"])
def sousuo(page=1):
"""
搜索
:param page:
:return:
"""
if request.method == "POST":
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
gjc = request.form.get('gjc')
session['gjc'] = gjc
print('用户搜索---', session.get('username'), '-----', gjc)
book = Book.query.filter(Book.book_name.like("%" + gjc + "%")).all()
data = SouSuo(gjc)
if int(len(book) / 15) > len(book) / 15:
page_num = int(len(book) / 15)
else:
page_num = int(len(book) / 15)
page = int(page)
if book:
return render_template("sousuo.html", fenlei=book[(page - 1) * 15:(page * 15)], page_num=page_num,
page=int(page), data=data, publicity=publicity)
else:
return render_template("sousuo.html", page=0)
else:
publicity = "select * from publicity order by publicity.date desc"
publicity = SqlCx(publicity)
gjc = session.get('gjc')
data = SouSuo(gjc)
book = Book.query.filter(Book.book_name.like("%" + gjc + "%")).all()
print('用户搜索---', session.get('username'), '-----', gjc, '--', page)
if int(len(book) / 15) > len(book) / 15:
page_num = int(len(book) / 15)
else:
page_num = int(len(book) / 15)
page = int(page)
if book:
return render_template("sousuo.html", fenlei=book[(page - 1) * 15:(page * 15)], page_num=page_num,