示例#1
0
def _get_users():
    """获取用户,内部接口"""
    global _users

    # 有并发风险
    if _users is not None:
        return _users

    _users = {}
    # 默认的账号
    _users["admin"] = Storage(name = "admin", 
        password = "******", 
        salt = "",
        mtime = "",
        token = gen_new_token())

    user_list = dbutil.prefix_list("user")
    for user in user_list:
        if user.name is None:
            xutils.trace("UserList", "invalid user %s" % user)
            continue
        if isinstance(user.config, dict):
            user.config = Storage(**user.config)
        else:
            user.config = Storage()
        name = user.name.lower()
        _users[name] = user
    return _users
示例#2
0
def db_execute(path, sql, args=None):
    from xconfig import Storage
    db = sqlite3.connect(path)
    cursorobj = db.cursor()
    kv_result = []
    try:
        # print(sql)
        if args is None:
            cursorobj.execute(sql)
        else:
            cursorobj.execute(sql, args)
        result = cursorobj.fetchall()
        # result.rowcount
        db.commit()
        for single in result:
            resultMap = Storage()
            for i, desc in enumerate(cursorobj.description):
                name = desc[0]
                resultMap[name] = single[i]
            kv_result.append(resultMap)

    except Exception as e:
        raise e
    finally:
        db.close()
    return kv_result
示例#3
0
def get_user_config(name):
    user = get_user(name)
    if user != None:
        if user.config is None:
            user.config = Storage()
        return user.config
    return None
示例#4
0
def add_user(name, password):
    if name == "" or name == None:
        return
    if password == "" or password == None:
        return
    if not is_valid_username(name):
        return dict(code="INVALID_NAME", message="非法的用户名")

    name  = name.lower()
    found = find_by_name(name)
    if found is not None:
        found.mtime = xutils.format_time()
        found.salt  = textutil.random_string(6)
        found.token = gen_new_token()
        found.password = password
        update_user(name, found)
    else:
        user = Storage(name=name,
            password=password,
            token=gen_new_token(),
            ctime=xutils.format_time(),
            salt=textutil.random_string(6),
            mtime=xutils.format_time())
        dbutil.put("user:%s" % name, user)
        xutils.trace("UserAdd", name)
    refresh_users()
示例#5
0
文件: view.py 项目: 552301/xnote
 def convert(item):
     v = Storage()
     v.name = item.key
     v.summary = item.value
     v.mtime = item.mtime
     v.ctime = item.ctime
     v.url = "#"
     v.priority = 0
     return v
示例#6
0
文件: view.py 项目: 552301/xnote
def handle_note_recommend(kw, file, user_name):
    ctx = Storage(id=file.id, name = file.name, creator = file.creator, 
        content = file.content,
        parent_id = file.parent_id,
        result = [])
    xmanager.fire("note.recommend", ctx)
    kw.recommended_notes = ctx.result
    kw.next_note = NOTE_DAO.find_next_note(file, user_name)
    kw.prev_note = NOTE_DAO.find_prev_note(file, user_name)
示例#7
0
文件: dbutil.py 项目: Cicin411/xnote
def get_object_from_bytes(bytes):
    if bytes is None:
        return None
    str_value = bytes.decode("utf-8")
    try:
        obj = json.loads(str_value)
    except:
        xutils.print_exc()
        return str_value
    if isinstance(obj, dict):
        obj = Storage(**obj)
    return obj
示例#8
0
文件: xauth.py 项目: licshire/xnote
def _get_users():
    """获取用户,内部接口"""
    global _users

    # 有并发风险
    if _users is not None:
        return _users

    # defaults = read_users_from_ini("config/users.default.ini")
    # customs  = read_users_from_ini("config/users.ini")
    db = xtables.get_user_table()
    db_users = db.select()
    _users = {}
    # 默认的账号
    _users["admin"] = Storage(name="admin",
                              password="******",
                              mtime="",
                              token=gen_new_token())

    for user in db_users:
        _users[user.name] = user

    return _users
示例#9
0
def add_user(name, password):
    if name == "" or name == None:
        return dict(code="PARAM_ERROR", message="name为空")
    if password == "" or password == None:
        return dict(code="PARAM_ERROR", message="password为空")
    if not is_valid_username(name):
        return dict(code="INVALID_NAME", message="非法的用户名")

    name = name.lower()
    found = find_by_name(name)
    if found is not None:
        return dict(code="fail", message="用户已存在")
    else:
        user = Storage(name=name,
                       password=password,
                       token=gen_new_token(),
                       ctime=xutils.format_time(),
                       salt=textutil.random_string(6),
                       mtime=xutils.format_time())
        dbutil.put("user:%s" % name, user)
        xutils.trace("UserAdd", name)
        refresh_users()
        return dict(code="success", message="create success")
