-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
1070 lines (971 loc) · 40.7 KB
/
database.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
import pymysql
import math
import xml.dom.minidom as mdom
import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header
db = pymysql.Connect(host="localhost", user="root", passwd="******", db="minibbs", port=3306, charset='utf8')
cursor = db.cursor()
if __name__ == '__main__':
def make_user_data():
basicExpr = 'insert into User (UserName, Gender, Birthday, Password, Email) '
for i in range(50):
insertExpr = "values ('User"+str(i)+"','Male','1998-01-01','123','user"+str(i)+"@pku.edu.cn');"
cursor.execute(basicExpr+insertExpr)
for i in range(50, 100):
insertExpr = "values ('User"+str(i)+"','Female','1998-12-12','321','user"+str(i)+"@pku.edu.cn');"
cursor.execute(basicExpr+insertExpr)
try:
db.commit()
except:
db.rollback()
row = cursor.fetchall()
def make_post_data():
basicExpr = 'insert into Post (UserNo, SectionNo, Title, Content)'
'''for i in range(50):
insertExpr = "values ("+str(i+306)+",1,'Hello world!','I am new here. Lets start chatting!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i+306)+",2,'Good lecture!','I want to attend this lecture~');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i+306)+",3,'Looking for jobs.','I am looking for jobs.');"
cursor.execute(basicExpr+insertExpr)
for i in range(50,100):
insertExpr = "values ("+str(i+306)+",1,'Hello world!','I am new here. Lets start chatting!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i+306)+",4,'Studying','Come on and study together!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i+306)+",5,'Good game','Peking University Sports Events are so great!');"
cursor.execute(basicExpr+insertExpr)'''
for i in range(10):
insertExpr = "values ("+str(i+306)+",1,'Hello world!!!','I am really new here. Lets start chatting!');"
cursor.execute(basicExpr+insertExpr)
try:
db.commit()
except:
db.rollback()
row = cursor.fetchall()
def make_reply_data():
basicExpr = 'insert into Reply (PostNo, UserNo, ReplyContent)'
'''for i in range(1, 301):
insertExpr = "values ("+str(i)+",1,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",2,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",3,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",306,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",307,'Good Post!');"
cursor.execute(basicExpr+insertExpr)'''
for i in range(1, 50):
insertExpr = "values ("+str(i)+",400,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",401,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",402,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",403,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
insertExpr = "values ("+str(i)+",404,'Good Post!');"
cursor.execute(basicExpr+insertExpr)
try:
db.commit()
except:
db.rollback()
row = cursor.fetchall()
db.close()
def calculate_level(EXP):
EXP = int(EXP)
return '0' if EXP == 0 else str(int(math.log(EXP,2)))
def calculate_age(birthday):
birthday = str(birthday)
year, month, day = int(birthday[0:4]), int(birthday[5:7]), int(birthday[8:10])
current_time = datetime.datetime.now()
now_year, now_month, now_day = current_time.year, current_time.month, current_time.day
age = now_year-year
if now_month < month:
age = age-1
elif now_month == month and now_day < day:
age = age-1
return '0' if age < 0 else str(age)
def valid_login(account, password):
is_admin = False
is_master = []
userID = 0
sql = "SELECT UserName, Password, Admin, UserNo FROM User WHERE UserName = '"+account+"';"
cursor.execute(sql)
row = cursor.fetchall()
if cursor.rowcount == 0:
return 'User not existed!', is_admin, is_master, userID
elif row[0][1] == password:
is_admin = True if row[0][2] == 1 else False
userID = int(row[0][3])
checkMaster = "SELECT SectionNo FROM Section WHERE Master = "+str(userID)+";"
cursor.execute(checkMaster)
row = cursor.fetchall()
if cursor.rowcount != 0:
for i in row:
is_master.append(str(i[0]))
return False, is_admin, is_master, userID
else:
return 'Password Error.', is_admin, is_master, userID
def valid_register(request):
username = request.form["username"]
password = request.form["password"]
rpassword = request.form["rpassword"]
email = request.form["email"]
gender = request.form["gender"]
birthday = request.form["birthday"]
if password != rpassword:
return '', username, 'Two passwords are not the same!'
sql = """insert into User (UserName, Gender, Birthday, Password, Email)
values ('%s','%s','%s','%s','%s');"""
data = (username, gender, birthday, password, email)
try:
expr = sql%data
cursor.execute(expr)
cursor.execute("SELECT UserNo FROM User WHERE UserName= '%s';" % username)
row = cursor.fetchall()
db.commit()
return row[0][0], username, None
except:
db.rollback()
return '', username, 'This username has been used. Please try another.'
def get_sections():
'''
return type:
[{'number': ,
'name': ,
'description': ,
'master': ,
'posts':['','',''] # with ...
}]
'''
return_section = []
sql = "SELECT SectionNo, SectionName, SectionDesc, Master FROM Section;"
cursor.execute(sql)
row = cursor.fetchall()
for Block in row:
addblock = {}
addblock['number']=Block[0]
addblock['name']=Block[1]
addblock['description']=Block[2]
addblock['posts']=[]
sql = "SELECT UserName FROM User WHERE UserNo = '%s'" % Block[3]
cursor.execute(sql)
mod = cursor.fetchall()
addblock['master']=mod[0][0]
sql = "SELECT Content FROM Post WHERE Post.SectionNo = %s ORDER BY Post.PostTime desc limit 0, 3;" %addblock['number']
cursor.execute(sql)
pos = cursor.fetchall()
for i in pos:
temp_post = i[0]
if len(temp_post) > 70:
temp_post = temp_post[0:70] + '...'
addblock['posts'].append(temp_post)
return_section.append(addblock)
return return_section
def search_in_db(searching):
''' return the post that contains searching 搜索包含searching字符串的post
return type:
[{
'post_id': ,
'section': ,
'user': ,
'time': , # YYYY-MM-DD
'title' : ,
'content': ,
'clicks' : ,
'replies' : , # number
}]
'''
return_section = []
sql = "select * from Post where Title like '%%%s%%' or Content like '%%%s%%';" %(searching, searching)
cursor.execute(sql)
row = cursor.fetchall()
for pos in row:
addblock = {}
addblock['post_id'] = pos[0]
sql = "SELECT UserName FROM User WHERE UserNo = %s;" % pos[1]
cursor.execute(sql)
UserNo = cursor.fetchall()
addblock['user'] = UserNo[0][0]
sql = "SELECT SectionName FROM Section WHERE SectionNo = %s;" % pos[2]
cursor.execute(sql)
SectionNo = cursor.fetchall()
addblock['section'] = SectionNo[0][0]
addblock['title']=pos[3]
addblock['content']=pos[4]
addblock['clicks']=pos[5]
addblock['time']=pos[6]
addblock['replies']=pos[7]
return_section.append(addblock)
return return_section
def get_top10clicks_post():
# 返回点击量前十的post,返回格式同上
return_section = []
sql = "SELECT * from Post ORDER BY Clicks DESC LIMIT 0, 10;"
cursor.execute(sql)
row = cursor.fetchall()
for pos in row:
addblock = {}
addblock['post_id'] = pos[0]
sql = "SELECT UserName FROM User WHERE UserNo = %s;" % pos[1]
cursor.execute(sql)
UserNo = cursor.fetchall()
addblock['user'] = UserNo[0][0]
sql = "SELECT SectionName FROM Section WHERE SectionNo = %s;" % pos[2]
cursor.execute(sql)
SectionNo = cursor.fetchall()
addblock['section'] = SectionNo[0][0]
addblock['title']=pos[3]
addblock['content']=pos[4]
addblock['clicks']=pos[5]
addblock['time']=pos[6]
addblock['replies']=pos[7]
return_section.append(addblock)
return return_section
def get_top10replies_post():
#返回回复量前十的post,返回格式同上
return_section = []
sql = "SELECT * from Post ORDER BY Replies DESC LIMIT 0, 10;"
cursor.execute(sql)
row = cursor.fetchall()
for pos in row:
addblock = {}
addblock['post_id'] = pos[0]
sql = "SELECT UserName FROM User WHERE UserNo = %s;" % pos[1]
cursor.execute(sql)
UserNo = cursor.fetchall()
addblock['user'] = UserNo[0][0]
sql = "SELECT SectionName FROM Section WHERE SectionNo = %s;" % pos[2]
cursor.execute(sql)
SectionNo = cursor.fetchall()
addblock['section'] = SectionNo[0][0]
addblock['title']=pos[3]
addblock['content']=pos[4]
addblock['clicks']=pos[5]
addblock['time']=pos[6]
addblock['replies']=pos[7]
return_section.append(addblock)
return return_section
def moreposts(seca, secb):
''' 返回在seca版块比secb版块帖子发的更多的用户(包含admin、master)
return type:
[{
'id': ,
'name': ,
'level': ,
'postA': ,
'postB': ,
}]
'''
sql = "SELECT * FROM Section WHERE SectionName = '%s';" % seca
cursor.execute(sql)
row = cursor.fetchall()
if cursor.rowcount == 0:
return [], 'Section %s not found.' % seca
sql = "SELECT * FROM Section WHERE SectionName = '%s';" % secb
cursor.execute(sql)
row = cursor.fetchall()
if cursor.rowcount == 0:
return [], 'Section %s not found.' % secb
return_user = []
sql = "SELECT * FROM UserSection WHERE SectionName = '%s'" % seca
cursor.execute(sql)
row1 = cursor.fetchall()
sql = "SELECT * FROM UserSection WHERE SectionName = '%s'" % secb
cursor.execute(sql)
row2 = cursor.fetchall()
cursor.execute("create temporary table SecA(UserNo INT, Posts INT);")
cursor.execute("create temporary table SecB(UserNo INT, Posts INT);")
cursor.execute("select UserNo, Posts from UserSection where SectionName = '%s';" % seca)
ans1 = cursor.fetchall()
cursor.execute("select UserNo, Posts from UserSection where SectionName = '%s';" % secb)
ans2 = cursor.fetchall()
cursor.executemany("insert into SecA values (%s, %s)", ans1)
cursor.executemany("insert into SecB values (%s, %s)", ans2)
sql = "select SecA.UserNo, SecA.Posts, SecB.Posts from (SecA inner join SecB on SecA.UserNo = SecB.UserNo) " \
"where SecA.Posts>SecB.Posts order by SecB.Posts desc, SecA.Posts desc;"
cursor.execute(sql)
row = cursor.fetchall()
for i in row:
sql = 'SELECT UserName, EXP FROM User WHERE UserNo = %s' % i[0]
cursor.execute(sql)
data = cursor.fetchall()
adduser={}
adduser['id'] = i[0]
adduser['name'] = data[0][0]
adduser['postA'] = i[1]
adduser['postB'] = i[2]
adduser['level'] = calculate_level(data[0][1])
return_user.append(adduser)
sql = "select UserNo, Posts from SecA where SecA.UserNo not in (select UserNo from SecB) order by Posts desc;"
cursor.execute(sql)
row = cursor.fetchall()
for i in row:
sql = 'SELECT UserName, EXP FROM User WHERE UserNo = %s' % i[0]
cursor.execute(sql)
data = cursor.fetchall()
adduser={}
adduser['id'] = i[0]
adduser['name'] = data[0][0]
adduser['postA'] = i[1]
adduser['postB'] = '0'
adduser['level'] = calculate_level(data[0][1])
return_user.append(adduser)
cursor.execute("drop temporary table if exists SecA;")
cursor.execute("drop temporary table if exists SecB;")
return return_user, None
def sec_information(section_id):
''' 返回版块的信息 master是版主的意思
return type:
{
'name': ,
'master': ,
'description': ,
}
'''
section = {}
sql = "SELECT SectionName, Master, SectionDesc FROM Section WHERE SectionNo = %s" % section_id
cursor.execute(sql)
row = cursor.fetchall()
section['name'] = row[0][0]
section['master'] = row[0][1]
section['description'] = row[0][2]
return section
def update_section(section_id, sec):
# 返回error,默认为None,传入的sec格式同上
error = None
if 'master' in sec.keys():
data = (sec['name'], sec['description'], sec['master'], section_id)
sql = "UPDATE Section SET SectionName = '%s', SectionDesc = '%s', Master = %s WHERE SectionNo = %s" % data
try:
cursor.execute(sql)
db.commit()
return error
except:
db.rollback()
error = "Update failed! Maybe section name has been used or master ID is invalid. "
return error
else:
data = (sec['name'], sec['description'], section_id)
sql = "update Section set SectionName = '%s', SectionDesc = '%s' where SectionNo = %s" % data
try:
cursor.execute(sql)
db.commit()
return error
except:
db.rollback()
error = "Update failed! Maybe section name has been used or master ID is invalid. "
return error
def add_sectionTo(sec):
# 返回section_id 和 error,默认为None,传入的sec格式同上
error = None
data = (sec['name'], sec['description'], sec['master'])
sql = "insert into Section (SectionName, SectionDesc, Master) values ('%s', '%s', '%s')" % data
try:
cursor.execute(sql)
cursor.execute("select SectionNo from Section where SectionName = '%s';" % sec['name'])
section_id = cursor.fetchall()
db.commit()
return section_id[0][0], error
except:
db.rollback()
error = "Add failed! Maybe section name has been used or master ID is invalid."
return -1, error
def delete_sectionFromDB(id):
sql = "delete from Section where SectionNo = %s" % id
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def get_all_users():
''' 返回所有的用户(包括admin、master)
return type:
[{
'id': ,
'name': ,
'gender': ,
'level': ,
'birthday': ,
'reg_time': ,
'email': ,
'power': ,
}]
'''
return_user = []
sql = "SELECT * FROM User;"
cursor.execute(sql)
users = cursor.fetchall()
sql = "SELECT Master FROM Section;"
cursor.execute(sql)
masters_wrap = cursor.fetchall()
masters = []
for i in masters_wrap:
masters.append(i[0])
for user in users:
adduser = {}
adduser['id'] = str(user[0])
adduser['name'] = user[1]
adduser['gender'] = user[2]
adduser['level'] = calculate_level(user[6])
adduser['birthday'] = str(user[3])
adduser['reg_time'] = str(user[8])[0:10]
adduser['email'] = user[5]
if user[7] == 1:
adduser['power'] = 'admin'
elif user[0] in masters:
adduser['power'] = 'master'
else:
adduser['power'] = 'user'
return_user.append(adduser)
return return_user
def delete_user(id):
# 用于删除用户(此用户不含admin、master),返回error默认为None
try:
sql = "DELETE FROM User WHERE UserNo = %s" % id
cursor.execute(sql)
db.commit()
return None
except:
db.rollback()
return "Delete failed! "
def update_user(request, userID):
password = request.form["password"]
rpassword = request.form["rpassword"]
email = request.form["email"]
gender = request.form["gender"]
birthday = request.form["birthday"]
if password != rpassword:
return 'Two passwords are not the same!'
sql = """update User set Gender = '%s', Birthday = '%s', Password = '%s', Email = '%s' where UserNo = %s;"""
data = (gender, birthday, password, email, userID)
try:
expr = sql%data
cursor.execute(expr)
db.commit()
return None
except:
db.rollback()
return 'Update failed!'
def get_user_info(id):
cursor.execute("SELECT * FROM User WHERE UserNo = %s" % id)
user_db = cursor.fetchall()
user = {}
user['gender'] = user_db[0][2]
user['birthday'] = user_db[0][3]
user['username'] = user_db[0][1]
user['email'] = user_db[0][5]
return user
def make_user_xml(id):
sql = "SELECT * FROM User WHERE UserNo = %s" % id
cursor.execute(sql)
user = cursor.fetchall()
sql = "SELECT * FROM Post WHERE UserNo = %s ORDER BY PostTime Desc" % id
cursor.execute(sql)
posts = cursor.fetchall()
sql = "SELECT * FROM Reply WHERE UserNo = %s ORDER BY ReplyTime Desc" % id
cursor.execute(sql)
replies = cursor.fetchall()
doc = mdom.Document()
doctype = mdom.DocumentType('User')
doctype.systemId = "userprofile.dtd"
doctype.publicId = None
doc.appendChild(doctype)
root = doc.createElement('User')
doc.appendChild(root)
node_username = doc.createElement('UserName')
node_username.appendChild(doc.createTextNode(str(user[0][1])))
root.appendChild(node_username)
node_info = doc.createElement('Info')
node_basicinfo = doc.createElement('BasicInfo')
node_gender = doc.createElement('Gender')
node_gender.appendChild(doc.createTextNode(str(user[0][2])))
node_age = doc.createElement('Age')
node_age.appendChild(doc.createTextNode(calculate_age(user[0][3])))
node_level = doc.createElement('Level')
node_level.appendChild(doc.createTextNode(calculate_level(user[0][6])))
node_birth = doc.createElement('Birthday')
node_birth.appendChild(doc.createTextNode(str(user[0][3])))
node_email = doc.createElement('Email')
node_email.appendChild(doc.createTextNode(str(user[0][5])))
node_basicinfo.appendChild(node_gender)
node_basicinfo.appendChild(node_age)
node_basicinfo.appendChild(node_level)
node_basicinfo.appendChild(node_birth)
node_basicinfo.appendChild(node_email)
node_info.appendChild(node_basicinfo)
node_otherinfo = doc.createElement('OtherInfo')
node_posts = doc.createElement('Posts')
node_replies = doc.createElement('Replies')
for x in posts:
node_post = doc.createElement('Post')
node_No = doc.createElement('No')
node_No.appendChild(doc.createTextNode(str(x[0])))
node_block = doc.createElement('Block')
sql = "select SectionName from Section where SectionNo = %s" % x[2]
cursor.execute(sql)
sec = cursor.fetchall()
node_block.appendChild(doc.createTextNode(str(sec[0][0])))
node_postuser = doc.createElement('PostUser')
node_postuser.appendChild(doc.createTextNode(str(user[0][1])))
node_title = doc.createElement('Title')
node_title.appendChild(doc.createTextNode(str(x[3])))
node_content = doc.createElement('Content')
node_content.appendChild(doc.createTextNode(str(x[4])))
node_posttime = doc.createElement('PostTime')
node_posttime.appendChild(doc.createTextNode(str(x[6])))
node_clicks = doc.createElement('Clicks')
node_clicks.appendChild(doc.createTextNode(str(x[5])))
node_replynum = doc.createElement('ReplyNum')
node_replynum.appendChild(doc.createTextNode(str(x[7])))
node_post.appendChild(node_No)
node_post.appendChild(node_block)
node_post.appendChild(node_postuser)
node_post.appendChild(node_title)
node_post.appendChild(node_content)
node_post.appendChild(node_posttime)
node_post.appendChild(node_clicks)
node_post.appendChild(node_replynum)
node_posts.appendChild(node_post)
for x in replies:
node_reply = doc.createElement('Reply')
sql = "select * from Post where PostNo = %s" % x[2]
cursor.execute(sql)
temp_post = cursor.fetchall()
node_title = doc.createElement('Title')
node_title.appendChild(doc.createTextNode(str(temp_post[0][3])))
sql = "select UserName from User where UserNo = %s" % temp_post[0][1]
cursor.execute(sql)
temp_user = cursor.fetchall()
node_postuser = doc.createElement('PostUser')
node_postuser.appendChild(doc.createTextNode(str(temp_user[0][0])))
node_postid = doc.createElement('PostID')
node_postid.appendChild(doc.createTextNode(str(x[2])))
node_original = doc.createElement('OriginalNo')
node_original.appendChild(doc.createTextNode(str(x[0])))
node_floor = doc.createElement('Floor')
node_floor.appendChild(doc.createTextNode(str(x[1])))
node_replyuser = doc.createElement('ReplyUser')
node_replyuser.appendChild(doc.createTextNode(str(user[0][1])))
node_replycontent = doc.createElement('ReplyContent')
node_replycontent.appendChild(doc.createTextNode(str(x[4])))
node_replytime = doc.createElement('ReplyTime')
node_replytime.appendChild(doc.createTextNode(str(x[5])))
node_praisenum = doc.createElement('PraiseNum')
node_praisenum.appendChild(doc.createTextNode(str(x[6])))
node_reply.appendChild(node_title)
node_reply.appendChild(node_postuser)
node_reply.appendChild(node_postid)
node_reply.appendChild(node_original)
node_reply.appendChild(node_floor)
node_reply.appendChild(node_replyuser)
node_reply.appendChild(node_replycontent)
node_reply.appendChild(node_replytime)
node_reply.appendChild(node_praisenum)
node_replies.appendChild(node_reply)
node_otherinfo.appendChild(node_posts)
node_otherinfo.appendChild(node_replies)
node_info.appendChild(node_otherinfo)
root.appendChild(node_info)
filepath = './static/xml/user_%s.xml' % user[0][1]
xmlfile = open(filepath, 'w', encoding='utf-8')
doc.writexml(xmlfile, indent='\t', addindent='\t', newl='\n', encoding='UTF-8')
xmlfile.close()
def make_section_xml(id):
sql = "SELECT * FROM Section WHERE SectionNo = %s" % id
cursor.execute(sql)
section = cursor.fetchall()
sql = "SELECT * FROM Post WHERE SectionNo = %s ORDER BY PostTime Desc" % id
cursor.execute(sql)
posts = cursor.fetchall()
postNum = cursor.rowcount
doc = mdom.Document()
doctype = mdom.DocumentType('Section')
doctype.systemId = "secprofile.dtd"
doctype.publicId = None
doc.appendChild(doctype)
root = doc.createElement('Section')
doc.appendChild(root)
node_info = doc.createElement('Info')
node_secid = doc.createElement('SecId')
node_secid.appendChild(doc.createTextNode(str(id)))
node_secname = doc.createElement('SecName')
node_secname.appendChild(doc.createTextNode(str(section[0][1])))
node_master = doc.createElement('Master')
sql = "SELECT UserName FROM User WHERE UserNo = %s" % section[0][3]
cursor.execute(sql)
master_name = cursor.fetchall()
node_master.appendChild(doc.createTextNode(str(master_name[0][0])))
node_postnum = doc.createElement('PostNum')
node_postnum.appendChild(doc.createTextNode(str(postNum)))
node_desc = doc.createElement('Description')
node_desc.appendChild(doc.createTextNode(str(section[0][2])))
node_info.appendChild(node_secid)
node_info.appendChild(node_secname)
node_info.appendChild(node_master)
node_info.appendChild(node_postnum)
node_info.appendChild(node_desc)
root.appendChild(node_info)
node_posts = doc.createElement('Posts')
for x in posts:
node_post = doc.createElement('Post')
node_No = doc.createElement('No')
node_No.appendChild(doc.createTextNode(str(x[0])))
node_postuser = doc.createElement('PostUser')
sql = "SELECT UserName FROM User WHERE UserNo = %s" % x[1]
cursor.execute(sql)
username = cursor.fetchall()
node_postuser.appendChild(doc.createTextNode(str(username[0][0])))
node_title = doc.createElement('Title')
node_title.appendChild(doc.createTextNode(str(x[3])))
#print(node_title.getElementsByTagName('Title')[0])
node_content = doc.createElement('Content')
node_content.appendChild(doc.createTextNode(str(x[4])))
node_posttime = doc.createElement('PostTime')
node_posttime.appendChild(doc.createTextNode(str(x[6])))
node_clicks = doc.createElement('Clicks')
node_clicks.appendChild(doc.createTextNode(str(x[5])))
node_replies = doc.createElement('Replies')
sql = "SELECT * FROM Reply WHERE PostNo = %s ORDER BY ReplyTime Desc LIMIT 0, 3" % x[0]
cursor.execute(sql)
replies = cursor.fetchall()
for y in replies:
node_reply = doc.createElement('Reply')
node_floor = doc.createElement('Floor')
node_floor.appendChild(doc.createTextNode(str(y[1])))
node_replyuser = doc.createElement('ReplyUser')
sql = "SELECT UserName FROM User WHERE UserNo = %s" % y[3]
cursor.execute(sql)
replyuser = cursor.fetchall()
node_replyuser.appendChild(doc.createTextNode(str(replyuser[0][0])))
node_replycontent = doc.createElement('ReplyContent')
original_content = str(y[4])
reply_content = original_content[0:80]+'...' if len(original_content) > 80 else original_content
node_replycontent.appendChild(doc.createTextNode(reply_content))
node_replytime = doc.createElement('ReplyTime')
node_replytime.appendChild(doc.createTextNode(str(y[5])))
node_praisenum = doc.createElement('PraiseNum')
node_praisenum.appendChild(doc.createTextNode(str(y[6])))
node_reply.appendChild(node_floor)
node_reply.appendChild(node_replyuser)
node_reply.appendChild(node_replycontent)
node_reply.appendChild(node_replytime)
node_reply.appendChild(node_praisenum)
node_replies.appendChild(node_reply)
node_post.appendChild(node_No)
node_post.appendChild(node_postuser)
node_post.appendChild(node_title)
node_post.appendChild(node_content)
node_post.appendChild(node_posttime)
node_post.appendChild(node_clicks)
node_post.appendChild(node_replies)
node_posts.appendChild(node_post)
root.appendChild(node_posts)
filepath = './static/xml/sec_%s.xml' % section[0][0]
xmlfile = open(filepath, 'w', encoding='utf-8')
doc.writexml(xmlfile, indent='\t', addindent='\t', newl='\n', encoding='UTF-8')
xmlfile.close()
def make_post_xml(id):
sql = "SELECT * FROM Post WHERE PostNo = %s;" % id
cursor.execute(sql)
target_post = cursor.fetchall()[0]
sql = "SELECT * FROM Reply WHERE PostNo = %s ORDER BY ReplyNo Asc;" % id
cursor.execute(sql)
replies = cursor.fetchall()
replyNum = cursor.rowcount
doc = mdom.Document()
doctype = mdom.DocumentType('Post')
doctype.systemId = "postprofile.dtd"
doctype.publicId = None
doc.appendChild(doctype)
root = doc.createElement('Post')
doc.appendChild(root)
node_No = doc.createElement('No')
node_No.appendChild(doc.createTextNode(str(target_post[0])))
node_postuser = doc.createElement('PostUser')
sql = "SELECT UserName FROM User WHERE UserNo = %s" % target_post[1]
cursor.execute(sql)
username = cursor.fetchall()
node_block = doc.createElement('Block')
sql = "select SectionName from Section where SectionNo = %s" % target_post[2]
cursor.execute(sql)
node_block.appendChild(doc.createTextNode((str(cursor.fetchall()[0][0]))))
node_postuser.appendChild(doc.createTextNode(str(username[0][0])))
node_title = doc.createElement('Title')
node_title.appendChild(doc.createTextNode(str(target_post[3])))
node_content = doc.createElement('Content')
node_content.appendChild(doc.createTextNode(str(target_post[4])))
node_posttime = doc.createElement('PostTime')
node_posttime.appendChild(doc.createTextNode(str(target_post[6])))
node_clicks = doc.createElement('Clicks')
node_clicks.appendChild(doc.createTextNode(str(target_post[5])))
node_replynum = doc.createElement('ReplyNum')
node_replynum.appendChild(doc.createTextNode(str(replyNum)))
node_replies = doc.createElement('Replies')
for y in replies:
node_reply = doc.createElement('Reply')
node_floor = doc.createElement('Floor')
node_floor.appendChild(doc.createTextNode(str(y[1])))
node_orino = doc.createElement('OriginalNo')
node_orino.appendChild(doc.createTextNode(str(y[0])))
node_replyuser = doc.createElement('ReplyUser')
sql = "SELECT UserName FROM User WHERE UserNo = %s" % y[3]
cursor.execute(sql)
replyuser = cursor.fetchall()
node_replyuser.appendChild(doc.createTextNode(str(replyuser[0][0])))
node_replycontent = doc.createElement('ReplyContent')
original_content = str(y[4])
reply_content = original_content[0:80] + '...' if len(original_content) > 80 else original_content
node_replycontent.appendChild(doc.createTextNode(reply_content))
node_replytime = doc.createElement('ReplyTime')
node_replytime.appendChild(doc.createTextNode(str(y[5])))
node_praisenum = doc.createElement('PraiseNum')
node_praisenum.appendChild(doc.createTextNode(str(y[6])))
node_reply.appendChild(node_floor)
node_reply.appendChild(node_orino)
node_reply.appendChild(node_replyuser)
node_reply.appendChild(node_replycontent)
node_reply.appendChild(node_replytime)
node_reply.appendChild(node_praisenum)
node_replies.appendChild(node_reply)
root.appendChild(node_No)
root.appendChild(node_postuser)
root.appendChild(node_block)
root.appendChild(node_title)
root.appendChild(node_content)
root.appendChild(node_posttime)
root.appendChild(node_clicks)
root.appendChild(node_replynum)
root.appendChild(node_replies)
filepath = './static/xml/post_%s.xml' % target_post[0]
xmlfile = open(filepath, 'w', encoding='utf-8')
doc.writexml(xmlfile, indent='\t', addindent='\t', newl='\n', encoding='UTF-8')
xmlfile.close()
def delete_postOf(id):
sql = "delete from Post where PostNo = %s" % id
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def add_click(post_id):
sql = "update Post set Clicks = Clicks+1 where PostNo = %s" % post_id
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def add_praiseTo(reply_id):
sql = "update Reply set PraiseNum=PraiseNum+1 where ReplyNo = %s" % reply_id
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def get_postFromReply(reply_id):
cursor.execute("select PostNo from Reply where ReplyNo = %s" % reply_id)
result = cursor.fetchall()
return result[0][0]
def add_replyTo(userID, post_id, reply):
if '"' in reply:
sql = "insert into Reply(PostNo, UserNo, ReplyContent) values (%s, %s, '%s');" % (post_id, userID, reply)
else:
sql = 'insert into Reply(PostNo, UserNo, ReplyContent) values (%s, %s, "%s");' % (post_id, userID, reply)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
def add_postTo(user_id, section_id, title, content):
sql = "insert into Post(UserNo, SectionNo, Title, Content) values "
if '"' in title and '"' in content:
sql += "(%s, %s, '%s', '%s');" % (user_id, section_id, title, content)
elif '"' in title and "'" in content:
sql += "(%s, %s, '%s', \"%s\");" % (user_id, section_id, title, content)
elif '"' in content:
sql += "(%s, %s, \"%s\", '%s');" % (user_id, section_id, title, content)
else:
sql += "(%s, %s, \"%s\", \"%s\");" % (user_id, section_id, title, content)
try:
cursor.execute(sql)
db.commit()
check_anomalies()
except:
db.rollback()
def get_active_users(section_id, sortedByPosts):
'''
return:
sectionName
active_user: type:
[{
'id'
'name'
'level'
'gender'
'age'
'posts'
'replies'
}]
'''
cursor.execute("select SectionName from Section where SectionNo = %s;" % section_id)
sectionName = cursor.fetchall()[0][0]
cursor.execute("drop view if exists PostNum;")
cursor.execute("drop view if exists ReplyNum;")
cursor.execute("drop view if exists ActiveUsers;")
sql = 'create view PostNum as select UserNo as pUserNo, count(PostNo) as Posts from Post where SectionNo = %s group by UserNo;' % section_id
cursor.execute(sql)
sql = '''create view ReplyNum as select Reply.UserNo as rUserNo, count(*) as Replies from
((select PostNo, UserNo from Post where SectionNo = %s) as Post inner join
(select ReplyNo, UserNo, PostNo from Reply) as Reply on Post.PostNo = Reply.PostNo) group by rUserNo;''' % section_id
cursor.execute(sql)
sql = '''create view ActiveUsers as select pUserNo as UserNo, IfNull(Posts,0) as Posts, IfNull(Replies,0) as Replies from
(select * from PostNum left join ReplyNum on PostNum.pUserNo = ReplyNum.rUserNo union select * from
PostNum right join ReplyNum on PostNum.pUserNo = ReplyNum.rUserNo) as t;'''
cursor.execute(sql)
active_users = []
if sortedByPosts:
cursor.execute("select UserNo, Posts, Replies from ActiveUsers order by Posts desc, Replies desc;")
else:
cursor.execute("select UserNo, Posts, Replies from ActiveUsers order by Replies desc, Posts desc;")
row = cursor.fetchall()
for x in row:
if x[0] == None: continue
temp_user = {}
temp_user['id'] = str(x[0])
temp_user['posts'] = str(x[1])
cursor.execute(
"select UserName, EXP, Gender, timestampdiff(year, Birthday, current_timestamp) from User where UserNo = %s" %
x[0])
row2 = cursor.fetchall()[0]
temp_user['name'] = str(row2[0])
temp_user['level'] = calculate_level(row2[1])
temp_user['gender'] = str(row2[2])
temp_user['age'] = str(row2[3])
temp_user['replies'] = str(x[2])
active_users.append(temp_user)
cursor.execute("drop view if exists PostNum;")
cursor.execute("drop view if exists ReplyNum;")
cursor.execute("drop view if exists ActiveUsers;")
return sectionName, active_users
def get_hot_post(section_id):
cursor.execute("select SectionName from Section where SectionNo = %s;" % section_id)
sectionName = cursor.fetchall()[0][0]
sql = "create view lastReply as select Post.PostNo as PostNo, Post.PostTime as PostTime, max(Reply.ReplyTime) as lastReplyTime " \
"from ((select * from Post where SectionNo = %s) as Post natural join Reply) group by Post.PostNo;" % section_id
cursor.execute(sql)
sql = "select PostNo from lastReply where timediff(PostTime, lastReplyTime) >= " \
"all (select timediff(PostTime, lastReplyTime) from lastReply); "
cursor.execute(sql)
result = cursor.fetchall()
cursor.execute("select * from Post where PostNo = %s;" % result[0][0])
result_post = cursor.fetchall()
hotpost = {}
hotpost['id'] = result_post[0][0]
cursor.execute("select UserName from User where UserNo = %s;" % result_post[0][1])
result_user = cursor.fetchall()
hotpost['user'] = result_user[0][0]
hotpost['title'] = result_post[0][3]
hotpost['content'] = result_post[0][4]
hotpost['clicks'] = result_post[0][5]
hotpost['posttime'] = result_post[0][6]
replies = []
cursor.execute("select * from Reply where PostNo = %s;" % hotpost['id'])
result_replies = cursor.fetchall()
for reply in result_replies:
addreply = {}
addreply['floor'] = reply[1]
cursor.execute("select UserName from User where UserNo = %s;" % reply[3])
result_user = cursor.fetchall()
addreply['user'] = result_user[0][0]
addreply['time'] = reply[5]
addreply['praises'] = reply[6]
addreply['content'] = reply[4]
replies.append(addreply)
hotpost['replies'] = replies
cursor.execute("drop view if exists lastReply;")
return sectionName, hotpost
def get_target_posts(section_id, clicks):
cursor.execute("select SectionName from Section where SectionNo = %s;" % section_id)
sectionName = cursor.fetchall()[0][0]
return_value = []
if clicks:
cursor.execute("select avg(Clicks) from Post where SectionNo = %s group by SectionNo;" % section_id)
avgclicks = cursor.fetchall()[0][0]
sql = "select * from Post where SectionNo = %s and Clicks >= (select avg(Clicks) " \
"from Post where SectionNo = %s group by SectionNo) order by Clicks desc;" % (section_id, section_id)
cursor.execute(sql)
result = cursor.fetchall()
for temp_post in result:
addpost = {}
addpost['id'] = temp_post[0]
cursor.execute("select UserName from User where UserNo = %s;" % temp_post[1])