예제 #1
0
파일: ajax.py 프로젝트: yyy921104/me
def get_post(id):
    """get a post.
    Args:
        id: post id
    Returns:
        post: a dict of post"""

    key = POST_CACHE_KEY % id

    post = memcache.get(key)
    if post is None:
        post = apis.Post.get_by_id(id)
        if post:
            post = post.to_dict()
            memcache.set(key, post)

    if not post:
        raise Exception(lazy_gettext(MSG_NO_POST, id=id))

    if not post["public"]:
        cur_user = apis.User.get_current_user()
        if not cur_user.is_admin():
            raise Exception(lazy_gettext(MSG_UNAUTHORIZED))

    return {
        "post": post
    }
예제 #2
0
파일: ajax.py 프로젝트: errorcode7/me
def get_post(id):
    """get a post.
    Args:
        id: post id
    Returns:
        post: a dict of post"""

    key = POST_CACHE_KEY % id

    post = memcache.get(key)
    if post is None:
        post = apis.Post.get_by_id(id)
        if post:
            post = post.to_dict()
            memcache.set(key, post)

    if not post:
        raise Exception(lazy_gettext(MSG_NO_POST, id=id))

    if not post["public"]:
        cur_user = apis.User.get_current_user()
        if not cur_user.is_admin():
            raise Exception(lazy_gettext(MSG_UNAUTHORIZED))

    return {
        "post": post
    }
예제 #3
0
파일: ajax.py 프로젝트: errorcode7/me
def get_posts_by_tag(name, page=1, per_page=10):
    """get posts by tag.
    Args:
        name: tag name
        page: page number, starts with 1
        per_page: post count per page
    Returns:
        tag: a tag
        pager: a pager
        posts: a list of posts"""

    tag = apis.Tag.get_tag_by_name(name)
    if not tag:
        raise Exception(lazy_gettext(MSG_NO_TAG, name=name))

    key = TAG_POSTS_CACHE_KEY % (tag.name, page, per_page)

    posts = memcache.get(key)
    if posts is None:
        posts = [post.to_dict() for post in tag.get_posts(page, per_page)]
        memcache.set(key, posts, 3600)  # cache 1 hour

    result = {
        "tag": tag.to_dict(),
        "pager": {
            "cur_page": page,
            "per_page": per_page,
            "is_last_page": len(posts) < per_page
        },
        "posts": posts
    }

    return result
예제 #4
0
파일: ajax.py 프로젝트: yyy921104/me
def get_posts_by_tag(name, page=1, per_page=10):
    """get posts by tag.
    Args:
        name: tag name
        page: page number, starts with 1
        per_page: post count per page
    Returns:
        tag: a tag
        pager: a pager
        posts: a list of posts"""

    tag = apis.Tag.get_tag_by_name(name)
    if not tag:
        raise Exception(lazy_gettext(MSG_NO_TAG, name=name))

    key = TAG_POSTS_CACHE_KEY % (tag.name, page, per_page)

    posts = memcache.get(key)
    if posts is None:
        posts = [post.to_dict() for post in tag.get_posts(page, per_page)]
        memcache.set(key, posts, 3600)  # cache 1 hour

    result = {
        "tag": tag.to_dict(),
        "pager": {
            "cur_page": page,
            "per_page": per_page,
            "is_last_page": len(posts) < per_page
        },
        "posts": posts
    }

    return result
예제 #5
0
파일: ajax.py 프로젝트: errorcode7/me
def get_posts_by_category(category="", page=1, per_page=10, group_by="", start_cursor=""):
    """get all posts of a category.
    Args:
        category: category id or url, if "", return all posts
        page: page number starts from 1
        per_page: item count per page
        start_cursor: GAE data store cursor
    Returns:
        pager: a pager
        posts: list of post"""

    try:
        if isinstance(category, int):
            category = get_category(id=category)["category"]
        else:
            category = get_category(url=category)["category"]
    except:
        raise Exception(lazy_gettext(MSG_ERROR_CATEGORY, id=category))

    if not category:
        raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=category))

    cur_user = apis.User.get_current_user()
    get_no_published = cur_user.is_admin()

    result = {
        "pager": {
            "cur_page": page,
            "per_page": per_page,
            "group_by": group_by,
            "start_cursor": start_cursor,
        },
        "category": category,
    }

    if page <= CATEGORY_CACHE_PAGES:  # cache only CATEGORY_CACHE_PAGES pages
        key = CATEGORY_CACHE_KEY % (category.id, page, per_page, get_no_published)
        res = memcache.get(key)

        if res is None:
            posts, end_cursor = category.get_posts(page, per_page, get_no_published, start_cursor)
            posts = [post.to_dict() for post in posts]
            memcache.set(key, (posts, end_cursor))
        else:
            posts, end_cursor = res
    else:
        posts, end_cursor = category.get_posts(page, per_page, get_no_published, start_cursor)

    result["posts"] = posts
    result["pager"]["start_cursor"] = end_cursor
    result["pager"]["is_last_page"] = len(posts) < per_page
    if group_by:
        result["group_posts"] = apis.group_by(posts, group_by)

    return result
