Ejemplo n.º 1
0
    def GET(self):
        sql = "SELECT * FROM file WHERE type = 'group' AND is_deleted = 0 AND creator = $creator ORDER BY name LIMIT 1000"
        data = list(xtables.get_file_table().query(sql, vars = dict(creator=xauth.get_current_name())))
        ungrouped_count = xtables.get_file_table().count(where="creator=$creator AND parent_id=0 AND is_deleted=0 AND type!='group'", 
            vars=dict(creator=xauth.get_current_name()))

        tools = list(filter(tool_filter, _tools))[:4]
        return xtemplate.render("index.html", 
            ungrouped_count = ungrouped_count,
            file_type="group_list",
            files = data,
            tools = tools)
Ejemplo n.º 2
0
    def GET(self):
        id = int(xutils.get_argument("id"))
        name = xutils.get_argument("name", "")
        parent_id = xutils.get_argument("parent_id", "")
        record = dao.get_by_id(id)
        db = xtables.get_file_table()
        pathlist = dao.get_pathlist(db, record)
        if parent_id != "":
            db.update(where=dict(id=id), parent_id=parent_id)
            raise web.seeother("/file/view?id=%s" % parent_id)

        if name != "" and name != None:
            filelist = dao.search_name(name, file_type="group")
            newlist = []
            for f in filelist:
                if f.id == id:
                    continue
                else:
                    newlist.append(f)
            filelist = newlist
        else:
            filelist = db.select(where=dict(type="group", is_deleted=0),
                                 limit=200,
                                 order="name DESC")
        return xtemplate.render("file/archive.html",
                                record=record,
                                pathlist=pathlist,
                                filelist=filelist)
Ejemplo n.º 3
0
    def GET(self):
        page = xutils.get_argument("page", 1, type=int)
        db = xtables.get_file_table()
        user_name = xauth.get_current_name()
        pagesize = xconfig.PAGE_SIZE

        vars = dict()
        vars["name"] = user_name
        vars["offset"] = (page - 1) * pagesize
        vars["limit"] = pagesize

        sql = """SELECT a.* FROM file a LEFT JOIN file b ON a.parent_id = b.id 
            WHERE a.is_deleted = 0 
                AND a.type != 'group' 
                AND a.creator = $name AND (b.id is null OR b.type != 'group') 
            ORDER BY mtime DESC LIMIT $offset, $limit"""
        files = db.query(sql, vars=vars)

        count_sql = """SELECT COUNT(1) AS amount FROM file a LEFT JOIN file b ON a.parent_id = b.id 
            WHERE a.is_deleted = 0 
                AND a.type != 'group' 
                AND a.creator = $name
                AND (b.id is null OR b.type != 'group')"""
        amount = db.count(sql=count_sql, vars=vars)

        return xtemplate.render(VIEW_TPL,
                                file_type="group",
                                files=files,
                                file=Storage(name="未分类", type="group"),
                                page=page,
                                page_max=math.ceil(amount / pagesize),
                                page_url="/file/group/ungrouped?page=")
Ejemplo n.º 4
0
 def POST(self):
     content = xutils.get_argument("content", "")
     data = xutils.get_argument("data", "")
     id = xutils.get_argument("id", "0", type=int)
     type = xutils.get_argument("type")
     name = xauth.get_current_name()
     db = xtables.get_file_table()
     where = None
     if xauth.is_admin():
         where = dict(id=id)
     else:
         where = dict(id=id, creator=name)
     kw = dict(size=len(content),
               mtime=xutils.format_datetime(),
               where=where)
     if type == "html":
         kw["data"] = data
         kw["content"] = data
         if xutils.bs4 is not None:
             soup = xutils.bs4.BeautifulSoup(data, "html.parser")
             content = soup.get_text(separator=" ")
             kw["content"] = content
         kw["size"] = len(content)
     else:
         kw["content"] = content
         kw["size"] = len(content)
     rowcount = db.update(**kw)
     if rowcount > 0:
         return dict(code="success")
     else:
         return dict(code="fail")
Ejemplo n.º 5
0
 def remove_by_name(self, name):
     db = xtables.get_file_table()
     file = db.select_one(where=dict(name=name))
     if file is None:
         return dict(code="success")
     db.update(is_deleted=1, where=dict(id=file.id))
     return dict(code="success")
