Beispiel #1
0
    def GET(self):
        parent_id = xutils.get_argument("parent_id", 0)
        user_name = xauth.current_name()

        notes = NOTE_DAO.list_note(user_name, parent_id, 0, 200)
        parent = Storage(url="/note/%s" % parent_id, name="上级目录")
        current = Storage(url="#", name="整理")
        return xtemplate.render("search/search_result.html",
                                files=notes,
                                show_path=True,
                                current=current,
                                parent=parent)
Beispiel #2
0
def add_visit_log(user_name, name, url):
    exist_log = find_visit_log(user_name, url)
    if exist_log != None:
        update_visit_log(exist_log, name)
        return

    log = Storage()
    log.name = name
    log.url = url
    log.time = dateutil.format_datetime()

    dbutil.insert("plugin_visit_log:%s" % user_name, log)
Beispiel #3
0
def convert_to_index(note):
    note_index = Storage(**note)

    del_dict_key(note_index, 'path')
    del_dict_key(note_index, 'url')
    del_dict_key(note_index, 'icon')
    del_dict_key(note_index, 'data')
    del_dict_key(note_index, 'content')
    del_dict_key(note_index, 'show_edit')

    note_index.parent_id = str(note_index.parent_id)

    return note_index
Beispiel #4
0
    def search_files(self, path, key, blacklist_str, filename, **kw):
        ignore_case = self.ignore_case
        recursive = self.recursive
        total_lines = 0
        blacklist = to_list(blacklist_str)

        if key is None or key == "":
            return [], total_lines
        if not os.path.isdir(path):
            raise Exception("%s is not a directory" % path)

        if self.use_regexp:
            pattern = re.compile(key)

        result_list = []
        for root, dirs, files in os.walk(path):
            for fname in files:
                # 匹配文件名,过滤黑名单
                if self.should_skip(root, fname, filename):
                    # print("skip", fname)
                    continue
                try:
                    fpath = os.path.join(root, fname)
                    content = xutils.readfile(fpath)
                except Exception as e:
                    xutils.print_exc()
                    result_list.append(
                        Storage(name=fpath,
                                result=[
                                    LineInfo(-1, "read file fail, e=%s" % e,
                                             None)
                                ]))
                    continue
                # 查找结果
                if self.use_regexp:
                    result = self.search_by_regexp(content, pattern, blacklist,
                                                   ignore_case)
                else:
                    result = code_find(content,
                                       key,
                                       blacklist_str,
                                       ignore_case=ignore_case)
                if key != "" and len(result) == 0:
                    # key do not match
                    continue
                total_lines += len(result)
                result_list.append(Storage(name=fpath, result=result))

            if not recursive:
                break
        return result_list, total_lines
Beispiel #5
0
    def GET(self, orderby="edit", show_notice=False):
        if not xauth.has_login():
            raise web.seeother("/note/public")
        if xutils.sqlite3 is None:
            raise web.seeother("/fs_list")
        days = xutils.get_argument("days", 30, type=int)
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int)
        page = max(1, page)
        offset = max(0, (page - 1) * pagesize)
        limit = pagesize
        time_attr = "ctime"

        creator = xauth.get_current_name()
        if orderby == "viewed":
            html_title = "Recent Viewed"
            files = xutils.call("note.list_recent_viewed", creator, offset,
                                limit)
            time_attr = "atime"
        elif orderby == "created":
            html_title = "Recent Created"
            files = xutils.call("note.list_recent_created", None, offset,
                                limit)
            time_attr = "ctime"
        else:
            html_title = "Recent Updated"
            files = xutils.call("note.list_recent_edit", None, offset, limit)
            time_attr = "mtime"

        groups = xutils.call("note.list_group", creator)
        count = xutils.call("note.count_user_note", creator)

        return xtemplate.render("note/recent.html",
                                html_title=html_title,
                                pathlist=[
                                    Storage(name=T(html_title),
                                            type="group",
                                            url="/note/recent_" + orderby)
                                ],
                                file_type="group",
                                files=files,
                                file=Storage(name=T(html_title), type="group"),
                                groups=groups,
                                show_notice=show_notice,
                                show_mdate=True,
                                show_groups=False,
                                show_aside=True,
                                page=page,
                                time_attr=time_attr,
                                page_max=math.ceil(count / xconfig.PAGE_SIZE),
                                page_url="/note/recent_%s?page=" % orderby)