示例#10
0
文件: view.py 项目: 552301/xnote
    def GET(self, op, id = None):
        if id is None:
            id = xutils.get_argument("id", "")
        name          = xutils.get_argument("name", "")
        page          = xutils.get_argument("page", 1, type=int)
        pagesize      = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu     = xutils.get_argument("show_menu", "true") != "false"
        show_search   = xutils.get_argument("show_search", "true") != "false"
        orderby       = xutils.get_argument("orderby", None)
        is_iframe     = xutils.get_argument("is_iframe", "false")
        user_name     = xauth.current_name()
        show_add_file = False
        title         = None
        show_pagination = True
        show_search_div = False

        kw = Storage()
        kw.show_left   = False
        kw.show_groups = False
        kw.show_aside  = True
        kw.groups = []
        kw.recommended_notes = []

        if id == "0":
            raise web.found("/")
        # 回收站的笔记也能看到
        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            file = NOTE_DAO.get_by_id(id)
        elif name is not None:
            file = NOTE_DAO.get_by_name(name)
        if file is None:
            raise web.notfound()
        
        if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        pathlist        = xutils.call("note.list_path", file)
        can_edit        = (file.creator == user_name) or (user_name == "admin")
        role            = xauth.get_current_role()

        # 定义一些变量
        show_mdate     = False
        files          = []
        recent_created = []
        amount         = 0
        show_recommend = False
        template_name  = "note/view.html"
        next_note      = None
        prev_note      = None

        title  = file.name
        if file.type == "group":
            if orderby != None and file.orderby != orderby:
                NOTE_DAO.update(where = dict(id = file.id, creator = file.creator), orderby = orderby)
            else:
                orderby = file.orderby

            files  = NOTE_DAO.list_by_parent(user_name, file.id, (page-1)*pagesize, pagesize, orderby)
            amount = NOTE_DAO.count(user_name, file.id)
            content         = file.content
            show_search_div = True
            show_add_file   = True
            show_mdate      = True
            kw.show_aside   = False
        elif file.type == "md" or file.type == "text":
            content = file.content
            show_recommend = True
            show_pagination = False
            if op == "edit":
                show_recommend = False
                template_name = "note/editor/markdown_edit.html"
        elif file.type == "list":
            kw.show_aside = False
            show_pagination = False
        else:
            # post/html 等其他类型
            handle_note_content(file)
            show_recommend = True
            show_pagination = False

        # 处理笔记背后的文件系统
        handle_note_files(kw, file)

        if show_recommend and user_name is not None:
            # 推荐系统
            handle_note_recommend(kw, file, user_name)
            
        
        xmanager.fire("note.view", file)
        if op == "edit":
            kw.show_aside = False

        if is_iframe == "true":
            show_menu = False
            show_search = False

        kw.show_menu   = show_menu
        kw.show_search = show_search

        # 如果是页面,需要查出上级目录列表
        handle_left_dir(kw, user_name, file, op)

        return xtemplate.render(template_name,
            html_title    = title,
            file          = file, 
            note_id       = id,
            op            = op,
            show_mdate    = show_mdate,
            show_add_file = show_add_file,
            show_pagination = show_pagination,
            can_edit = can_edit,
            pathlist = pathlist,
            page_max = math.ceil(amount/pagesize),
            page     = page,
            page_url = "/note/view?id=%s&orderby=%s&page=" % (id, orderby),
            files    = files, 
            recent_created    = recent_created,
            is_iframe         = is_iframe, **kw)