Ejemplo n.º 6
0
Archivo: tag.py Proyecto: burushi/xnote
    def POST(self):
        id = xutils.get_argument("file_id", type=int)
        tags_str = xutils.get_argument("tags")

        tag_db = xtables.get_file_tag_table()
        if tags_str is None or tags_str == "":
            tag_db.delete(where=dict(file_id=id))
            return dict(code="success")
        new_tags = set(tags_str.split(" "))
        file = dao.get_by_id(id)
        db = dao.get_file_db()
        file_db = xtables.get_file_table()
        # 求出两个差集进行运算
        old_tags = tag_db.select(where=dict(file_id=id))
        old_tags = set([v.name for v in old_tags])

        to_delete = old_tags - new_tags
        to_add = new_tags - old_tags

        for item in to_delete:
            tag_db.delete(where=dict(name=item, file_id=id))
        for item in to_add:
            if item == "": continue
            tag_db.insert(name=item, file_id=id)

        file_db.update(related=tags_str, where=dict(id=id))
        return dict(code="", message="", data="OK")
Ejemplo n.º 7
0
    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)))
        xmanager.fire("note.remove", dict(id=id))
        return dict(code="success")
Ejemplo n.º 8
0
def rdb_list_recent_edit(parent_id=None, offset=0, limit=None):
    if limit is None:
        limit = xconfig.PAGE_SIZE
    db = xtables.get_file_table()
    creator = xauth.get_current_name()
    if creator:
        where = "is_deleted = 0 AND (creator = $creator) AND type != 'group'"
    else:
        # 未登录
        where = "is_deleted = 0 AND is_public = 1 AND type != 'group'"

    cache_key = "[%s]note.recent$%s$%s" % (creator, offset, limit)
    files = cacheutil.get(cache_key)
    if files is None:
        files = list(
            db.select(
                what=
                "name, id, parent_id, ctime, mtime, type, creator, priority",
                where=where,
                vars=dict(creator=creator),
                order="priority DESC, mtime DESC",
                offset=offset,
                limit=limit))
        fill_parent_name(files)
        cacheutil.set(cache_key, files, expire=600)
    return files
Ejemplo n.º 9
0
    def GET(self):
        days = xutils.get_argument("days", 30, type=int)
        page = xutils.get_argument("page", 1, type=int)
        page = max(1, page)

        db = xtables.get_file_table()
        where = "is_deleted = 0 AND (creator = $creator OR is_public = 1)"
        files = db.select(where=where,
                          vars=dict(creator=xauth.get_current_name()),
                          order="mtime DESC",
                          offset=(page - 1) * PAGE_SIZE,
                          limit=PAGE_SIZE)
        count = db.count(where, vars=dict(creator=xauth.get_current_name()))
        return xtemplate.render("note/view.html",
                                pathlist=[
                                    Storage(name="最近更新",
                                            type="group",
                                            url="/file/recent_edit")
                                ],
                                file_type="group",
                                files=list(files),
                                file=Storage(name="最近更新", type="group"),
                                page=page,
                                page_max=math.ceil(count / PAGE_SIZE),
                                show_mdate=True,
                                page_url="/file/recent_edit?page=")
Ejemplo n.º 10
0
def list_recent_edit(parent_id=None, offset=0, limit=None):
    if limit is None:
        limit = xconfig.PAGE_SIZE
    db = xtables.get_file_table()
    t = Timer()
    t.start()
    creator = xauth.get_current_name()
    if creator:
        where = "is_deleted = 0 AND (creator = $creator) AND type != 'group'"
    else:
        # 未登录
        where = "is_deleted = 0 AND is_public = 1 AND type != 'group'"

    cache_key = "[%s]note.recent$%s$%s" % (creator, offset, limit)
    files = cacheutil.get(cache_key)
    if files is None:
        files = list(
            db.select(
                what=
                "name, id, parent_id, ctime, mtime, type, creator, priority",
                where=where,
                vars=dict(creator=creator),
                order="priority DESC, mtime DESC",
                offset=offset,
                limit=limit))
        for item in files:
            item.parent_name = query_note_name(item.parent_id)
        cacheutil.set(cache_key, files, expire=600)
    t.stop()
    xutils.trace("NoteDao.ListRecentEdit", "", t.cost_millis())
    return files