Beispiel #6
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   = 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)
                file_dict = dict(**file)
                del file_dict["default_value"]
                inserted_id = db.insert(**file_dict)                
                # 更新分组下面页面的数量
                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("/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,
            pathlist = get_pathlist(db, parent_id),
            message = error,
            code = code)
Beispiel #7
0
    def GET(self):
        days = xutils.get_argument("days", 30, type=int)
        page = xutils.get_argument("page", 1, type=int)
        pagesize = xutils.get_argument("pagesize", PAGE_SIZE, type=int)
        page = max(1, page)

        db = xtables.get_file_table()
        t = Timer()
        t.start()
        creator = xauth.get_current_name()
        where = "is_deleted = 0 AND (creator = $creator OR is_public = 1) AND type != 'group'"

        cache_key = "recent_notes#%s#%s" % (creator, page)
        files = cacheutil.get(cache_key)
        if files is None:
            files = list(
                db.select(
                    what="name, id, parent_id, ctime, mtime, type, creator",
                    where=where,
                    vars=dict(creator=creator),
                    order="mtime DESC",
                    offset=(page - 1) * pagesize,
                    limit=pagesize))
            cacheutil.set(cache_key, files, expire=600)
        t.stop()
        xutils.log("list recent edit %s" % t.cost())

        t.start()
        groups = xutils.call("note.list_group")
        t.stop()
        xutils.log("list group %s" % t.cost())

        count = db.count(where, vars=dict(creator=xauth.get_current_name()))
        return xtemplate.render("note/view.html",
                                html_title="最近更新",
                                pathlist=[
                                    Storage(name="最近更新",
                                            type="group",
                                            url="/file/recent_edit")
                                ],
                                file_type="group",
                                files=files,
                                file=Storage(name="最近更新", type="group"),
                                page=page,
                                show_notice=True,
                                page_max=math.ceil(count / PAGE_SIZE),
                                groups=groups,
                                show_mdate=True,
                                show_groups=True,
                                page_url="/file/recent_edit?page=")
Beispiel #8
0
def get_root():
    root = Storage()
    root.name = "根目录"
    root.type = "group"
    root.size = None
    root.id = 0
    root.parent_id = 0
    root.content = ""
    root.priority = 0
    build_note_info(root)
    return root
Beispiel #9
0
    def GET(self):
        shell_list = []
        dirname = "scripts"
        if os.path.exists(dirname):
            for fname in os.listdir(dirname):
                fpath = os.path.join(dirname, fname)
                if os.path.isfile(fpath) and fpath.endswith(".bat"):
                    shell_list.append(fpath)

        # 自定义链接
        customized_items = []
        user_config = get_tools_config(xauth.get_current_name())
        if user_config is not None:
            config_list = xutils.parse_config_text(user_config.value)
            customized_items = map(
                lambda x: Storage(name=x.get("key"), url=x.get("value")),
                config_list)

        return xtemplate.render("system/system.html",
                                show_aside=(xconfig.OPTION_STYLE == "aside"),
                                html_title="系统",
                                Storage=Storage,
                                os=os,
                                user=xauth.get_current_user(),
                                customized_items=customized_items)
Beispiel #10
0
    def GET(self, path_key=None):
        """search files by name and content"""
        load_rules()
        key = xutils.get_argument("key", "")
        title = xutils.get_argument("title", "")
        category = xutils.get_argument("category", "default")
        page = xutils.get_argument("page", 1, type=int)
        user_name = xauth.get_current_name()
        page_url  =  "/search/search?key=%s&category=%s&page="\
            % (key, category)
        pagesize = xconfig.SEARCH_PAGE_SIZE
        offset = (page - 1) * pagesize
        limit = pagesize

        if path_key:
            key = xutils.unquote(path_key)

        if key == "" or key == None:
            raise web.found("/search/history")
        key = key.strip()
        ctx = Storage()
        files = self.do_search(ctx, key, offset, pagesize)
        count = len(files)
        files = files[offset:offset + limit]
        fill_note_info(files)
        return xtemplate.render("search/page/search_result.html",
                                show_aside=False,
                                key=key,
                                html_title="Search",
                                category=category,
                                files=files,
                                title=title,
                                page_max=int(math.ceil(count / pagesize)),
                                page_url=page_url,
                                **ctx)
