def _actually_run(self): import logging import tornado.options logging.getLogger().setLevel(logging.INFO) tornado.options.enable_pretty_logging() import tornado.web import tornado.httpserver import tornado.ioloop import tornado.autoreload # import hashlib # import random # m = hashlib.md5() # m.update((str(random.random()) + str(random.random())).encode('utf-8')) # secret = m.digest() # print("SECRET:", secret) secret = random_key(100) secret = "reiujgerjregiuj" app = tornado.web.Application(self.handlers, static_path=self.static, cookie_secret=secret) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(self.port) logging.info("waiting for requests on http://%s:%d" % (self.hostname or "localhost", self.port)) ioloop = tornado.ioloop.IOLoop.instance() tornado.autoreload.start(ioloop) ioloop.start()
def register(self, fields): """validates the data then registers and returns True if it is valid (otherwise returns false)""" if len(fields) != 6: raise ValueError("fields needs to contain 6 items") fields[-1] = bool(fields[-1]) * 2 email, first, last, pwd, conf_pwd, teacher = fields if pwd != conf_pwd: return False try: User(email) return False #ValueError means that user could not be found, and the username is free except ValueError: fields = [email, teacher, random_key(200), first, last, pwd] if None not in fields and "" not in fields: if len(pwd) < 6: return False first = fields[3] = first.title() last = fields[4] = last.title() fields[-1] = encrypt(fields[-1]) send_email([email], "Registration for assignment management system", """Hi {first} {last}. You have signed up for the CHS assignment management system. In order to activate your account, you must click on this link. {address}/activate/{code} If you did not register for this account, delete this email and nothing will happen.""".format(first=first, last=last, address=WEBSITE_ADDRESS, code=fields[2])) query("INSERT INTO users VALUES (NULL, ?, ?, ?, ?, ?, ?);", fields) return True
def add(self, teacher_id, name, year, key): if None in [name, year, key]: return ["", ""] if name == "": return ["", "Name should not be empty"] if not (year.isdigit() and 6 < int(year) < 13): return ["", "Year should be a number between 7 and 12"] if queryone("SELECT id FROM classes WHERE name=?", [name]) != None: return ["", "That name for a class is already taken"] while key == "": key = random_key(10, string.ascii_lowercase+string.digits) if queryone("SELECT id FROM classes WHERE key=?", [key]) != None: key = "" #at this point, everything is valid query("""INSERT INTO classes VALUES ( NULL, ?, ?, ?, ? )""", [year, name, key, teacher_id]) return ["The class {} has been created. Use the key {} to let students join".format(name, key), ""]
def prepareReset(self): new_key = random_key(200) query("UPDATE users SET key=? WHERE ID=?", [new_key, self.id]) send_email(self.email, "Password reset for CHS assignment management system", """A password reset has been requested for this account on the CHS assignment management system. If you did not click this link, discard this message {address}/reset/{key}""".format(address=WEBSITE_ADDRESS, key=new_key))
def register(self, id): key = random_key(200) query("INSERT INTO sessions VALUES (?, ?, NULL)", [id, key]) self.refresh(key) return key
def activate(self, key): query("UPDATE users SET state=state+1, key=? WHERE (state=0 OR state=2) AND key=?", [random_key(200), key])