Ejemplo n.º 11
0
    def GET(self, tagname):
        from . import dao
        tagname = xutils.unquote(tagname)
        db = xtables.get_file_table()
        page = xutils.get_argument("page", 1, type=int)
        limit = xutils.get_argument("limit", 10, type=int)
        offset = (page - 1) * limit
        pagesize = xconfig.PAGE_SIZE

        if xauth.has_login():
            user_name = xauth.get_current_name()
        else:
            user_name = ""
        count_sql = "SELECT COUNT(1) AS amount FROM file_tag WHERE LOWER(name) = $name AND (user=$user OR is_public=1)"
        sql = "SELECT f.* FROM file f, file_tag ft ON ft.file_id = f.id WHERE LOWER(ft.name) = $name AND (ft.user=$user OR ft.is_public=1) ORDER BY f.ctime DESC LIMIT $offset, $limit"
        count = db.query(count_sql,
                         vars=dict(name=tagname.lower(),
                                   user=user_name))[0].amount

        files = db.query(sql,
                         vars=dict(name=tagname.lower(),
                                   offset=offset,
                                   limit=limit,
                                   user=user_name))
        files = [dao.FileDO.fromDict(f) for f in files]
        return xtemplate.render("note/tagname.html",
                                show_aside=True,
                                tagname=tagname,
                                files=files,
                                show_mdate=True,
                                page_max=math.ceil(count / pagesize),
                                page=page)
Ejemplo n.º 12
0
Archivo: tag.py Proyecto: burushi/xnote
    def GET(self, tagname):
        tagname = xutils.unquote(tagname)
        db = xtables.get_file_table()
        page = xutils.get_argument("page", 1, type=int)
        limit = xutils.get_argument("limit", 10, type=int)
        offset = (page - 1) * limit
        pagesize = xconfig.PAGE_SIZE
        role = "admin"

        if role == "admin":
            count_sql = "SELECT COUNT(1) AS amount FROM file_tag WHERE LOWER(name) = $name"
            sql = "SELECT f.* FROM file f, file_tag ft ON ft.file_id = f.id WHERE LOWER(ft.name) = $name ORDER BY f.ctime DESC LIMIT $offset, $limit"
        else:
            count_sql = "SELECT COUNT(1) AS amount FROM file_tag WHERE LOWER(name) = $name AND groups IN $groups"
            sql = "SELECT f.* FROM file f, file_tag ft ON ft.file_id = f.id WHERE LOWER(ft.name) = $name AND f.groups IN $groups ORDER BY f.ctime DESC LIMIT $offset, $limit"
        groups = ["*", role]
        count = db.query(count_sql,
                         vars=dict(name=tagname.lower(),
                                   groups=groups))[0].amount

        files = db.query(sql,
                         vars=dict(name=tagname.lower(),
                                   offset=offset,
                                   limit=limit,
                                   groups=groups))
        files = [dao.FileDO.fromDict(f) for f in files]
        return xtemplate.render("note/tagname.html",
                                tagname=tagname,
                                files=files,
                                page_max=math.ceil(count / pagesize),
                                page=page)
Ejemplo n.º 13
0
 def GET(self):
     db = xtables.get_file_table()
     if xauth.has_login():
         user_name = xauth.get_current_name()
         tag_list = get_taglist(db, user_name)
     else:
         tag_list = get_taglist(db, "")
     return xtemplate.render("note/taglist.html", tag_list=tag_list)
Ejemplo n.º 14
0
def list_most_visited():
    where = "is_deleted = 0 AND (creator = $creator OR is_public = 1)"
    db = xtables.get_file_table()
    return list(
        db.select(where=where,
                  vars=dict(creator=xauth.get_current_name()),
                  order="visited_cnt DESC",
                  limit=5))
Ejemplo n.º 15
0
 def GET(self):
     id = xutils.get_argument("id")
     db = xtables.get_file_table()
     file = dao.get_by_id(id, db=db)
     user_name = xauth.get_current_name()
     if file.is_public != 1 and user_name != "admin" and user_name != file.creator:
         raise web.seeother("/unauthorized")
     return xtemplate.render("note/print.html", show_menu=False, note=file)
Ejemplo n.º 16
0
Archivo: tag.py Proyecto: burushi/xnote
 def GET(self):
     db = xtables.get_file_table()
     user_name = xauth.get_current_name()
     # if user_name == "admin":
     #     sql = "SELECT name, COUNT(*) AS amount FROM file_tag GROUP BY name ORDER BY amount DESC, name ASC";
     # else:
     #     sql = "SELECT name, COUNT(*) AS amount FROM file_tag WHERE groups in $groups GROUP BY name ORDER BY amount DESC, name ASC";
     tag_list = get_taglist(db, user_name)
     return xtemplate.render("note/taglist.html", tag_list=tag_list)
Ejemplo n.º 17
0
 def GET(self):
     id = xutils.get_argument("id", type=int)
     db = xtables.get_file_table()
     db.update(is_public=0,
               where=dict(id=id, creator=xauth.get_current_name()))
     tag = xtables.get_file_tag_table()
     tag.update(is_public=0,
                where=dict(file_id=id, user=xauth.get_current_name()))
     raise web.seeother("/note/view?id=%s" % id)
