old-homepage/comments.py

53 lines
2.2 KiB
Python
Raw Normal View History

2016-06-12 16:04:01 +00:00
#!/usr/bin/env python3
from htmlgen import *
2016-06-12 16:30:10 +00:00
import traceback
import sys
2016-06-12 16:04:01 +00:00
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
form=cgi.FieldStorage()
2016-06-12 16:07:45 +00:00
aid=int(form["aid"].value)
2016-06-12 16:04:01 +00:00
try:
2016-06-12 16:07:45 +00:00
seed=int(form["seed"].value)
2016-06-12 16:04:01 +00:00
random.seed(seed)
2016-06-12 16:42:06 +00:00
checkstr="".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5))
2016-06-12 16:07:03 +00:00
if not checkstr == form["checkstr"].value:
2016-06-12 16:04:01 +00:00
print("Captcha's wrong")
2016-06-12 16:37:59 +00:00
raise Exception()
2016-06-12 16:07:03 +00:00
username=form["username"].value
2016-06-12 16:37:59 +00:00
message=form["message"].value
2016-06-12 16:04:01 +00:00
timestamp=int(time.time())
2016-06-12 16:56:11 +00:00
data={"name":cgi.escape(form["title"].value),"markdown":cgi.escape(message),"author":cgi.escape(username),"date":timestamp}
2016-06-12 16:04:01 +00:00
storage.append("comments-%i"%aid,data)
2016-06-12 16:33:03 +00:00
except KeyError:
2016-06-12 16:04:01 +00:00
pass
2016-06-12 16:32:13 +00:00
2016-06-12 16:04:01 +00:00
seed=random.SystemRandom().randint(0,2**24)
random.seed(seed)
2016-06-12 16:42:06 +00:00
checkstr="".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5))
2016-06-12 16:04:01 +00:00
image = ImageCaptcha()
capt = image.generate(checkstr)
html=htmlgen.HTMLgen(pagelayout.getLayoutXML().decode('utf-8'),"Comments")
2016-06-12 16:39:03 +00:00
html.appendHTML("<form action=\"comments.py?aid=%i\" method=\"POST\">"%aid)
2016-06-12 16:04:01 +00:00
html.appendHTML("<input type=\"hidden\" name=\"seed\" value=\"%i\" />"%seed)
html.appendHTML("<input placeholder=\"Username\" name=\"username\" /><br />")
2016-06-12 16:49:18 +00:00
html.appendHTML("<input placeholder=\"Title\" name=\"title\" /><br />")
2016-07-13 15:01:48 +00:00
html.appendHTML("<textarea rows=\"25\" cols=\"80\" name=\"message\" placeholder=\"Compose your message. Markdown is enabled.\" ></textarea><br />")
2016-06-12 16:12:09 +00:00
html.appendHTML("<img src=\"data:image/png;base64,%s\" alt=\"Captcha image\" />" % base64.b64encode(capt.getvalue()).decode("UTF-8"))
2016-06-12 16:04:01 +00:00
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):
2016-07-13 14:51:23 +00:00
html.addArticle(aid=aid,**(storage.get("comments-%i"%aid,i)))
2016-06-12 16:08:22 +00:00
sys.stdout.buffer.write("Content-type: text/html\r\n\r\n".encode('utf8'))
sys.stdout.buffer.write("<!DOCTYPE html>".encode('utf8'))
sys.stdout.buffer.write(html.renderSite(True).encode('utf8'))
2016-07-31 10:01:06 +00:00
sys.stdout.flush()
#print(html.renderSite(True))