Is that better?

This commit is contained in:
Morten Delenk 2016-06-12 18:49:18 +02:00
parent 264f794eea
commit 12661e9006
2 changed files with 10 additions and 16 deletions

View file

@ -11,29 +11,18 @@ import time, string
print("Content-type: text/html\r\n\r\n") print("Content-type: text/html\r\n\r\n")
form=cgi.FieldStorage() form=cgi.FieldStorage()
aid=int(form["aid"].value) aid=int(form["aid"].value)
print("1")
try: try:
seed=int(form["seed"].value) seed=int(form["seed"].value)
print("2")
random.seed(seed) random.seed(seed)
print("3")
checkstr="".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5)) checkstr="".join(random.choice(string.digits+string.ascii_lowercase) for _ in range(5))
print(checkstr)
print("4")
if not checkstr == form["checkstr"].value: if not checkstr == form["checkstr"].value:
print("Captcha's wrong") print("Captcha's wrong")
raise Exception() raise Exception()
print("5")
username=form["username"].value username=form["username"].value
print("6")
message=form["message"].value message=form["message"].value
print("7")
timestamp=int(time.time()) timestamp=int(time.time())
print("8") data={"name":form["title"],"markdown":message,"author":username,"date":timestamp}
data={"name":"","markdown":message,"author":username,"date":timestamp}
print("9")
storage.append("comments-%i"%aid,data) storage.append("comments-%i"%aid,data)
print("10")
except KeyError: except KeyError:
pass pass
@ -47,6 +36,7 @@ html=htmlgen.HTMLgen(pagelayout.getLayoutXML().decode('utf-8'),"Comments")
html.appendHTML("<form action=\"comments.py?aid=%i\" method=\"POST\">"%aid) html.appendHTML("<form action=\"comments.py?aid=%i\" method=\"POST\">"%aid)
html.appendHTML("<input type=\"hidden\" name=\"seed\" value=\"%i\" />"%seed) html.appendHTML("<input type=\"hidden\" name=\"seed\" value=\"%i\" />"%seed)
html.appendHTML("<input placeholder=\"Username\" name=\"username\" /><br />") html.appendHTML("<input placeholder=\"Username\" name=\"username\" /><br />")
html.appendHTML("<input placeholder=\"Title\" name=\"title\" /><br />")
html.appendHTML("<textarea name=\"message\" placeholder=\"Compose your message. Markdown is enabled.\" ></textarea><br />") html.appendHTML("<textarea name=\"message\" placeholder=\"Compose your message. Markdown is enabled.\" ></textarea><br />")
html.appendHTML("<img src=\"data:image/png;base64,%s\" alt=\"Captcha image\" />" % base64.b64encode(capt.getvalue()).decode("UTF-8")) html.appendHTML("<img src=\"data:image/png;base64,%s\" alt=\"Captcha image\" />" % base64.b64encode(capt.getvalue()).decode("UTF-8"))
html.appendHTML("<input placeholder=\"Captcha. lowercase only. case sensitive\" name=\"checkstr\" />") html.appendHTML("<input placeholder=\"Captcha. lowercase only. case sensitive\" name=\"checkstr\" />")
@ -56,4 +46,4 @@ for i in range(count):
html.addArticle(**(storage.get("comments-%i"%aid,i))) html.addArticle(**(storage.get("comments-%i"%aid,i)))
print("<!DOCTYPE html>") print("<!DOCTYPE html>")
print(html.renderSite()) print(html.renderSite(True))

View file

@ -21,7 +21,7 @@ class HTMLgen:
self.asideHTML=text+self.asideHTML self.asideHTML=text+self.asideHTML
def appendHTML(self, text): def appendHTML(self, text):
self.asideHTML=self.asideHTML+text self.asideHTML=self.asideHTML+text
def renderSite(self): def renderSite(self, comments=False):
nav="" nav=""
x=len(self.titles)-1 x=len(self.titles)-1
for title in self.titles[::-1]: for title in self.titles[::-1]:
@ -31,7 +31,11 @@ class HTMLgen:
x=len(self.articles)-1 x=len(self.articles)-1
for article in self.articles[::-1]: for article in self.articles[::-1]:
main=main+("<h2 id=\"%i\">%s</h2><p>Written on <time datetime=\"%s\">%s</time> by %s</p><article>%s</article>" %(x,self.titles[x],datetime.datetime.fromtimestamp(self.dates[x]).strftime("%Y-%m-%d %H:%M:%S"),datetime.datetime.fromtimestamp(self.dates[x]).strftime("%c"),self.authors[x],article)) main=main+("<h2 id=\"%i\">%s</h2><p>Written on <time datetime=\"%s\">%s</time> by %s</p><article>%s</article>" %(x,self.titles[x],datetime.datetime.fromtimestamp(self.dates[x]).strftime("%Y-%m-%d %H:%M:%S"),datetime.datetime.fromtimestamp(self.dates[x]).strftime("%c"),self.authors[x],article))
main=main+("<a href=\"comments.py?aid=%i\">Comments (%i)</a>") % (x, storage.count("comments-%i"%x)) if not comments:
main=main+("<a href=\"comments.py?aid=%i\">Comments (%i)</a>") % (x, storage.count("comments-%i"%x))
x=x-1 x=x-1
styleargs = {"title":self.title,"nav":nav,"main":main,"aside":self.asideHTML,"footer":"Copyright 2016 Morten"} if not comments:
styleargs = {"title":self.title,"nav":nav,"main":main,"aside":self.asideHTML,"footer":"Copyright 2016 Morten"}
else:
styleargs = {"title":self.title,"nav":nav,"main":self.asideHTML+main,"aside":"","footer":"Copyright 2016 Morten"}
return self.layout%styleargs return self.layout%styleargs