Beispiel #11
0
    def POST(self):
        id = xutils.get_argument("id")
        content = xutils.get_argument("content")
        status = xutils.get_argument("status")
        location = xutils.get_argument("location", "")
        user_name = xauth.get_current_name()
        db = xtables.get_message_table()
        # 对消息进行语义分析处理,后期优化把所有规则统一管理起来
        ctx = Storage(id=id, content=content, user=user_name, type="")
        for rule in rules:
            rule.match_execute(ctx, content)

        ip = get_remote_ip()

        if id == "" or id is None:
            ctime = xutils.get_argument("date", xutils.format_datetime())
            inserted_id = xutils.call("message.create",
                                      content=content,
                                      user=user_name,
                                      status=get_status_by_code(status),
                                      ip=ip,
                                      mtime=ctime,
                                      ctime=ctime)
            id = inserted_id
            xmanager.fire(
                'message.add',
                dict(id=id, user=user_name, content=content, ctime=ctime))
            return dict(code="success",
                        data=dict(id=inserted_id, content=content,
                                  ctime=ctime))
        else:
            update_message_content(id, user_name, content)
        return dict(code="success", data=dict(id=id))
Beispiel #12
0
    def do_search(self, key, offset, limit):
        global _rules

        category = xutils.get_argument("category", "")
        words = textutil.split_words(key)
        files = []

        start_time = time.time()
        ctx = SearchContext()
        ctx.input_text = key
        ctx.words = words
        ctx.category = category
        ctx.search_message = (category == "message")
        ctx.search_file_full = (category == "content")
        ctx.search_dict = (category == "dict")
        ctx.user_name = xauth.get_current_name()

        if ctx.search_message:
            ctx.search_file = False
            ctx.search_file_full = False
            ctx.search_tool = False
        if ctx.search_dict:
            ctx.search_file = False
        if ctx.search_file_full:
            ctx.search_tool = False
        if ctx.category == "book":
            ctx.search_file = False
            ctx.search_tool = False

        xutils.log("  key=%s" % key)

        xmanager.fire("search.before", ctx)
        xmanager.fire("search", ctx)

        for rule in _rules:
            pattern = rule.pattern
            func = rule.func
            # re.match内部已经实现了缓存
            m = re.match(pattern, key)
            if m:
                try:
                    start_time0 = time.time()
                    results = func(ctx, *m.groups())
                    cost_time0 = time.time() - start_time0
                    xutils.log("  >>> %s - %d ms" %
                               (func.modfunc, cost_time0 * 1000))
                    if results is not None:
                        files += results
                except Exception as e:
                    xutils.print_exc()
        cost_time = (time.time() - start_time) * 1000
        xutils.log("  === total - %d ms ===" % cost_time)
        xconfig.search_history.put(
            Storage(name="#search# %s - %d ms" % (key, cost_time),
                    category=category,
                    user=xauth.get_current_name(),
                    link=web.ctx.fullpath))

        xmanager.fire("search.after", ctx)
        return ctx.tools + files
Beispiel #13
0
def link(name, url, title=""):
    return Storage(name=name,
                   url=url,
                   link=url,
                   title=title,
                   editable=False,
                   atime="")
Beispiel #14
0
    def POST(self):
        id = xutils.get_argument("id")
        content = xutils.get_argument("content")
        tag = xutils.get_argument("tag", DEFAULT_TAG)
        location = xutils.get_argument("location", "")
        user_name = xauth.get_current_name()

        # 对消息进行语义分析处理,后期优化把所有规则统一管理起来
        ctx = Storage(id=id, content=content, user=user_name, type="")
        for rule in rules:
            rule.match_execute(ctx, content)

        ip = get_remote_ip()

        if id == "" or id is None:
            ctime = xutils.get_argument("date", xutils.format_datetime())
            inserted_id = MSG_DAO.create(content=content,
                                         user=user_name,
                                         tag=tag,
                                         ip=ip,
                                         mtime=ctime,
                                         ctime=ctime)
            id = inserted_id
            MSG_DAO.refresh_message_stat(user_name)

            xmanager.fire(
                'message.add',
                dict(id=id, user=user_name, content=content, ctime=ctime))
            return dict(code="success",
                        data=dict(id=inserted_id, content=content,
                                  ctime=ctime))
        else:
            update_message_content(id, user_name, content)
        return dict(code="success", data=dict(id=id))
