Example #1
0
    def POST(self):
        name = xutils.get_argument("username", "")
        pswd = xutils.get_argument("password", "")
        target = xutils.get_argument("target")
        users = xauth.get_users()
        error = ""
        count = cacheutil.get("login.fail.count#%s" % name, 0)
        if count >= RETRY_LIMIT:
            error = "重试次数过多"
        elif name in users:
            user = users[name]
            if pswd == user["password"]:
                save_login_info(name, "success")
                xauth.write_cookie(name)
                xauth.update_user(name,
                                  dict(login_time=xutils.format_datetime()))
                if target is None:
                    raise web.seeother("/")
                raise web.seeother(target)
            else:
                error = "用户名或密码错误"
                save_login_info(name, pswd)
                cacheutil.set("login.fail.count#%s" % name, count + 1, 60)
        else:
            error = "用户名或密码错误"
            save_login_info(name, pswd)
            # 用户名异常的不做限制,防止缓存被刷爆
            # cacheutil.set("login.fail.count#%s" % name, count+1, 60)

        return xtemplate.render("login.html",
                                username=name,
                                password=pswd,
                                error=error)
Example #2
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
Example #3
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
Example #4
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
Example #5
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
Example #6
0
def log_search_history(user, key):
    cache_key = "%s@search_history" % user
    history = cacheutil.get(cache_key)
    if isinstance(history, list):
        while key in history:
            history.remove(key)
        history.append(key)
    else:
        history = [key]
    if len(history) > xconfig.SEARCH_HISTORY_MAX_SIZE:
        history = history[-xconfig.SEARCH_HISTORY_MAX_SIZE:]
    cacheutil.set(cache_key, history)
Example #7
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
Example #8
0
def count_user_note(creator):
    t = Timer()
    t.start()
    count_key = "[%s]note.count" % creator
    count = cacheutil.get(count_key)
    if count is None:
        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()))
        cacheutil.set(count_key, count, expire=600)
    t.stop()
    xutils.trace("NoteDao.CountRecentEdit", "", t.cost_millis())
    return count
Example #9
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=")
Example #10
0
def list_group(current_name=None):
    if current_name is None:
        current_name = str(xauth.get_current_name())
    t1 = time.time()
    cache_key = "[%s]note.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=600)
    t2 = time.time()
    xutils.trace("NoteDao.ListGroup", "", int((t2 - t1) * 1000))
    return value
Example #11
0
 def GET(self):
     key = xutils.get_argument("key", "")
     return dict(code="success", data=cacheutil.get(key))
Example #12
0
 def test_cache_set_delete(self):
     cacheutil.set("name", 123)
     self.assertEqual(123, cacheutil.get("name"))
     cacheutil.delete("name")
     self.assertEqual(None, cacheutil.get("name"))