Ejemplo n.º 18
0
    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")
        format = xutils.get_argument("_format", "")
        parent_id = xutils.get_argument("parent_id", 0, type=int)

        if key == "":
            key = time.strftime("%Y.%m.%d")

        file = Storage(name=name)
        file.atime = xutils.format_datetime()
        file.mtime = xutils.format_datetime()
        file.ctime = xutils.format_datetime()
        file.creator = xauth.get_current_name()
        file.parent_id = parent_id
        file.type = type
        file.content = ""
        file.size = len(content)
        file.is_public = 0

        code = "fail"
        error = ""
        try:
            db = xtables.get_file_table()
            if name != '':
                f = get_by_name(db, name)
                if f != None:
                    key = name
                    raise Exception(u"%s 已存在" % name)
                file_dict = dict(**file)
                del file_dict["default_value"]
                inserted_id = db.insert(**file_dict)
                update_note_content(inserted_id, content)
                # 更新分组下面页面的数量
                update_children_count(parent_id, db=db)
                xmanager.fire("note.add", dict(name=name, type=type))
                if format == "json":
                    return dict(code="success", id=inserted_id)
                raise web.seeother("/note/view?id={}".format(inserted_id))
        except web.HTTPError as e1:
            raise e1
        except Exception as e:
            xutils.print_exc()
            error = str(e)
        return xtemplate.render("note/add.html",
                                key="",
                                name=key,
                                tags=tags,
                                error=error,
                                pathlist=get_pathlist(db, parent_id),
                                message=error,
                                groups=xutils.call("note.list_group"),
                                code=code)
Ejemplo n.º 19
0
def list_recent_created(parent_id=None, limit=10):
    where = "is_deleted = 0 AND (creator = $creator OR is_public = 1)"
    if parent_id != None:
        where += " AND parent_id = %s" % parent_id
    db = xtables.get_file_table()
    return list(
        db.select(where=where,
                  vars=dict(creator=xauth.get_current_name()),
                  order="ctime DESC",
                  limit=limit))
Ejemplo n.º 20
0
def list_group():
    current_name = str(xauth.get_current_name())
    cache_key = "group.list#" + current_name
    value = cacheutil.get(cache_key)
    if value is None:
        sql = "SELECT * FROM file WHERE type = 'group' AND is_deleted = 0 AND creator = $creator ORDER BY name LIMIT 1000"
        value = list(xtables.get_file_table().query(
            sql, vars=dict(creator=current_name)))
        cacheutil.set(cache_key, value, expire=-1)
    return value
Ejemplo n.º 21
0
def rdb_list_group(current_name=None):
    if current_name is None:
        current_name = str(xauth.get_current_name())
    cache_key = "[%s]note.group.list" % current_name
    value = cacheutil.get(cache_key)
    if value is None:
        sql = "SELECT * FROM file WHERE creator = $creator AND type = 'group' AND is_deleted = 0 ORDER BY name LIMIT 1000"
        value = list(xtables.get_file_table().query(
            sql, vars=dict(creator=current_name)))
        cacheutil.set(cache_key, value, expire=600)
    return value
Ejemplo n.º 22
0
 def POST(self):
     id = xutils.get_argument("id", type=int)
     content = xutils.get_argument("content")
     name = xutils.get_argument("name")
     db = xtables.get_file_table()
     # record = db.select_one(where={"id": id})
     if name != "":
         db.update(where={"id": id}, content=content, name=name)
     else:
         db.update(where={"id": id}, content=content)
     return dict(code="success")
Ejemplo n.º 23
0
    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")
        format = xutils.get_argument("_format", "")
        parent_id = xutils.get_argument("parent_id", 0, type=int)

        if key == "":
            key = time.strftime("%Y.%m.%d")

        file = FileDO(name)
        file.atime = xutils.format_datetime()
        file.mtime = xutils.format_datetime()
        file.ctime = xutils.format_datetime()
        file.creator = xauth.get_current_name()
        # 默认私有
        file.groups = file.creator
        file.parent_id = parent_id
        file.type = type
        file.content = content
        file.size = len(content)

        code = "fail"
        error = ""
        try:
            db = xtables.get_file_table()
            if name != '':
                f = get_by_name(db, name)
                if f != None:
                    key = name
                    raise Exception(u"%s 已存在" % name)
                # 分组提前
                if file.type == "group":
                    file.priority = 1
                inserted_id = db.insert(**file)
                # 更新分组下面页面的数量
                dao.update_children_count(parent_id, db=db)
                if format == "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_exc()
            error = str(e)
        return xtemplate.render("note/add.html",
                                key="",
                                name=key,
                                tags=tags,
                                error=error,
                                message=error,
                                code=code)