예제 #6
0
파일: ajax.py 프로젝트: yyy921104/me
def get_posts_by_category(category="", page=1, per_page=10, group_by="", start_cursor=""):
    """get all posts of a category.
    Args:
        category: category id or url, if "", return all posts
        page: page number starts from 1
        per_page: item count per page
        start_cursor: GAE data store cursor
    Returns:
        pager: a pager
        posts: list of post"""

    try:
        if isinstance(category, int):
            category = get_category(id=category)["category"]
        else:
            category = get_category(url=category)["category"]
    except:
        raise Exception(lazy_gettext(MSG_ERROR_CATEGORY, id=category))

    if not category:
        raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=category))

    cur_user = apis.User.get_current_user()
    get_no_published = cur_user.is_admin()

    result = {
        "pager": {
            "cur_page": page,
            "per_page": per_page,
            "group_by": group_by,
            "start_cursor": start_cursor,
        },
        "category": category,
    }

    if page <= CATEGORY_CACHE_PAGES:  # cache only CATEGORY_CACHE_PAGES pages
        key = CATEGORY_CACHE_KEY % (category.id, page, per_page, get_no_published)
        res = memcache.get(key)

        if res is None:
            posts, end_cursor = category.get_posts(page, per_page, get_no_published, start_cursor)
            posts = [post.to_dict() for post in posts]
            memcache.set(key, (posts, end_cursor))
        else:
            posts, end_cursor = res
    else:
        posts, end_cursor = category.get_posts(page, per_page, get_no_published, start_cursor)

    result["posts"] = posts
    result["pager"]["start_cursor"] = end_cursor
    result["pager"]["is_last_page"] = len(posts) < per_page
    if group_by:
        result["group_posts"] = apis.group_by(posts, group_by)

    return result
예제 #7
0
def email_captcha():
    email = request.args.get('email')
    captcha = list(string.ascii_letters)
    captcha.extend(map(lambda x: str(x), range(0, 10)))
    captcha = ''.join(random.sample(captcha, 6))
    user = g.cms_user
    try:
        send_mail.delay("达尼论坛验证码", [email], captcha, user.username, '更改邮箱')
        memcache.set(email, captcha)
    except:
        return restful.params_error('此邮箱不存在')
    return restful.success()
예제 #8
0
파일: ajax.py 프로젝트: errorcode7/me
def get_sitemap():
    sitemap = memcache.get(SITEMAP_CACHE_KEY)
    if sitemap is None:
        sitemap = []
        for post in apis.Post.latest_posts(count=1000):
            sitemap.append({
                "loc": url_for("post", postid=post.id, _external=True),
                "lastmod": post.updated_date.strftime(JSON_DATETIME_FORMAT[0])
            })

        if sitemap:
            memcache.set(SITEMAP_CACHE_KEY, sitemap, SITEMAP_CACHE_TIME)

    return sitemap
예제 #9
0
파일: ajax.py 프로젝트: yyy921104/me
def get_sitemap():
    sitemap = memcache.get(SITEMAP_CACHE_KEY)
    if sitemap is None:
        sitemap = []
        for post in apis.Post.latest_posts(count=1000):
            sitemap.append({
                "loc": url_for("post", postid=post.id, _external=True),
                "lastmod": post.updated_date.strftime(JSON_DATETIME_FORMAT[0])
            })

        if sitemap:
            memcache.set(SITEMAP_CACHE_KEY, sitemap, SITEMAP_CACHE_TIME)

    return sitemap
예제 #10
0
def get_latest_posts(count=12, order="updated_date desc"):
    """get latest posts.
    Args:
        count: post count
        order: posts order, default is "updated_date desc"
    Returns:
        posts: a list of post"""
    key = format_key(LATEST_POSTS_CACHE_KEY, count, order)
    posts = memcache.get(key)
    if posts is None:
        posts = apis.Post.latest_posts(count, order)
        if posts:
            memcache.set(key, posts, LATEST_POSTS_CACHE_TIME)

    return {"posts": posts}
