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 }
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
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
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()
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
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}
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)}
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}
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 }
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) }
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 }
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()
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
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