def insert(file): name = file.name f = get_by_name(name) if f is not None: raise FileExistsError(name) if not hasattr(file, "is_deleted"): file.is_deleted = 0 if not hasattr(file, "ctime"): file.ctime = dateutil.get_seconds() file.sctime = dateutil.format_time(file.ctime) if not hasattr(file, "atime"): file.atime = dateutil.get_seconds() file.satime = dateutil.format_time(file.atime) if not hasattr(file, "mtime"): file.mtime = dateutil.get_seconds() file.smtime = dateutil.format_time(file.mtime) if hasattr(file, "atime"): delattr(file, "atime") if hasattr(file, "ctime"): delattr(file, "ctime") if hasattr(file, "mtime"): delattr(file, "mtime") # values = [build_sql_row(file, k) for k in file] # sql = "insert into file (%s) values (%s)" % (','.join(file), ",".join(values)) return get_db().insert(**file)
def POST(self): name = xutils.get_argument("name", "") tags = xutils.get_argument("tags", "") key = xutils.get_argument("key", "") content = xutils.get_argument("content", "") type = xutils.get_argument("type", "post") _type = xutils.get_argument("_type", "") parent_id = xutils.get_argument("parent_id", 0, type=int) if key == "": key = time.strftime("%Y.%m.%d") file = FileDO(name) file.atime = dateutil.get_seconds() file.satime = dateutil.format_time() file.mtime = dateutil.get_seconds() file.smtime = dateutil.format_time() file.ctime = dateutil.get_seconds() file.sctime = dateutil.format_time() file.creator = xauth.get_current_name() # 默认私有 file.groups = file.creator file.parent_id = parent_id file.type = type file.content = content code = "fail" error = "" try: if name != '': f = dao.get_by_name(name) if f != None: key = name raise Exception(u"%s 已存在" % name) # 分组提前 if file.type == "group": file.priority = 1 f = dao.insert(file) inserted = dao.get_by_name(name) if _type == "json": return dict(code="success", id=inserted.id) raise web.seeother("/file/view?id={}".format(inserted.id)) except web.HTTPError as e1: raise e1 except Exception as e: xutils.print_stacktrace() error = str(e) return xtemplate.render("file/add.html", key="", name=key, tags=tags, error=error, message=error, code=code)
def GET(self): id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") file = None if id == "" and name == "": return dict(code="fail", message="id,name至少一个不为空") db = xtables.get_file_table() if id != "": file = db.select_one(where=dict(id=int(id), is_deleted=0)) elif name != "": file = get_by_name(db, name) if file is None: return dict(code="fail", message="文件不存在") id = file.id if not xauth.is_admin() and file.creator != xauth.get_current_name(): return dict(code="fail", message="没有删除权限") if file.type == "group": children_count = db.count(where="parent_id=%s AND is_deleted=0" % id) if children_count > 0: return dict(code="fail", message="分组不为空") db.update(is_deleted=1, mtime=dateutil.format_time(), where=dict(id=int(id))) db.delete(where="is_deleted=1 AND mtime < $date", vars=dict(date=dateutil.before(days=30, format=True))) return dict(code="success")
def update(where, **kw): db = get_file_db() kw["mtime"] = dateutil.format_time() # 处理乐观锁 version = where.get("version") if version: kw["version"] = version + 1 return db.update(where=where, vars=None, **kw)
def __init__(self, name, path, op): self.name = name self.path = path self.op = op info = self if os.path.exists(path): info.path = path st = os.stat(path) info.mtime = dateutil.format_time(st.st_mtime) info.size = fsutil.format_size(st.st_size) else: info.path = None info.mtime = None info.size = None
def remove_by_id(self, id): db = xtables.get_file_table() file = db.select_one(where=dict(id=int(id))) if file is None: return dict(code="fail", message="文件不存在") if file.type == "group": children_count = db.count(where="parent_id=%s AND is_deleted=0" % id) if children_count > 0: return dict(code="fail", message="分组不为空") db.update(is_deleted=1, smtime=dateutil.format_time(), where=dict(id=int(id))) db.delete(where="is_deleted=1 AND smtime < $date", vars=dict(date=dateutil.before(days=30, format=True))) return dict(code="success")
def get_info(): info = T() info.path = _dest_path if os.path.exists(_dest_path): info.name = _zipname info.path = _dest_path st = os.stat(_dest_path) info.mtime = dateutil.format_time(st.st_mtime) info.size = fsutil.format_size(st.st_size) else: info.name = None info.path = None info.mtime = None info.size = None return info
def visit_by_id(id, db=None): sql = "UPDATE file SET visited_cnt = visited_cnt + 1, atime='%s' where id = %s " % \ (dateutil.format_time(), id) return db.query(sql)
def visit_by_id(id): db = get_db() # return db.update(satime=dateutil.format_time(), visited_cnt=visited_cnt+1, where=dict(id=id)) sql = "update file set visited_cnt = visited_cnt + 1, satime='%s' where id = %s and visited_cnt < %s" % \ (dateutil.format_time(), id, MAX_VISITED_CNT) return db.query(sql)