Beispiel #15
0
    def POST(self):
        id = xutils.get_argument("id")
        content = xutils.get_argument("content")
        user_name = xauth.get_current_name()
        db = xtables.get_message_table()
        # 对消息进行语义分析处理,后期优化把所有规则统一管理起来
        ctx = Storage(id=id, content=content, user=user_name, type="")
        for rule in rules:
            rule.match_execute(ctx, content)

        if id == "" or id is None:
            ctime = xutils.get_argument("date", xutils.format_datetime())
            inserted_id = db.insert(content=content,
                                    user=user_name,
                                    status=0,
                                    mtime=ctime,
                                    ctime=ctime)
            id = inserted_id
            xmanager.fire(
                'message.add',
                dict(id=id, user=user_name, content=content, ctime=ctime))
            return dict(code="success",
                        data=dict(id=inserted_id, content=content,
                                  ctime=ctime))
        else:
            db.update(content=content,
                      mtime=xutils.format_datetime(),
                      where=dict(id=id, user=user_name))
            xmanager.fire("message.update",
                          dict(id=id, user=user_name, content=content))
        return dict(code="success", data=dict(id=id))
Beispiel #16
0
    def GET(self):
        key  = xutils.get_argument("key")
        user = xauth.get_current_name()
        default_value = ""

        if key == "settings":
            default_value = DEFAULT_SETTINGS

        config = Storage(key = key, value = xutils.cache_get("%s@prop_%s" % (user, key), 
            default_value))

        if config is None:
            config = Storage(key=key, value="")
        return xtemplate.render("system/properties.html", 
            show_aside = False,
            config = config)
Beispiel #17
0
def convert_to_path_item(note):
    return Storage(name=note.name,
                   url=note.url,
                   id=note.id,
                   type=note.type,
                   priority=note.priority,
                   is_public=note.is_public)
Beispiel #18
0
    def GET(self, path=""):
        if path == "":
            path = xutils.get_argument("path")
        else:
            path = xutils.unquote(path)

        basename = os.path.basename(path)
        op = xutils.get_argument("op")
        path = xutils.get_real_path(path)
        kw = Storage()

        if os.path.isfile(path):
            check_resource(path)
            type = "file"
            content = xutils.readfile(path)
            _, ext = os.path.splitext(path)
            if ext == ".csv" and not content.startswith("```csv"):
                content = "```csv\n" + content + "\n```"
            children = None
        else:
            # file not exists or not readable
            children = None
            content = "File \"%s\" does not exists" % path
            type = "file"

        handle_layout(kw)
        return render("code/preview.html",
                      html_title=basename,
                      os=os,
                      path=path,
                      content=content,
                      type=type,
                      has_readme=False,
                      **kw)
Beispiel #19
0
def guest_link(name, url, icon="cube"):
    return Storage(name=name,
                   url=url,
                   link=url,
                   user=None,
                   is_guest=True,
                   icon=icon)
Beispiel #20
0
def public_link(name, url, icon="cube"):
    return Storage(name=name,
                   url=url,
                   link=url,
                   user=None,
                   is_public=True,
                   icon=icon)
Beispiel #21
0
    def POST(self):
        dirname = xutils.get_argument("dirname")
        old_name = xutils.get_argument("old_name", "")
        new_name = xutils.get_argument("new_name", "")
        user_name = xauth.current_name()

        if dirname is None or dirname == "":
            return dict(code="fail", message="dirname is blank")
        if old_name is None or old_name == "":
            return dict(code="fail", message="old_name is blank")

        if ".." in new_name:
            return dict(code="fail", message="invalid new name")
        if new_name == "":
            new_name = os.path.basename(old_name)
        if xconfig.USE_URLENCODE:
            old_name = xutils.quote_unicode(old_name)
            new_name = xutils.quote_unicode(new_name)

        old_path = os.path.join(dirname, old_name)
        new_path = os.path.join(dirname, new_name)
        if not xauth.is_admin() and not check_file_auth(old_path, user_name):
            return dict(code="fail", message="unauthorized")
        if not os.path.exists(old_path):
            return dict(code="fail", message="源文件 `%s` 不存在" % old_name)
        if os.path.exists(new_path):
            return dict(code="fail", message="目标文件 `%s` 已存在" % new_name)
        os.rename(old_path, new_path)
        xmanager.fire(
            "fs.rename",
            Storage(user=user_name,
                    path=new_path,
                    new_path=new_path,
                    old_path=old_path))
        return dict(code="success")
Beispiel #22
0
    def GET(self):
        user_name = xauth.current_name()
        menu_list = []

        def filter_link_func(link):
            if link.is_guest:
                return user_name is None
            if link.is_user:
                return user_name != None
            if link.user is None:
                return True
            return link.user == user_name

        for category in xconfig.MENU_LIST:
            children = category.children
            if len(children) == 0:
                continue
            children = list(filter(filter_link_func, children))
            menu_list.append(Storage(name=category.name, children=children))

        return xtemplate.render("system/template/system.html",
                                html_title="系统",
                                Storage=Storage,
                                os=os,
                                user=xauth.get_current_user(),
                                menu_list=menu_list,
                                customized_items=[])