示例#11
0
    def GET(self, op, id=None):
        if id is None:
            id = xutils.get_argument("id", "")
        name = xutils.get_argument("name", "")
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu = xutils.get_argument("show_menu", "true") != "false"
        show_search = xutils.get_argument("show_search", "true") != "false"
        orderby = xutils.get_argument("orderby", None)
        is_iframe = xutils.get_argument("is_iframe", "false")
        token = xutils.get_argument("token", "")
        user_name = xauth.current_name()

        kw = Storage()
        kw.show_left = False
        kw.show_groups = False
        kw.show_aside = True
        kw.groups = []
        kw.files = []
        kw.op = op
        kw.user_name = user_name
        kw.page = page
        kw.orderby = orderby
        kw.pagesize = pagesize
        kw.recommended_notes = []
        kw.show_add_file = False
        kw.template_name = "note/page/view.html"
        kw.search_action = "/note/timeline"
        kw.search_placeholder = "搜索笔记"

        if id == "0":
            raise web.found("/")
        # 回收站的笔记也能看到
        file = find_note_for_view(token, id, name)

        if file is None:
            raise web.notfound()

        if token == "":
            check_auth(file, user_name)

        pathlist = NOTE_DAO.list_path(file)
        can_edit = (file.creator == user_name) or (user_name == "admin")
        role = xauth.get_current_role()

        # 定义一些变量
        show_mdate = False
        recent_created = []
        show_recommend = False
        next_note = None
        prev_note = None

        event_ctx = Storage(id=file.id, user_name=user_name)
        xmanager.fire("note.view", event_ctx)

        view_func = VIEW_FUNC_DICT.get(file.type, view_md_func)
        view_func(file, kw)

        if show_recommend and user_name is not None:
            # 推荐系统
            handle_note_recommend(kw, file, user_name)

        if op == "edit":
            kw.show_aside = False
            kw.show_search = False
            kw.show_comment = False

        if is_iframe == "true":
            kw.show_menu = False
            kw.show_search = False

        template_name = kw['template_name']
        del kw['template_name']

        # 如果是页面,需要查出上级目录列表
        handle_left_dir(kw, user_name, file, op)
        return xtemplate.render_by_ua(
            template_name,
            html_title=file.name,
            file=file,
            note_id=id,
            show_mdate=show_mdate,
            can_edit=can_edit,
            pathlist=pathlist,
            page_url="/note/view?id=%s&orderby=%s&page=" % (id, orderby),
            recent_created=recent_created,
            CREATE_BTN_TEXT_DICT=CREATE_BTN_TEXT_DICT,
            is_iframe=is_iframe,
            **kw)
示例#12
0
文件: view.py 项目: Cicin411/xnote
    def GET(self, op):
        id            = xutils.get_argument("id", "")
        name          = xutils.get_argument("name", "")
        page          = xutils.get_argument("page", 1, type=int)
        pagesize      = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu     = xutils.get_argument("show_menu", "true") != "false"
        orderby       = xutils.get_argument("orderby", "mtiem_desc")
        user_name     = xauth.get_current_name()
        show_add_file = False
        title         = None
        show_pagination = True
        show_search_div = False

        if id == "0":
            raise web.found("/")
        # 回收站的笔记也能看到
        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            file = xutils.call("note.get_by_id", id)
        elif name is not None:
            file = xutils.call("note.get_by_name", name, db=db)
        if file is None:
            raise web.notfound()
        
        if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        pathlist        = xutils.call("note.list_path", file)
        can_edit        = (file.creator == user_name) or (user_name == "admin")
        role            = xauth.get_current_role()

        # 定义一些变量
        show_groups    = False
        show_mdate     = False
        files          = []
        recent_created = []
        groups         = []
        amount         = 0
        show_recommend = False
        template_name  = "note/view.html"
        next_note      = None
        prev_note      = None
        filelist       = None
        xconfig.note_history.put(dict(user=user_name, 
            link = "/note/view?id=%s" % id, 
            name = file.name))
        recommended_notes = []

        title  = file.name
        if file.type == "group":
            files  = xutils.call("note.list_by_parent", user_name, file.id, (page-1)*pagesize, pagesize, orderby)
            amount = xutils.call("note.count", user_name, file.id)
            content         = file.content
            show_search_div = True
            show_add_file   = True
            show_mdate      = True
        elif file.type == "md" or file.type == "text":
            content = file.content
            show_recommend = True
            show_pagination = False
            if op == "edit":
                show_recommend = False
                template_name = "note/editor/markdown_edit.html"
        else:
            content = file.content
            content = content.replace(u'\xad', '\n')
            content = content.replace(u'\n', '<br/>')
            file.data = file.data.replace(u"\xad", "\n")
            file.data = file.data.replace(u'\n', '<br/>')
            if file.data == None or file.data == "":
                file.data = content
            show_recommend = True
            show_pagination = False

        fpath = os.path.join(xconfig.UPLOAD_DIR, file.creator, str(file.parent_id), str(id))

        # 处理相册
        if file.type == "gallery":
            if os.path.exists(fpath):
                filelist = fsutil.list_files(fpath, webpath = True)
            else:
                filelist = []
            file.path = fpath

        if show_recommend and user_name is not None:
            show_groups = False
            # 推荐系统
            ctx = Storage(id=file.id, name = file.name, creator = file.creator, 
                content = file.content,
                parent_id = file.parent_id,
                result = [])
            xmanager.fire("note.recommend", ctx)
            recommended_notes = ctx.result

            next_note = xutils.call("note.find_next_note", file)
            prev_note = xutils.call("note.find_prev_note", file)
        
        xmanager.fire("note.view", file)
        show_aside = True
        if op == "edit":
            show_aside = False

        return xtemplate.render(template_name,
            show_aside    = show_aside,
            html_title    = title,
            file          = file, 
            path          = fpath,
            filelist      = filelist,
            note_id       = id,
            op            = op,
            show_mdate    = show_mdate,
            show_add_file = show_add_file,
            show_menu     = show_menu,
            show_pagination = show_pagination,
            can_edit = can_edit,
            pathlist = pathlist,
            page_max = math.ceil(amount/pagesize),
            page     = page,
            page_url = "/note/view?id=%s&orderby=%s&page=" % (id, orderby),
            files    = files, 
            recent_created    = recent_created,
            show_groups       = show_groups,
            groups            = groups,
            prev_note         = prev_note,
            next_note         = next_note,
            recommended_notes = recommended_notes)