Ejemplo n.º 24
0
 def GET(self):
     id = xutils.get_argument("id", "", type=int)
     parent_id = xutils.get_argument("parent_id", "", type=int)
     db = xtables.get_file_table()
     file = db.select_first(where=dict(id=id))
     if file is None:
         return dict(code="fail", message="file not exists")
     db.update(parent_id=parent_id, where=dict(id=id))
     update_children_count(file.parent_id, db=db)
     update_children_count(parent_id, db=db)
     return dict(code="success")
Ejemplo n.º 25
0
 def POST(self):
     id = xutils.get_argument("id")
     name = xutils.get_argument("name")
     if name == "" or name is None:
         return dict(code="fail", message="名称为空")
     db = xtables.get_file_table()
     file = db.select_one(where=dict(name=name))
     if file is not None:
         return dict(code="fail", message="%r已存在" % name)
     db.update(where=dict(id=id), name=name)
     return dict(code="success")
Ejemplo n.º 26
0
def count_user_note(creator):
    if xconfig.DB_ENGINE == "sqlite":
        db    = xtables.get_file_table()
        where = "is_deleted = 0 AND creator = $creator AND type != 'group'"
        count = db.count(where, vars = dict(creator = xauth.get_current_name()))
    else:
        def count_func(key, value):
            if value.is_deleted:
                return False
            return value.creator == creator and type != 'group'
        count = dbutil.prefix_count("note_tiny:%s" % creator, count_func)
    return count
Ejemplo n.º 27
0
def count_ungrouped(creator):
    t = Timer()
    t.start()
    count_key = "*****@*****.**" % creator
    count = cacheutil.get(count_key)
    if count is None:
        count = xtables.get_file_table().count(where="creator=$creator AND parent_id=0 AND is_deleted=0 AND type!='group'", 
            vars=dict(creator=creator))
        xutils.cache_put(count_key, count, expire=600)
    t.stop()
    xutils.trace("NoteDao.CountUngrouped", "", t.cost_millis())
    return count
Ejemplo n.º 28
0
def delete_note(id):
    if xconfig.DB_ENGINE == "sqlite":
        db = xtables.get_file_table()
        sql = "UPDATE file SET is_deleted = 1, mtime=$mtime where id = $id"
        db.query(sql, vars=dict(mtime=xutils.format_datetime(), id=id))
    else:
        note = get_by_id(id)
        if note:
            note.mtime = xutils.format_datetime()
            note.is_deleted = 1
            kv_put_note(id, note)
            update_children_count(note.parent_id)
Ejemplo n.º 29
0
    def POST(self):
        is_public = xutils.get_argument("public", "")
        id = xutils.get_argument("id")
        content = xutils.get_argument("content")
        version = xutils.get_argument("version", 0, type=int)
        file_type = xutils.get_argument("type")
        name = xutils.get_argument("name", "")
        db = xtables.get_file_table()
        file = xutils.call("note.get_by_id", id)

        if file is None:
            return xtemplate.render("note/view.html",
                                    pathlist=[],
                                    file=file,
                                    content=content,
                                    error="笔记不存在")

        # 理论上一个人是不能改另一个用户的存档,但是可以拷贝成自己的
        # 所以权限只能是创建者而不是修改者
        update_kw = dict(content=content,
                         type=file_type,
                         size=len(content),
                         version=version)

        if name != "" and name != None:
            update_kw["name"] = name

        # 不再处理文件,由JS提交
        rowcount = xutils.call("note.update",
                               where=dict(id=id, version=version),
                               **update_kw)
        if rowcount > 0:
            xmanager.fire(
                'note.updated',
                dict(id=id,
                     name=file.name,
                     mtime=dateutil.format_datetime(),
                     content=content,
                     version=version + 1))
            raise web.seeother("/note/view?id=" + str(id))
        else:
            # 传递旧的content
            cur_version = file.version
            file.content = content
            file.version = version
            return xtemplate.render(
                "note/view.html",
                pathlist=[],
                file=file,
                content=content,
                error="更新失败, 版本冲突,当前version={},最新version={}".format(
                    version, cur_version))
Ejemplo n.º 30
0
 def GET(self):
     offset = 0
     db = xtables.get_file_table()
     files = db.select(where="is_deleted=0 AND creator=$name",
                       vars=dict(name=xauth.get_current_name()),
                       order="ctime DESC",
                       offset=offset,
                       limit=PAGE_SIZE)
     return xtemplate.render("note/view.html",
                             file_type="group",
                             files=files,
                             show_date=True,
                             show_opts=False)