예제 #11
0
def get_hot_tags(count=12):
    """get hot tags.
    Args:
        count: tags count
    Returns:
        tags: a list of tags"""

    key = format_key(HOT_TAGS_CACHE_KEY, count)

    tags = memcache.get(key)
    if tags is None:
        tags = apis.Tag.hot_tags(count)
        memcache.set(key, tags, 3600 * 24)  # cache 24 hour

    return {"tags": apis.Tag.hot_tags(count)}
예제 #12
0
def get_hot_posts(count=8, order="view_count desc"):
    """get hot posts.
    Args:
        count: post count
        order: hot order, default is "view_count desc"
    Returns:
        posts: a list of post"""

    key = format_key(HOT_POSTS_CACHE_KEY, count, order)
    posts = memcache.get(key)
    if posts is None:
        posts = apis.Post.hot_posts(count, order)
        if posts:
            memcache.set(key, posts, HOT_POSTS_CACHE_TIME)

    return {"posts": posts}
예제 #13
0
파일: ajax.py 프로젝트: errorcode7/me
def get_latest_posts(count=12, order="updated_date desc"):
    """get latest posts.
    Args:
        count: post count
        order: posts order, default is "updated_date desc"
    Returns:
        posts: a list of post"""
    key = LATEST_POSTS_CACHE_KEY % (count, order)
    posts = memcache.get(key)
    if posts is None:
        posts = apis.Post.latest_posts(count, order)
        if posts:
            memcache.set(key, posts, LATEST_POSTS_CACHE_TIME)

    return {
        "posts": posts
    }
예제 #14
0
파일: ajax.py 프로젝트: errorcode7/me
def get_hot_tags(count=12):
    """get hot tags.
    Args:
        count: tags count
    Returns:
        tags: a list of tags"""


    key = HOT_TAGS_CACHE_KEY % count

    tags = memcache.get(key)
    if tags is None:
        tags = apis.Tag.hot_tags(count)
        memcache.set(key, tags, 3600*24)  # cache 24 hour

    return {
        "tags": apis.Tag.hot_tags(count)
    }
예제 #15
0
파일: ajax.py 프로젝트: errorcode7/me
def get_hot_posts(count=8, order="view_count desc"):
    """get hot posts.
    Args:
        count: post count
        order: hot order, default is "view_count desc"
    Returns:
        posts: a list of post"""

    key = HOT_POSTS_CACHE_KEY % (count, order)
    posts = memcache.get(key)
    if posts is None:
        posts = apis.Post.hot_posts(count, order)
        if posts:
            memcache.set(key, posts, HOT_POSTS_CACHE_TIME)

    return {
        "posts": posts
    }
예제 #16
0
def send_sms():
    telephone = request.form.get('telephone')
    token = request.form.get("token")
    authenticate = request.form.get("authenticate")
    check_ret = yprequest.check_ticket(token, authenticate)
    user = FrontUser.query.filter_by(telephone=telephone).first()
    if not user:
        if check_ret.get("code") == 0:
            captcha = ''.join(random.sample(list(string.digits), 6))
            result = send_sms_captcha(telephone, captcha)
            memcache.set(telephone, captcha, timeout=300)
            if result.get('Code') == 'OK':
                return restful.success()
            else:
                return restful.unauth_error()
        else:
            return restful.server_error()
    return restful.params_error()
예제 #17
0
def get_comments_by_post(id):
    """get comments by post.
    Args:
        id: post id
    Returns:
        comments: a list of comments"""
    post = apis.Post.get_by_id(id)
    if not post:
        raise Exception(lazy_gettext(MSG_NO_POST, id=id))

    key = format_key(COMMENT_CACHE_KEY, id)

    comments = memcache.get(key)
    if comments is None:
        comments = [comment.to_dict() for comment in post.Comments]
        memcache.set(key, comments)

    result = {"comments": comments}
    return result
예제 #18
0
파일: ajax.py 프로젝트: errorcode7/me
def get_comments_by_post(id):
    """get comments by post.
    Args:
        id: post id
    Returns:
        comments: a list of comments"""
    post = apis.Post.get_by_id(id)
    if not post:
        raise Exception(lazy_gettext(MSG_NO_POST, id=id))

    key = COMMENT_CACHE_KEY % id

    comments = memcache.get(key)
    if comments is None:
        comments = [comment.to_dict() for comment in post.Comments]
        memcache.set(key, comments)

    result = {
        "comments": comments
    }
    return result