示例#13
0
文件: view.py 项目: zenistzw/xnote
    def GET(self, op):
        id = xutils.get_argument("id", "")
        name = xutils.get_argument("name", "")
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        show_menu = xutils.get_argument("show_menu", "true") != "false"
        db = xtables.get_file_table()
        user_name = xauth.get_current_name()
        show_add_file = False
        title = None
        show_pagination = True
        show_search_div = False

        if id == "" and name == "":
            raise HTTPError(504)
        if id != "":
            id = int(id)
            file = dao.get_by_id(id, db=db)
        elif name is not None:
            file = dao.get_by_name(name, db=db)
        if file is None:
            raise web.notfound()
        if file.is_deleted == 1:
            raise web.seeother("/")

        if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator:
            raise web.seeother("/unauthorized")
        pathlist = dao.get_pathlist(db, file)
        can_edit = (file.creator == user_name) or (user_name == "admin")
        role = xauth.get_current_role()

        # 定义一些变量
        show_groups = False
        show_mdate = False
        files = []
        recent_created = []
        groups = []
        amount = 0
        show_recommend = False
        template_name = "note/view.html"
        next_note = None
        prev_note = None
        xconfig.note_history.put(
            dict(user=user_name, link="/note/view?id=%s" % id, name=file.name))
        recommended_notes = []

        title = file.name
        if file.type == "group":
            where_sql = "parent_id=$parent_id AND is_deleted=0 AND (creator=$creator OR is_public=1)"
            if xauth.is_admin():
                where_sql = "parent_id=$parent_id AND is_deleted=0"
            amount = db.count(where=where_sql,
                              vars=dict(parent_id=file.id, creator=user_name))
            files = db.select(where=where_sql,
                              vars=dict(parent_id=file.id,
                                        is_deleted=0,
                                        creator=user_name),
                              order="name",
                              limit=pagesize,
                              offset=(page - 1) * pagesize)
            content = file.content
            show_search_div = True
            show_add_file = True
            show_mdate = True
            # recent_created  = xutils.call("note.list_recent_created", file.id, 10)
        elif file.type == "md" or file.type == "text":
            content = file.content
            if op == "edit":
                template_name = "note/markdown_edit.html"
            show_recommend = True
            show_pagination = False
        else:
            content = file.content
            content = content.replace(u'\xad', '\n')
            content = content.replace(u'\n', '<br/>')
            file.data = file.data.replace(u"\xad", "\n")
            file.data = file.data.replace(u'\n', '<br/>')
            if file.data == None or file.data == "":
                file.data = content
            show_recommend = True
            show_pagination = False

        if show_recommend:
            show_groups = False
            # 推荐系统
            ctx = Storage(id=file.id,
                          name=file.name,
                          creator=file.creator,
                          content=file.content,
                          parent_id=file.parent_id,
                          result=[])
            xmanager.fire("note.recommend", ctx)
            recommended_notes = ctx.result

            next_note = xutils.call("note.find_next_note", file)
            prev_note = xutils.call("note.find_prev_note", file)

        xmanager.fire("note.view", file)
        show_aside = True
        if op == "edit":
            show_aside = False
        return xtemplate.render(template_name,
                                show_aside=show_aside,
                                html_title=title,
                                file=file,
                                note_id=id,
                                op=op,
                                show_mdate=show_mdate,
                                show_add_file=show_add_file,
                                show_menu=show_menu,
                                show_pagination=show_pagination,
                                can_edit=can_edit,
                                pathlist=pathlist,
                                page_max=math.ceil(amount / pagesize),
                                page=page,
                                page_url="/note/view?id=%s&page=" % id,
                                files=files,
                                recent_created=recent_created,
                                show_groups=show_groups,
                                groups=groups,
                                prev_note=prev_note,
                                next_note=next_note,
                                recommended_notes=recommended_notes)