old-homepage/comments.py

44 lines
1.7 KiB
Python
Raw Normal View History

2016-06-12 16:04:01 +00:00
#!/usr/bin/env python3
from htmlgen import *
import storage
import cgi
import random
import base64
from io import BytesIO
from captcha.image import ImageCaptcha
2016-06-12 16:06:16 +00:00
import time, string
2016-06-12 16:04:01 +00:00
print("Content-type: text/html\r\n\r\n")
form=cgi.FieldStorage()
2016-06-12 16:05:36 +00:00
aid=form["aid"]
2016-06-12 16:04:01 +00:00
try:
seed=form["seed"]
random.seed(seed)
checkstr=list("".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5)))
if not checkstr == form["checkstr"]:
print("Captcha's wrong")
raise Exception()
username=form["username"]
message=form["message"]
timestamp=int(time.time())
data={"name":"","markdown":message,"author":username,"date":timestamp}
storage.append("comments-%i"%aid,data)
except:
pass
seed=random.SystemRandom().randint(0,2**24)
random.seed(seed)
checkstr=list("".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5)))
image = ImageCaptcha()
capt = image.generate(checkstr)
html=htmlgen.HTMLgen(pagelayout.getLayoutXML().decode('utf-8'),"Comments")
html.appendHTML("<form action=\"comments.py?aid=%i\" method=\"POST\""%aid)
html.appendHTML("<input type=\"hidden\" name=\"seed\" value=\"%i\" />"%seed)
html.appendHTML("<input placeholder=\"Username\" name=\"username\" /><br />")
html.appendHTML("<textarea name=\"message\" placeholder=\"Compose your message. Markdown is enabled.\" /><br />")
html.appendHTML("<img src=\"data:image/png;base64,%s alt=\"Captcha image\" />" % base64.b64encode(capt.getvalue()))
html.appendHTML("<input placeholder=\"Captcha. lowercase only. case sensitive\" name=\"checkstr\" />")
html.appendHTML("<input type=\"submit\" /></form>")
count=storage.count("comments-%i"%aid)
for i in range(count):
html.addArticle(**(storage.get("comments-%i"%aid,i)))