Adding comment into DB
This commit is contained in:
parent
9b6a7718f4
commit
4be55cdb23
|
@ -25,6 +25,8 @@ CREATE TABLE IF NOT EXISTS "Comments" (
|
||||||
"User" INTEGER NOT NULL,
|
"User" INTEGER NOT NULL,
|
||||||
"Page" TEXT NOT NULL,
|
"Page" TEXT NOT NULL,
|
||||||
"Reply" INTEGER,
|
"Reply" INTEGER,
|
||||||
|
"Date" INTEGER NOT NULL,
|
||||||
|
"Comment" TEXT NOT NULL,
|
||||||
PRIMARY KEY("ID" AUTOINCREMENT),
|
PRIMARY KEY("ID" AUTOINCREMENT),
|
||||||
FOREIGN KEY("Page") REFERENCES "Pages"("ID"),
|
FOREIGN KEY("Page") REFERENCES "Pages"("ID"),
|
||||||
FOREIGN KEY("User") REFERENCES "Users"("ID")
|
FOREIGN KEY("User") REFERENCES "Users"("ID")
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
</div>
|
</div>
|
||||||
<form action="/Comments" method="POST">
|
<form action="/Comments" method="POST">
|
||||||
<input type="hidden" name="Lang" value="{Lang}">
|
<input type="hidden" name="Lang" value="{Lang}">
|
||||||
|
<input type="hidden" name="Site" value="{Site}">
|
||||||
|
<input type="hidden" name="Page" value="{Page}">
|
||||||
{Form}
|
{Form}
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<div id="PlainDiscussComment{ID}">
|
<div id="PlainDiscussComment{ID}">
|
||||||
<h5>{User}, {Date}, <a href="#PlainDiscussComment{ID}">#{ID}</a></h5>
|
<h5>{User}, {Date}, <a href="#PlainDiscussComment{ID}">#{ID}</a></h5>
|
||||||
|
<button type="submit" name="Reply" value="{ID}">[Locale:DoReply]</button>
|
||||||
<button type="submit" name="Report" value="{ID}">[Locale:DoReport]</button>
|
<button type="submit" name="Report" value="{ID}">[Locale:DoReport]</button>
|
||||||
<button type="submit" name="Delete" value="{ID}">[Locale:DoDelete]</button>
|
<button type="submit" name="Delete" value="{ID}">[Locale:DoDelete]</button>
|
||||||
<button type="submit" name="Reply" value="{ID}">[Locale:DoReply]</button>
|
|
||||||
<p>
|
<p>
|
||||||
{Comment}
|
{Comment}
|
||||||
</p>
|
</p>
|
||||||
|
|
132
Source/Server.py
132
Source/Server.py
|
@ -73,14 +73,66 @@ def GetDB():
|
||||||
|
|
||||||
def GetComments(Site, Page):
|
def GetComments(Site, Page):
|
||||||
DB = GetDB()
|
DB = GetDB()
|
||||||
|
Comments = []
|
||||||
|
|
||||||
SiteID = DB.cursor().execute('SELECT "ID" from "Sites" WHERE "PubKey" == "{}"'.format(Site))
|
SiteID = DB.cursor().execute('SELECT "ID" FROM "Sites" WHERE "PubKey" == "{}"'.format(Site)).fetchall()[0][0]
|
||||||
PageID = DB.cursor().execute('SELECT "ID" FROM "Pages" WHERE "Site" == "{}" AND "Path" == "{}"'.format(SiteID, Page))
|
PageID = DB.cursor().execute('SELECT "ID" FROM "Pages" WHERE "Site" == "{}" AND "Path" == "{}"'.format(SiteID, Page)).fetchall()
|
||||||
Comments = DB.cursor().execute('SELECT * FROM "Comments" WHERE "Page" == "{}"'.format(PageID))
|
if PageID:
|
||||||
|
PageID = PageID[0][0]
|
||||||
|
Comments = DB.cursor().execute('SELECT * FROM "Comments" WHERE "Page" == "{}"'.format(PageID)).fetchall()
|
||||||
|
|
||||||
DB.commit()
|
DB.commit()
|
||||||
return Comments
|
return Comments
|
||||||
|
|
||||||
|
def PostComment(Site, Page, Comment, User, SecKey, Reply):
|
||||||
|
DB = GetDB()
|
||||||
|
|
||||||
|
SiteID = DB.cursor().execute('SELECT "ID" FROM "Sites" WHERE "PubKey" == "{}"'.format(Site)).fetchall()[0][0]
|
||||||
|
PageID = DB.cursor().execute('SELECT "ID" FROM "Pages" WHERE "Site" == "{}" AND "Path" == "{}"'.format(SiteID, Page)).fetchall()
|
||||||
|
if PageID:
|
||||||
|
PageID = PageID[0][0]
|
||||||
|
else:
|
||||||
|
DB.cursor().execute('INSERT INTO "Pages"("Site", "Path") VALUES("{}", "{}")'.format(SiteID, Page))
|
||||||
|
PageID = DB.cursor().execute('SELECT "ID" FROM "Pages" WHERE "Site" == "{}" AND "Path" == "{}"'.format(SiteID, Page)).fetchall()[0][0]
|
||||||
|
UserID = DB.cursor().execute('SELECT "ID" FROM "Users" WHERE "Name" == "{}" AND "SecKey" == "{}"'.format(User, SecKey)).fetchall()
|
||||||
|
if UserID:
|
||||||
|
UserID = UserID[0][0]
|
||||||
|
else:
|
||||||
|
DB.cursor().execute('INSERT INTO "Users"("Name", "SecKey") VALUES("{}", "{}")'.format(User, SecKey))
|
||||||
|
UserID = DB.cursor().execute('SELECT "ID" FROM "Users" WHERE "Name" == "{}" AND "SecKey" == "{}"'.format(User, SecKey)).fetchall()[0][0]
|
||||||
|
|
||||||
|
DB.cursor().execute('INSERT INTO "Comments"("User", "Page", "Reply", "Date", "Comment") VALUES("{}", "{}", "{}", "{}", "{}")'.format(UserID, PageID, Reply, time.time(), Comment))
|
||||||
|
DB.commit()
|
||||||
|
|
||||||
|
print(UserID, PageID, Reply, time.time(), Comment)
|
||||||
|
|
||||||
|
def PostCommentData(Data):
|
||||||
|
Good, Error = "", ""
|
||||||
|
Missing = []
|
||||||
|
for i in ['User', 'Comment']:
|
||||||
|
if not (i in Data and Data[i]):
|
||||||
|
Missing += [i]
|
||||||
|
if len(Missing) > 0:
|
||||||
|
Error = """\
|
||||||
|
<p>
|
||||||
|
Some fields are missing:
|
||||||
|
<br>
|
||||||
|
{}
|
||||||
|
</p>""".format(Missing)
|
||||||
|
else:
|
||||||
|
#try:
|
||||||
|
PostComment(
|
||||||
|
Data['Site'], Data['Page'], Data['Comment'], Data['User'],
|
||||||
|
Data['SecKey'] if 'SecKey' in Data and Data['SecKey'] else secrets.token_urlsafe(64),
|
||||||
|
Data['Reply'] if 'Reply' in Data and Data['Reply'] else None)
|
||||||
|
Good = """\
|
||||||
|
<p>
|
||||||
|
Your comment has been posted!
|
||||||
|
</p>"""
|
||||||
|
#except Exception:
|
||||||
|
# Error = "<p>Server error. Please try again later.</p>"
|
||||||
|
return Good, Error
|
||||||
|
|
||||||
def PatchCommentsHTML(Data):
|
def PatchCommentsHTML(Data):
|
||||||
FormBase = ReadFile('Source/Form.Base.html')
|
FormBase = ReadFile('Source/Form.Base.html')
|
||||||
FormMain = ReadFile('Source/Form.Main.html')
|
FormMain = ReadFile('Source/Form.Main.html')
|
||||||
|
@ -94,17 +146,30 @@ def PatchCommentsHTML(Data):
|
||||||
FormComment = FormComment.replace('[Locale:{}]'.format(String), Locale[String])
|
FormComment = FormComment.replace('[Locale:{}]'.format(String), Locale[String])
|
||||||
|
|
||||||
FormMain = FormMain.format(
|
FormMain = FormMain.format(
|
||||||
SecKey=Data['SecKey'] if Data['SecKey'] else '',
|
SecKey=Data['SecKey'] if 'SecKey' in Data and Data['SecKey'] else '',
|
||||||
User=Data['User'] if Data['User'] else '',
|
User=Data['User'] if 'User' in Data and Data['User'] else '',
|
||||||
Comment=Data['Comment'] if Data['Comment'] else '')
|
Comment=Data['Comment'] if 'Comment' in Data and Data['Comment'] else '')
|
||||||
|
|
||||||
|
if 'Action' in Data and Data['Action']:
|
||||||
|
if Data['Action'] == 'Login':
|
||||||
|
Good, Error = '', ''
|
||||||
|
elif Data['Action'] == 'Post':
|
||||||
|
Good, Error = PostCommentData(Data)
|
||||||
|
elif 'Reply' in Data and Data['Reply']:
|
||||||
|
Good, Error = PostCommentData(Data)
|
||||||
|
elif 'Delete' in Data and Data['Delete']:
|
||||||
|
Good, Error = '', ''
|
||||||
|
else:
|
||||||
|
Good, Error = '', ''
|
||||||
|
|
||||||
Comments = ''
|
Comments = ''
|
||||||
for Comment in GetComments(Data['Site'], Data['Page']):
|
for ID,User,Page,Reply,Date,Comment in GetComments(Data['Site'], Data['Page']):
|
||||||
|
print(Comment)
|
||||||
Comments += "\n<hr>\n" + FormComment.format(
|
Comments += "\n<hr>\n" + FormComment.format(
|
||||||
User="User",
|
User=User,
|
||||||
Date="Date",
|
Date=Date,
|
||||||
ID="ID",
|
ID=ID,
|
||||||
Comment="Comment")
|
Comment=Comment)
|
||||||
|
|
||||||
return FormBase.format(
|
return FormBase.format(
|
||||||
Lang=Data['Lang'] if Data['Lang'] else '',
|
Lang=Data['Lang'] if Data['Lang'] else '',
|
||||||
|
@ -112,28 +177,20 @@ def PatchCommentsHTML(Data):
|
||||||
Site=Data['Site'] if Data['Site'] else '',
|
Site=Data['Site'] if Data['Site'] else '',
|
||||||
Page=Data['Page'] if Data['Page'] else '',
|
Page=Data['Page'] if Data['Page'] else '',
|
||||||
Form=FormMain+Comments,
|
Form=FormMain+Comments,
|
||||||
StatusGood='',
|
StatusGood=Good,
|
||||||
StatusError='')
|
StatusError=Error)
|
||||||
|
|
||||||
def CommentsGet(Req):
|
|
||||||
Data = {}
|
|
||||||
for i in ['Lang','StyleFile','Site','Page']:
|
|
||||||
Data.update({i:Req.args.get(i)})
|
|
||||||
return PatchCommentsHTML(Data)
|
|
||||||
|
|
||||||
def CommentsPost(Req):
|
|
||||||
Data = {}
|
|
||||||
for i in ['Lang','StyleFile','Site','Page','User','CAPTCHA','Comment','SecKey','Action','Reply','Report','Delete']:
|
|
||||||
Data.update({i:Req.form.get(i)})
|
|
||||||
return PatchCommentsHTML(Data)
|
|
||||||
|
|
||||||
@App.route('/Comments', methods=['GET', 'POST'])
|
@App.route('/Comments', methods=['GET', 'POST'])
|
||||||
def Comments():
|
def Comments():
|
||||||
Req = request
|
Req = request
|
||||||
|
Data = {}
|
||||||
if Req.method == 'GET':
|
if Req.method == 'GET':
|
||||||
return CommentsGet(Req)
|
for i in ['Lang','StyleFile','Site','Page']:
|
||||||
|
Data.update({i:Req.args.get(i)})
|
||||||
if Req.method == 'POST':
|
if Req.method == 'POST':
|
||||||
return CommentsPost(Req)
|
for i in ['Lang','StyleFile','Site','Page','User','CAPTCHA','Comment','SecKey','Action','Reply','Report','Delete']:
|
||||||
|
Data.update({i:Req.form.get(i)})
|
||||||
|
return PatchCommentsHTML(Data)
|
||||||
|
|
||||||
@App.route('/Main.css')
|
@App.route('/Main.css')
|
||||||
def SendCSS():
|
def SendCSS():
|
||||||
|
@ -178,34 +235,18 @@ def PatchManageHTML(Data):
|
||||||
Good, Error = '', ''
|
Good, Error = '', ''
|
||||||
|
|
||||||
return HTML.format(
|
return HTML.format(
|
||||||
Lang=Data['Lang'] if Data['Lang'] else '',
|
Lang=Data['Lang'] if 'Lang' in Data and Data['Lang'] else '',
|
||||||
StatusGood=Good,
|
StatusGood=Good,
|
||||||
StatusError=Error)
|
StatusError=Error)
|
||||||
|
|
||||||
"""
|
|
||||||
def ManageGet(Req):
|
|
||||||
Data = {}
|
|
||||||
for i in ['Lang']:
|
|
||||||
Data.update({i:Req.args.get(i)})
|
|
||||||
return PatchManageHTML(Data)
|
|
||||||
|
|
||||||
def ManagePost(Req):
|
|
||||||
Data = {}
|
|
||||||
for i in ['Lang', 'Action']:
|
|
||||||
Data.update({i:Req.form.get(i)})
|
|
||||||
return PatchManageHTML(Data)
|
|
||||||
"""
|
|
||||||
|
|
||||||
@App.route('/Manage', methods=['GET', 'POST'])
|
@App.route('/Manage', methods=['GET', 'POST'])
|
||||||
def SendManage():
|
def SendManage():
|
||||||
Req = request
|
Req = request
|
||||||
Data = {}
|
Data = {}
|
||||||
if Req.method == 'GET':
|
if Req.method == 'GET':
|
||||||
#return ManageGet(Req)
|
|
||||||
for i in ['Lang']:
|
for i in ['Lang']:
|
||||||
Data.update({i:Req.args.get(i)})
|
Data.update({i:Req.args.get(i)})
|
||||||
elif Req.method == 'POST':
|
elif Req.method == 'POST':
|
||||||
#return ManagePost(Req)
|
|
||||||
for i in ['Lang', 'Action']:
|
for i in ['Lang', 'Action']:
|
||||||
Data.update({i:Req.form.get(i)})
|
Data.update({i:Req.form.get(i)})
|
||||||
return PatchManageHTML(Data)
|
return PatchManageHTML(Data)
|
||||||
|
@ -213,11 +254,8 @@ def SendManage():
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
Locales = GetLocales()
|
Locales = GetLocales()
|
||||||
Config = GetConfig()
|
Config = GetConfig()
|
||||||
|
|
||||||
#DB = sqlite3.connect('Comments.db')
|
|
||||||
DB = GetDB()
|
DB = GetDB()
|
||||||
InitDB()
|
InitDB()
|
||||||
#DB.close()
|
|
||||||
|
|
||||||
if Config['Development']:
|
if Config['Development']:
|
||||||
App.run(host='0.0.0.0', port=Config['Port'], debug=True)
|
App.run(host='0.0.0.0', port=Config['Port'], debug=True)
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
<head>
|
<head>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<iframe src="http://localhost:8080/Comments?Lang=it&Site=Test&Page=/index.html" width="100%" height="80%"></iframe>
|
<iframe src="http://localhost:8080/Comments?Lang=it&Site=5dGQi5vT0hxWULCprKW7Og9g0xRNkHqxxEKI3cGdh-A&Page=/index.html" width="100%" height="80%"></iframe>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue