def post(self, got): self.response.headers['Content-Type'] = 'text/plain' got = self.request.query_string if self.request.get(self.r): nid = utils.new_id() urlfix = '' filetype = self.request.get(self.l) if filetype: if filetype == 'auto': try: filetype = utils.guess_by_content(self.request.get(self.r)) urlfix = '?' + filetype except: urlfix = '' else: urlfix = '?' + filetype while Sprunge.gql('WHERE name = :1', nid).get(): nid = utils.new_id() s = Sprunge() s.content = self.request.get(self.r) s.filetype = filetype s.name = nid s.put() self.response.out.write(' ' + self.u + '/' + nid + urlfix + '\n')
def get_csrf_token(): if "csrf_token" not in session: token = new_id() session["csrf_token"] = token else: token = session["csrf_token"] return token
def load(self): i=0 if self.test(): log.i("Loading %s : %s" % (self.url, self.meta['name'] if (self.meta and 'name' in self.meta) else "Anonyme")) data = self.file.split("\n") char = "," if not "," in data[0]: char = ';' for line in self.file.split("\n"): if i>0 and len(line): row=line.split(char) self.data.append(row) if self.insert(row): self.inserted+=1 i+=1 self.db.exec(""" insert into metadata values ( '%s', '%s', '%s', %d, %d) """% (utils.new_id(32), self.table, self.url, self.date, self.inserted)) url = self.url+"" if self.meta and 'name' in self.meta: url+=" (%s)"%self.meta["name"] log.i("\t%s -> %d row modified or inserted" % (url, self.inserted)) self.db.commit()
def __init__(self, pagesize=-1, page=0): self.id = utils.new_id() self.start_time = time.time() self.process_time = 0 self.columns = () self.pagesize = pagesize self.page = page self.data = []
def processed(*args, **kwargs): try: return f(*args, **kwargs) except Exception: error_id = new_id(32) db.log("api_uncaught_exception", traceback=format_exc(), error_id=error_id) return {"status": 2, "error_id": error_id}
def __init__(self, db, col=[], order=[], set=None, pagesize=-1, page=0): self.db = db self.id = utils.new_id() self.start_time = time.time() self.process_time = 0 self.order = order self.columns = col self.pagesize = pagesize self.page = page self.data = (set if set else []) if set != None: self.close()
def get_token(user_id, lifespan=60 * 60 * 24 * 14): """ Get a existing valid token or generate a new one. Doesn't check permission. :param user_id: ID of user :return: A token in the form of a hex string """ token = (r.get(f"user.{user_id}.token") or b"").decode("utf-8") if token: return token else: token = new_id(64) p = r.pipeline() p.set(f"user.{user_id}.token", token, ex=lifespan) p.set(f"token.{token}.user_id", user_id, ex=lifespan) p.execute() return token
def new_user(email, username, password): """ Create a new user. :param email: The user's email :param username: The user's username :param password: The user's password :return: ID of the new user """ if len(password) < 8: raise AppError("密码长度至少为8个字符") if db.users.find_one({"$or": [{"username": username}, {"email": email}]}): raise AppError("用户名/邮箱已被使用") pwhash, salt = hash_password(password) user_id = new_id() db.users.insert_one({ "id": user_id, "username": username, "email": email, "pwhash": pwhash.hex(), "salt": salt.hex(), "registered_timestamp": time() }) log("register", username=username, email=email, user_id=user_id) return user_id
def __init__(self, *kargs, **kwargs): self.id = new_id() super().__init__(*kargs, **kwargs)
def __init__(self, data=None, id=None, new=False): self.data = data or {} self.id = id or new_id() self.new = new
def __init__(self, js={}, name=""): self.id = js["id"] if js else utils.new_id() self.name = js["name"] if js else name self.list = js["list"] if js else []