Skip to content
Browse files

clean up

  • Loading branch information...
1 parent 49e22ab commit fb7ed3b7a40bdafb43b2259f946d047b60b93937 @hunterlang committed Mar 23, 2012
Showing with 411 additions and 423 deletions.
  1. +411 −423 server.py
View
834 server.py
@@ -32,21 +32,21 @@
class Application(tornado.web.Application):
def __init__(self):
handlers = [
- (r"/", IndexHandler),
- (r"/adduser",AddHandler),
- (r"/login",LoginHandler),
- (r"/remove", RemoveHandler),
- (r"/changepwd", ChangePasswordHandler),
- (r"/logout", LogoutHandler),
- (r"/teacherlogin", TeacherLogin),
- (r"/addteacher", AddTeacherHandler),
- (r"/test", TestHandler),
- (r"/comment/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", CommentHandler),
- (r"/doc_id/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", IdHandler),
- (r"/view/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", ViewHandler),
- (r"/commit", CommitHandler),
- (r"/viewcomments/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", ViewCommentHandler),
- (r"/ie", IEHandler),
+ (r"/", IndexHandler),
+ (r"/adduser",AddHandler),
+ (r"/login",LoginHandler),
+ (r"/remove", RemoveHandler),
+ (r"/changepwd", ChangePasswordHandler),
+ (r"/logout", LogoutHandler),
+ (r"/teacherlogin", TeacherLogin),
+ (r"/addteacher", AddTeacherHandler),
+ (r"/test", TestHandler),
+ (r"/comment/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", CommentHandler),
+ (r"/doc_id/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", IdHandler),
+ (r"/view/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", ViewHandler),
+ (r"/commit", CommitHandler),
+ (r"/viewcomments/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", ViewCommentHandler),
+ (r"/ie", IEHandler),
]
@@ -79,33 +79,33 @@ def to_html(self, string):
body = body + string
#create leading tab
if not body[:6] == ''' ''':
- body = break_string + break_string + body
+ body = break_string + break_string + body
return body
def get_full_name(self, name):
try:
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute('select firstname, lastname from people where username=? ', [name])
- uname = c.fetchone()
- username = uname[0] + " " + uname[1]
- c.close()
- conn.close()
- return username
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute('select firstname, lastname from people where username=? ', [name])
+ uname = c.fetchone()
+ username = uname[0] + " " + uname[1]
+ c.close()
+ conn.close()
+ return username
except TypeError:
- return "Full name unknown"
+ return "Full name unknown"
def convert_date(self, date):
date = date.split("-")
months = ['01','02','03','04','05','06','07','08','09', '10', '11', '12'], ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
for i in range(len(months[0])):
- date[1] = date[1].replace(months[0][i], months[1][i])
+ date[1] = date[1].replace(months[0][i], months[1][i])
datestring = date[1] +' '+ date[2] + ', ' + date[0]
return datestring
def get_date(self):
date = str(datetime.date.today())
date = date.split("-")
months = ['01','02','03','04','05','06','07','08','09', '10', '11', '12'], ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
for i in range(len(months[0])):
- date[1] = date[1].replace(months[0][i], months[1][i])
+ date[1] = date[1].replace(months[0][i], months[1][i])
datestring = date[1] +' '+ date[2] + ', ' + date[0]
return datestring
def rand_str(self, leng):
@@ -123,126 +123,126 @@ def terminalize(self, string):
class IndexHandler(BaseHandler):
def get(self):
if self.current_user:
- name = tornado.escape.xhtml_escape(self.current_user)
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select firstname from people where username=? ', [name] )
- var = c.fetchone()
- c.close()
- conn.close()
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select title, body, doc_id, teacher from docs where author=? order by dateadded', [name])
- return_list = c.fetchall()
- documents = []
- try:
- for doc in return_list:
- if doc[3]: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#009b00'>Shared with " + doc[3] + ".</p>")]
- else: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#cd0a0a'>Not shared.</p>")]
- documents.append(data)
- except: TypeError
- if not documents:
- self.render('static/templates/nodocs.html')
- else:
- documents.reverse()
- self.render("static/app/index.html", name=var[0], docs=documents)
+ name = tornado.escape.xhtml_escape(self.current_user)
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select firstname from people where username=? ', [name] )
+ var = c.fetchone()
+ c.close()
+ conn.close()
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select title, body, doc_id, teacher from docs where author=? order by dateadded', [name])
+ return_list = c.fetchall()
+ documents = []
+ try:
+ for doc in return_list:
+ if doc[3]: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#009b00'>Shared with " + doc[3] + ".</p>")]
+ else: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#cd0a0a'>Not shared.</p>")]
+ documents.append(data)
+ except: TypeError
+ if not documents:
+ self.render('static/templates/nodocs.html')
+ else:
+ documents.reverse()
+ self.render("static/app/index.html", name=var[0], docs=documents)
elif self.current_teacher:
- name = tornado.escape.xhtml_escape(self.current_teacher)
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select title, body, doc_id, permalink, author, dateadded, period from docs where teacher=? order by dateadded', [name])
- return_list = c.fetchall()
-
- documents = []
- for doc in return_list:
- if doc[3]:
- c.execute('select teacher, comment from comments where comment_id=?', [doc[2]])
- comments = c.fetchall()
+ name = tornado.escape.xhtml_escape(self.current_teacher)
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select title, body, doc_id, permalink, author, dateadded, period from docs where teacher=? order by dateadded', [name])
+ return_list = c.fetchall()
+
+ documents = []
+ for doc in return_list:
+ if doc[3]:
+ c.execute('select teacher, comment from comments where comment_id=?', [doc[2]])
+ comments = c.fetchall()
#someone in the universe commented
- if comments:
- for comment in comments:
+ if comments:
+ for comment in comments:
#you commented
- if comment[0] == self.current_teacher:
- data = [(doc[0], 'Submitted by ' + self.get_full_name(doc[4]) + " from " + doc[6] + " on " + self.convert_date(doc[5]) , 'href="' + doc[3] + '">',"<p style='color:#009b00'>You commented.</p>" )]
+ if comment[0] == self.current_teacher:
+ data = [(doc[0], 'Submitted by ' + self.get_full_name(doc[4]) + " from " + doc[6] + " on " + self.convert_date(doc[5]) , 'href="' + doc[3] + '">',"<p style='color:#009b00'>You commented.</p>" )]
#comments, but not from you
- else: data = [(doc[0], 'Submitted by ' + self.get_full_name(doc[4]) + " from " + doc[6] + " on " + self.convert_date(doc[5]) , 'href="' + doc[3] + '">', "<p style='color:#cd0a0a'>No comments from you.</p>")]
+ else: data = [(doc[0], 'Submitted by ' + self.get_full_name(doc[4]) + " from " + doc[6] + " on " + self.convert_date(doc[5]) , 'href="' + doc[3] + '">', "<p style='color:#cd0a0a'>No comments from you.</p>")]
#no comments at all
- else:
- data = [(doc[0], 'Submitted by ' + self.get_full_name(doc[4]) + " from " + doc[6] + " on " + self.convert_date(doc[5]) , 'href="' + doc[3] + '">', "<p style='color:#cd0a0a'>No comments from you.</p>")]
- documents.append(data)
+ else:
+ data = [(doc[0], 'Submitted by ' + self.get_full_name(doc[4]) + " from " + doc[6] + " on " + self.convert_date(doc[5]) , 'href="' + doc[3] + '">', "<p style='color:#cd0a0a'>No comments from you.</p>")]
+ documents.append(data)
if not documents:
- self.render('static/templates/teacher_nodocs.html')
+ self.render('static/templates/teacher_nodocs.html')
else:
- self.render("static/app/teacher.html", name="", docs=documents)
+ self.render("static/app/teacher.html", name="", docs=documents)
else:
- self.render("static/templates/login.html")
-
+ self.render("static/templates/login.html")
+
class LoginHandler(BaseHandler):
- @tornado.web.asynchronous
- def get(self):
- if not self.current_user and not self.current_teacher:
- self.render("static/templates/login.html")
- else:
- self.redirect("/")
+ @tornado.web.asynchronous
+ def get(self):
+ if not self.current_user and not self.current_teacher:
+ self.render("static/templates/login.html")
+ else:
+ self.redirect("/")
def post(self):
- arguments = self.request.arguments
- name = arguments['username'][0]
- password = arguments['password'][0]
-
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select username, password from people where username=? ', [name] )
- var = c.fetchone()
- if var == None:
- self.render("static/templates/login_error.html", error="User not found.")
- else:
+ arguments = self.request.arguments
+ name = arguments['username'][0]
+ password = arguments['password'][0]
+
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select username, password from people where username=? ', [name] )
+ var = c.fetchone()
+ if var == None:
+ self.render("static/templates/login_error.html", error="User not found.")
+ else:
#print "Entered: " + password.strip()
#print "Actual: " + var[1]
- if password.strip() == var[1]:
- self.set_secure_cookie("user", name, expires_days=1)
- self.redirect("/")
- else:
- self.render("static/templates/login_error.html", error="Password incorrect.")
+ if password.strip() == var[1]:
+ self.set_secure_cookie("user", name, expires_days=1)
+ self.redirect("/")
+ else:
+ self.render("static/templates/login_error.html", error="Password incorrect.")
class TeacherLogin(LoginHandler):
- @tornado.web.asynchronous
- def get(self):
- self.render("static/templates/teacher_login.html")
+ @tornado.web.asynchronous
+ def get(self):
+ self.render("static/templates/teacher_login.html")
def post(self):
- arguments = self.request.arguments
- name = arguments['username'][0]
- password = arguments['password'][0]
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select username, password from teachers where username=? ', [name] )
- var = c.fetchone()
- if var == None:
- self.render("static/templates/teacher_login_error.html", error="User not found.")
- else:
- print password
- print var[1]
- if password == var[1]:
- self.clear_cookie("user")
- self.set_secure_cookie("teacher", name)
- finish = "Password Correct."
- self.redirect("/")
- else:
- finish = "Wrong Password."
- self.render("static/templates/teacher_login_error.html", error="Password incorrect.")
+ arguments = self.request.arguments
+ name = arguments['username'][0]
+ password = arguments['password'][0]
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select username, password from teachers where username=? ', [name] )
+ var = c.fetchone()
+ if var == None:
+ self.render("static/templates/teacher_login_error.html", error="User not found.")
+ else:
+ print password
+ print var[1]
+ if password == var[1]:
+ self.clear_cookie("user")
+ self.set_secure_cookie("teacher", name)
+ finish = "Password Correct."
+ self.redirect("/")
+ else:
+ finish = "Wrong Password."
+ self.render("static/templates/teacher_login_error.html", error="Password incorrect.")
class AddHandler(BaseHandler):
- @tornado.web.asynchronous
- def get(self):
- self.render("static/templates/register.html")
+ @tornado.web.asynchronous
+ def get(self):
+ self.render("static/templates/register.html")
def post(self):
- try:
- arguments = self.request.arguments
- name = arguments['username'][0].strip()
- password = arguments['password'][0].strip()
- email = arguments['email'][0].strip()
- firstname = arguments['firstname'][0].strip()
- lastname = arguments['lastname'][0].strip()
- except KeyError:
- self.render("static/templates/register_error.html", error='Please fill in all the fields.')
+ try:
+ arguments = self.request.arguments
+ name = arguments['username'][0].strip()
+ password = arguments['password'][0].strip()
+ email = arguments['email'][0].strip()
+ firstname = arguments['firstname'][0].strip()
+ lastname = arguments['lastname'][0].strip()
+ except KeyError:
+ self.render("static/templates/register_error.html", error='Please fill in all the fields.')
pwrd = password
conn = sqlite3.connect('doc.db')
@@ -251,36 +251,36 @@ def post(self):
c.execute('select username from people where email=? ', [email])
var = c.fetchone()
if var == None:
- c.execute('select username from people where username=? ', [name])
- var = c.fetchone()
- if var == None:
- for t in [(firstname, lastname, name, pwrd, email)]:
- c.execute('insert into people values (?,?,?,?,?)', t)
- conn.commit()
- c.close()
- conn.close()
- self.redirect("/")
+ c.execute('select username from people where username=? ', [name])
+ var = c.fetchone()
+ if var == None:
+ for t in [(firstname, lastname, name, pwrd, email)]:
+ c.execute('insert into people values (?,?,?,?,?)', t)
+ conn.commit()
+ c.close()
+ conn.close()
+ self.redirect("/")
else:
- self.render("static/templates/register_error.html", error='Username "' + name + '" is taken.')
+ self.render("static/templates/register_error.html", error='Username "' + name + '" is taken.')
else:
- self.render("static/templates/register_error.html", error='Email address "' + email + '" is already in use.')
+ self.render("static/templates/register_error.html", error='Email address "' + email + '" is already in use.')
class AddTeacherHandler(BaseHandler):
- @tornado.web.asynchronous
- def get(self):
- if self.current_teacher or self.current_user:
- self.redirect("/")
- else: self.render("static/templates/teacher_register.html")
+ @tornado.web.asynchronous
+ def get(self):
+ if self.current_teacher or self.current_user:
+ self.redirect("/")
+ else: self.render("static/templates/teacher_register.html")
def post(self):
- try:
- arguments = self.request.arguments
- print arguments
- name = arguments['username'][0].strip()
- password = arguments['password'][0].strip()
- email = arguments['email'][0].strip()
- firstname = arguments['firstname'][0].strip()
- lastname = arguments['lastname'][0].strip()
- except KeyError:
- self.render("static/templates/teacher_register_error.html", error='Please fill in all the fields.')
+ try:
+ arguments = self.request.arguments
+ print arguments
+ name = arguments['username'][0].strip()
+ password = arguments['password'][0].strip()
+ email = arguments['email'][0].strip()
+ firstname = arguments['firstname'][0].strip()
+ lastname = arguments['lastname'][0].strip()
+ except KeyError:
+ self.render("static/templates/teacher_register_error.html", error='Please fill in all the fields.')
pwrd = password
conn = sqlite3.connect('doc.db')
@@ -289,105 +289,105 @@ def post(self):
c.execute('select username from teachers where email=? ', [email])
var = c.fetchone()
if var == None:
- c.execute('select username from teachers where username=? ', [name])
- var = c.fetchone()
- if var == None:
- for t in data:
- c.execute('insert into teachers values (?,?,?,?,?)', t)
- conn.commit()
- c.close()
- conn.close()
- self.redirect("/teacherlogin")
+ c.execute('select username from teachers where username=? ', [name])
+ var = c.fetchone()
+ if var == None:
+ for t in data:
+ c.execute('insert into teachers values (?,?,?,?,?)', t)
+ conn.commit()
+ c.close()
+ conn.close()
+ self.redirect("/teacherlogin")
else:
- self.render("static/templates/teacher_register_error.html", error='Username "' + name + '" is taken.')
+ self.render("static/templates/teacher_register_error.html", error='Username "' + name + '" is taken.')
else:
- self.render("static/templates/teacher_register_error.html", error='Email address "' + email + '" is already in use.')
+ self.render("static/templates/teacher_register_error.html", error='Email address "' + email + '" is already in use.')
class IdHandler(BaseHandler):
- @tornado.web.asynchronous
- @tornado.web.authenticated
- def get(self, doc_id):
- try:
- name = tornado.escape.xhtml_escape(self.current_user)
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute('select * from docs where doc_id=? and author=?', [unicode(doc_id), name])
- file_data = c.fetchone()
- c.execute('select * from comments where comment_id=?', [doc_id])
- comments = c.fetchall()
- c.close()
- conn.close()
- if comments: comments = 'yes'
- else: comments = 'no'
- username = self.get_full_name(name)
- file_body = file_data[4]
- body=self.to_html(file_body)
+ @tornado.web.asynchronous
+ @tornado.web.authenticated
+ def get(self, doc_id):
+ try:
+ name = tornado.escape.xhtml_escape(self.current_user)
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute('select * from docs where doc_id=? and author=?', [unicode(doc_id), name])
+ file_data = c.fetchone()
+ c.execute('select * from comments where comment_id=?', [doc_id])
+ comments = c.fetchall()
+ c.close()
+ conn.close()
+ if comments: comments = 'yes'
+ else: comments = 'no'
+ username = self.get_full_name(name)
+ file_body = file_data[4]
+ body=self.to_html(file_body)
- self.render("static/templates/edit_headers.html", name=username, subject=file_data[2], date=self.convert_date(file_data[5]), period=file_data[3], title=file_data[0], body=body, teacher=file_data[8], comments=comments, doc_id=doc_id)
- except TypeError:
- self.finish("<html><body><div align='center'>Oops. Couldn't find that. Bummer.</div></body></html>")
+ self.render("static/templates/edit_headers.html", name=username, subject=file_data[2], date=self.convert_date(file_data[5]), period=file_data[3], title=file_data[0], body=body, teacher=file_data[8], comments=comments, doc_id=doc_id)
+ except TypeError:
+ self.finish("<html><body><div align='center'>Oops. Couldn't find that. Bummer.</div></body></html>")
class CommentHandler(BaseHandler):
- @tornado.web.asynchronous
- def get(self, doc_id):
- if not self.current_teacher:
- self.redirect("/teacherlogin")
- else:
- try:
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute('select * from docs where doc_id=?', [doc_id])
- file_data = c.fetchone()
- c.execute('select * from comments where comment_id=?', [doc_id])
- comments = c.fetchall()
- c.close()
- conn.close()
- username = self.get_full_name(file_data[1])
- file_body = file_data[4]
- body=self.to_html(file_body)
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select teacher, comment from comments where comment_id=?', [doc_id])
- comments = c.fetchall()
- new_comments = []
- for comment in comments:
- if comment[0] == self.current_teacher:
- comment = ("You", comment[1])
- new_comments.append(comment)
- self.render("static/templates/comment.html", comments = new_comments, name=username, subject=file_data[2], date=self.convert_date(file_data[5]), period=file_data[3], title=file_data[0], body=body, teacher=file_data[8], doc_id=doc_id)
- except TypeError:
- self.finish("<html><body><div align='center'>Oops. Couldn't find that. Bummer.</div></body></html>")
-
+ @tornado.web.asynchronous
+ def get(self, doc_id):
+ if not self.current_teacher:
+ self.redirect("/teacherlogin")
+ else:
+ try:
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute('select * from docs where doc_id=?', [doc_id])
+ file_data = c.fetchone()
+ c.execute('select * from comments where comment_id=?', [doc_id])
+ comments = c.fetchall()
+ c.close()
+ conn.close()
+ username = self.get_full_name(file_data[1])
+ file_body = file_data[4]
+ body=self.to_html(file_body)
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select teacher, comment from comments where comment_id=?', [doc_id])
+ comments = c.fetchall()
+ new_comments = []
+ for comment in comments:
+ if comment[0] == self.current_teacher:
+ comment = ("You", comment[1])
+ new_comments.append(comment)
+ self.render("static/templates/comment.html", comments = new_comments, name=username, subject=file_data[2], date=self.convert_date(file_data[5]), period=file_data[3], title=file_data[0], body=body, teacher=file_data[8], doc_id=doc_id)
+ except TypeError:
+ self.finish("<html><body><div align='center'>Oops. Couldn't find that. Bummer.</div></body></html>")
+
def post(self, doc_id):
- if not self.current_teacher:
- sel.redirect("/teacherlogin")
- else:
- comment = self.get_argument("comment")
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute('insert into comments values (?,?,?)', [comment, doc_id, self.current_teacher])
- conn.commit()
- c.execute('select teacher, comment from comments where comment_id=?', [doc_id])
- comments = c.fetchall()
- new_comments = []
- for comment in comments:
- if comment[0] == self.current_teacher:
- comment = ("You", comment[1])
- new_comments.append(comment)
+ if not self.current_teacher:
+ sel.redirect("/teacherlogin")
+ else:
+ comment = self.get_argument("comment")
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute('insert into comments values (?,?,?)', [comment, doc_id, self.current_teacher])
+ conn.commit()
+ c.execute('select teacher, comment from comments where comment_id=?', [doc_id])
+ comments = c.fetchall()
+ new_comments = []
+ for comment in comments:
+ if comment[0] == self.current_teacher:
+ comment = ("You", comment[1])
+ new_comments.append(comment)
c.close()
conn.close()
loader = template.Loader("")
html = loader.load("static/templates/comment_list.html").generate(comments=new_comments)
self.finish(html)
class ViewHandler(BaseHandler):
- @tornado.web.asynchronous
- def get(self, doc_id):
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute('select * from docs where doc_id=?', [unicode(doc_id)])
- file_data = c.fetchone()
- c.close()
- conn.close()
+ @tornado.web.asynchronous
+ def get(self, doc_id):
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute('select * from docs where doc_id=?', [unicode(doc_id)])
+ file_data = c.fetchone()
+ c.close()
+ conn.close()
#file_data:
#0: Title
#1: author's username
@@ -398,103 +398,92 @@ def get(self, doc_id):
#6: doc_id
#7: permalink
#8: teacher
- self.render("static/templates/doc_id_view.html", title=file_data[0], subject=file_data[2], name=self.get_full_name(file_data[1]), period=file_data[3], date=self.convert_date(file_data[5]), body=self.to_html(file_data[4]))
+ self.render("static/templates/doc_id_view.html", title=file_data[0], subject=file_data[2], name=self.get_full_name(file_data[1]), period=file_data[3], date=self.convert_date(file_data[5]), body=self.to_html(file_data[4]))
class ChangePasswordHandler(BaseHandler):
- @tornado.web.asynchronous
- @tornado.web.authenticated
- def get(self):
- self.render("static/templates/changepass.html")
+ @tornado.web.asynchronous
+ @tornado.web.authenticated
+ def get(self):
+ self.render("static/templates/changepass.html")
def post(self):
- #get arguments
- arguments = self.request.arguments
- currpass = arguments['currentpassword'][0]
- newpass = arguments['newpassword'][0]
-
- #get username
- name = tornado.escape.xhtml_escape(self.current_user)
-
- #commit changes to database
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- data = [currpass, newpass, name, currpass]
- c.execute('update people set password = replace(password, ?, ?) where username=? and password=?; ', data)
- conn.commit()
- c.close()
- conn.close()
-
- #Redirect to the app without logging out. ******Should there be a log out here?*****
- self.redirect("/")
+ #get arguments
+ arguments = self.request.arguments
+ currpass = arguments['currentpassword'][0]
+ newpass = arguments['newpassword'][0]
+
+ #get username
+ name = tornado.escape.xhtml_escape(self.current_user)
+
+ #commit changes to database
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ data = [currpass, newpass, name, currpass]
+ c.execute('update people set password = replace(password, ?, ?) where username=? and password=?; ', data)
+ conn.commit()
+ c.close()
+ conn.close()
+
+ #Redirect to the app without logging out. ******Should there be a log out here?*****
+ self.redirect("/")
class CommitHandler(BaseHandler):
- @tornado.web.asynchronous
- @tornado.web.authenticated
- def post(self):
+ @tornado.web.asynchronous
+ @tornado.web.authenticated
+ def post(self):
#get the form data
- arguments = self.request.arguments
- title = arguments['title'][0]
- subject = arguments['subject'][0]
- period = int(arguments['period'][0])
- doc_id = str(arguments['location'][0])
- teacher = arguments['teacher'][0]
+ arguments = self.request.arguments
+ title = arguments['title'][0]
+ subject = arguments['subject'][0]
+ period = int(arguments['period'][0])
+ doc_id = str(arguments['location'][0])
+ teacher = arguments['teacher'][0]
#get the username and full name of the author
- name = tornado.escape.xhtml_escape(self.current_user)
- username = self.get_full_name(name)
-
- doc_id = doc_id.strip('#')[-36:]
-
+ name = tornado.escape.xhtml_escape(self.current_user)
+ username = self.get_full_name(name)
+
+ doc_id = doc_id.strip('#')[-36:]
+
#select the document
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute('select * from docs where doc_id=? and author=?', [doc_id, name])
- document = c.fetchone()
- c.close()
- conn.close()
-
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute('select * from docs where doc_id=? and author=?', [doc_id, name])
+ document = c.fetchone()
+ c.close()
+ conn.close()
+
#convert the file's body to valid html
- body = self.to_html(document[4])
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- c.execute("select * from teachers where username=?", [teacher])
- var = c.fetchone()
- # if the teacher exists
- if var:
- #commit the changes to the database
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- for data in [(title, subject, "Period " + str(period), teacher, doc_id, name)]:
- c.execute('update docs set title=?, subject=?, period=?, teacher=? where doc_id=? and author=?', data)
- conn.commit()
- c.execute('select dateadded from docs where doc_id=?', [doc_id])
- date = c.fetchone()
- c.execute('select email from teachers where username=?', [teacher])
- email = c.fetchone()
- c.execute('select permalink from docs where doc_id=?', [doc_id])
- link = c.fetchone()
- c.close()
- conn.close()
-
- #email the teacher the link to the document
- #****speed this up*******#
- #msg = MIMEMultipart()
- #msg.attach(MIMEText("You can view this document at " + link[0]))
- #msg['From'] = "Document Uploader"
- #msg['To'] = email[0]
- #msg['Subject'] = name + " shared their document."
-
- #mailServer.sendmail(gmail_user, email[0], msg.as_string())
-
- #if the teacher doesnt exist
- else:
- conn = sqlite3.connect("doc.db")
- c = conn.cursor()
- for data in [(title, subject, "Period " + str(period), doc_id, name)]:
- c.execute('update docs set title=?, subject=?, period=? where doc_id=? and author=?', data)
- conn.commit()
- c.execute("select dateadded from docs where doc_id=?", [doc_id])
- date = c.fetchone()
- c.close()
- conn.close()
+ body = self.to_html(document[4])
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ c.execute("select * from teachers where username=?", [teacher])
+ var = c.fetchone()
+ # if the teacher exists
+ if var:
+ #commit the changes to the database
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ for data in [(title, subject, "Period " + str(period), teacher, doc_id, name)]:
+ c.execute('update docs set title=?, subject=?, period=?, teacher=? where doc_id=? and author=?', data)
+ conn.commit()
+ c.execute('select dateadded from docs where doc_id=?', [doc_id])
+ date = c.fetchone()
+ c.execute('select email from teachers where username=?', [teacher])
+ email = c.fetchone()
+ c.execute('select permalink from docs where doc_id=?', [doc_id])
+ link = c.fetchone()
+ c.close()
+ conn.close()
+
+ else:
+ conn = sqlite3.connect("doc.db")
+ c = conn.cursor()
+ for data in [(title, subject, "Period " + str(period), doc_id, name)]:
+ c.execute('update docs set title=?, subject=?, period=? where doc_id=? and author=?', data)
+ conn.commit()
+ c.execute("select dateadded from docs where doc_id=?", [doc_id])
+ date = c.fetchone()
+ c.close()
+ conn.close()
#write the html to a permanent file and return the link to the file.
datestring = self.convert_date(date[0])
@@ -503,109 +492,108 @@ def post(self):
self.finish(html)
class TestHandler(BaseHandler):
- @tornado.web.asynchronous
- @tornado.web.authenticated
- def post(self):
- #get username and full name
- name = tornado.escape.xhtml_escape(self.current_user)
- username = self.get_full_name(name)
-
- #get the uploaded file and filedata
- file_content = self.request.files['file'][0]
- file_body = file_content['body']
- filename = file_content['filename']
-
- #write the data out to a file
- random_file_name = self.rand_str(8) + os.path.splitext(filename)[1]
- output_file = open("docs/" + random_file_name, 'w')
- output_file.write(file_body)
- output_file.close()
-
-
-
- #set default values to be converted to txt(sent to core.py) and inserted in database
- path = random_file_name
- period = 8
- period = "Period " + str(period)
- title = "New Document"
- subject = 'Some Subject'
-
-
- #create the document ID
- doc_id = str(uuid.uuid4())
-
- #call 'core.py' and get the location of the new txt file
- var = commands.getstatusoutput('python core.py ' + path + " " + self.terminalize(username) + ' ' + self.terminalize(subject) + ' ' + str(period) + ' ' + self.terminalize(title))
-
- #get the location of the txt file created by core.py, open it, and set a string equal to its contents
- text_file = var[1]
- filein = open(text_file)
- string = filein.read()
-
- #create a new document entry in the database with the above values.
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- for t in [(unicode(title, 'utf-8'), unicode(tornado.escape.xhtml_escape(self.current_user), 'utf-8'), unicode(subject, 'utf-8'), unicode(period, 'utf-8'), unicode(string, 'utf-8'), unicode(str(datetime.date.today()), 'utf-8'), unicode(doc_id), unicode('http://hunterlang.com/comment/' + doc_id), None)]:
- c.execute('insert into docs values (?,?,?,?,?,?,?,?,?)', t)
- conn.commit()
- c.close()
- conn.close()
-
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select title, body, doc_id, teacher from docs where author=? order by dateadded', [name])
- return_list = c.fetchall()
- documents = []
- for doc in return_list:
- if doc[3]: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#009b00'>Shared with " + doc[3] + ".</p>")]
- else: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#cd0a0a'>Not shared.</p>")]
- documents.append(data)
+ @tornado.web.asynchronous
+ @tornado.web.authenticated
+ def post(self):
+ #get username and full name
+ name = tornado.escape.xhtml_escape(self.current_user)
+ username = self.get_full_name(name)
+
+ #get the uploaded file and filedata
+ file_content = self.request.files['file'][0]
+ file_body = file_content['body']
+ filename = file_content['filename']
+
+ #write the data out to a file
+ random_file_name = self.rand_str(8) + os.path.splitext(filename)[1]
+ output_file = open("docs/" + random_file_name, 'w')
+ output_file.write(file_body)
+ output_file.close()
+
+
+
+ #set default values to be converted to txt(sent to core.py) and inserted in database
+ path = random_file_name
+ period = 8
+ period = "Period " + str(period)
+ title = "New Document"
+ subject = 'Some Subject'
+
+
+ #create the document ID
+ doc_id = str(uuid.uuid4())
+
+ #call 'core.py' and get the location of the new txt file
+ var = commands.getstatusoutput('python core.py ' + path + " " + self.terminalize(username) + ' ' + self.terminalize(subject) + ' ' + str(period) + ' ' + self.terminalize(title))
+
+ #get the location of the txt file created by core.py, open it, and set a string equal to its contents
+ text_file = var[1]
+ filein = open(text_file)
+ string = filein.read()
+
+ #create a new document entry in the database with the above values.
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ for t in [(unicode(title, 'utf-8'), unicode(tornado.escape.xhtml_escape(self.current_user), 'utf-8'), unicode(subject, 'utf-8'), unicode(period, 'utf-8'), unicode(string, 'utf-8'), unicode(str(datetime.date.today()), 'utf-8'), unicode(doc_id), unicode('http://hunterlang.com/comment/' + doc_id), None)]:
+ c.execute('insert into docs values (?,?,?,?,?,?,?,?,?)', t)
+ conn.commit()
+ c.close()
+ conn.close()
+
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select title, body, doc_id, teacher from docs where author=? order by dateadded', [name])
+ return_list = c.fetchall()
+ documents = []
+ for doc in return_list:
+ if doc[3]: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#009b00'>Shared with " + doc[3] + ".</p>")]
+ else: data = [(doc[0], doc[1].rstrip()[:80], 'href="/doc_id/' + doc[2] + '">', "<p style='color:#cd0a0a'>Not shared.</p>")]
+ documents.append(data)
documents.reverse()
loader = template.Loader("")
html = loader.load("static/templates/just_list.html").generate(docs=documents)
val = {
- "HTML": html
- }
+ "HTML": html
+ }
self.finish(val)
class RemoveHandler(BaseHandler):
- @tornado.web.asynchronous
- @tornado.web.authenticated
- def post(self):
- referer = self.request.arguments['location'][0]
- referer = referer.strip('#')
- doc_id = referer[-36:]
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- name = self.current_user
- c.execute('delete from docs where author=? and doc_id=?', [name, doc_id])
- c.execute('delete from comments where comment_id=?', [doc_id])
- conn.commit()
- c.close()
- conn.close()
- self.redirect('/')
+ @tornado.web.asynchronous
+ @tornado.web.authenticated
+ def post(self):
+ referer = self.request.arguments['location'][0]
+ referer = referer.strip('#')
+ doc_id = referer[-36:]
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ name = self.current_user
+ c.execute('delete from docs where author=? and doc_id=?', [name, doc_id])
+ c.execute('delete from comments where comment_id=?', [doc_id])
+ conn.commit()
+ c.close()
+ conn.close()
+ self.redirect('/')
class LogoutHandler(BaseHandler):
- @tornado.web.asynchronous
- def get(self):
- self.clear_cookie("user")
- #put this in a different handler
- self.clear_cookie("teacher")
- self.redirect("/")
+ @tornado.web.asynchronous
+ def get(self):
+ self.clear_cookie("user")
+ self.clear_cookie("teacher")
+ self.redirect("/")
class ViewCommentHandler(BaseHandler):
- @tornado.web.authenticated
- @tornado.web.asynchronous
- def get(self, doc_id):
- conn = sqlite3.connect('doc.db')
- c = conn.cursor()
- c.execute('select * from comments where comment_id=?', [doc_id])
- comments = c.fetchall()
- self.render('static/templates/viewcomments.html', comments=comments, doc_id=doc_id)
+ @tornado.web.authenticated
+ @tornado.web.asynchronous
+ def get(self, doc_id):
+ conn = sqlite3.connect('doc.db')
+ c = conn.cursor()
+ c.execute('select * from comments where comment_id=?', [doc_id])
+ comments = c.fetchall()
+ self.render('static/templates/viewcomments.html', comments=comments, doc_id=doc_id)
class IEHandler(BaseHandler):
- def get(self):
- self.render("static/templates/ie.html")
+ def get(self):
+ self.render("static/templates/ie.html")
def main():
- tornado.options.parse_command_line()
- http_server = tornado.httpserver.HTTPServer(Application(), xheaders=True)
- http_server.listen(options.port)
- tornado.ioloop.IOLoop.instance().start()
+ tornado.options.parse_command_line()
+ http_server = tornado.httpserver.HTTPServer(Application(), xheaders=True)
+ http_server.listen(options.port)
+ tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()

0 comments on commit fb7ed3b

Please sign in to comment.
Something went wrong with that request. Please try again.