Beispiel #23
0
def migrate_note_tags():
    db = xtables.get_file_tag_table()
    count = 0
    for item in db.select():
        note_id = item.file_id
        creator = item.user
        note_key = build_note_full_key(note_id)
        note = dbutil.get(note_key)
        if note != None:
            tag_set = set()
            if note.tags != None:
                tag_set.update(note.tags)
            tag_set.add(item.name)
            note.tags = list(tag_set)

            # 更新入库
            dbutil.put(note_key, note)
            put_note_tiny(note)
            count += 1

    for item in dbutil.prefix_iter("note_tiny"):
        note_id = str(item.id)
        note_tag_key = "note_tags:%s:%s" % (item.creator, note_id)
        note_tag = dbutil.get(note_tag_key)
        if note_tag is None and item.tags != None:
            dbutil.put(note_tag_key, Storage(note_id=note_id, tags=item.tags))
            count += 1

    return "迁移完成,标签数量: %s" % count
Beispiel #24
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=")
Beispiel #25
0
    def GET(self, path=""):
        template_name = "code/code_edit.html"
        path = xutils.get_argument("path", "")
        key = xutils.get_argument("key", "")
        type = xutils.get_argument("type", "")
        readonly = False
        name, ext = os.path.splitext(path)
        if ext.lower() == ".md":
            template_name = "code/code_editPC.html"

        kw = Storage()

        # 处理嵌入页面
        handle_embed(kw)

        if path == "":
            return xtemplate.render(template_name,
                                    content="",
                                    error="path is empty")
        else:
            error = ""
            warn = ""
            try:
                path = self.resolve_path(path, type)
                max_file_size = xconfig.MAX_TEXT_SIZE
                if xutils.get_file_size(path, format=False) >= max_file_size:
                    warn = "文件过大,只显示部分内容"
                    readonly = True
                content = xutils.readfile(path, limit=max_file_size)
                plugin_name = fsutil.get_relative_path(path,
                                                       xconfig.PLUGINS_DIR)
                # 使用JavaScript来处理搜索关键字高亮问题
                # if key != "":
                #     content = xutils.html_escape(content)
                #     key     = xhtml_escape(key)
                #     content = textutil.replace(content, key, htmlutil.span("?", "search-key"), ignore_case=True, use_template=True)
                return xtemplate.render(template_name,
                                        show_preview=can_preview(path),
                                        readonly=readonly,
                                        error=error,
                                        warn=warn,
                                        pathlist=xutils.splitpath(path),
                                        name=os.path.basename(path),
                                        path=path,
                                        content=content,
                                        plugin_name=plugin_name,
                                        lines=content.count("\n") + 1,
                                        **kw)
            except Exception as e:
                xutils.print_exc()
                error = e
            return xtemplate.render(template_name,
                                    path=path,
                                    name="",
                                    readonly=readonly,
                                    error=error,
                                    lines=0,
                                    content="",
                                    **kw)
Beispiel #26
0
 def GET(self, id):
     note = xutils.call("note.get_by_id", id)
     tags = None
     if note and note.tags != None:
         tags = [Storage(name=name) for name in note.tags]
     if not isinstance(tags, list):
         tags = []
     return dict(code="", message="", data=tags)
Beispiel #27
0
def update_tags(creator, note_id, tags):
    key = "note_tags:%s:%s" % (creator, note_id)
    dbutil.put(key, Storage(note_id=note_id, tags=tags))

    note = get_by_id(note_id)
    if note != None:
        note.tags = tags
        update_index(note)
Beispiel #28
0
 def GET(self, id):
     creator = xauth.current_name()
     tags = xutils.call("note.get_tags", creator, id)
     if tags != None:
         tags = [Storage(name=name) for name in tags]
     if not isinstance(tags, list):
         tags = []
     return dict(code="", message="", data=tags)
Beispiel #29
0
def inner_link(name, url):
    return Storage(name=name,
                   url=url,
                   link=url,
                   title=name,
                   editable=False,
                   category="inner",
                   atime="")
Beispiel #30
0
def set_user_config(key, value):
    if key not in USER_CONFIG_KEY_SET:
        return
    user = xauth.current_user()
    if user.config is None:
        user.config = Storage()
    user.config[key] = value
    xauth.update_user(user["name"], user)