def update_user_profile(id, **profile): """update user profile. Args: id: string of user id profile: a dict of user profile Returns: user: a dict of user profile""" user = apis.User.get_by_id(id) if not user: raise Exception(lazy_gettext(MSG_NO_USER, id=id)) cur_user = apis.User.get_current_user() if cur_user.is_owner(): pass elif user == cur_user: if "role" in profile: raise Exception(lazy_gettext(MSG_UNAUTHORIZED)) else: raise Exception(lazy_gettext(MSG_UNAUTHORIZED)) user.update(**profile) result = { "user": user } return result
def update_user_profile(id, **profile): """update user profile. Args: id: string of user id profile: a dict of user profile Returns: user: a dict of user profile""" user = apis.User.get_by_id(id) if not user: raise Exception(lazy_gettext(MSG_NO_USER, id=id)) cur_user = apis.User.get_current_user() if cur_user.is_owner(): pass elif user == cur_user: if "role" in profile: raise Exception(lazy_gettext(MSG_UNAUTHORIZED)) else: raise Exception(lazy_gettext(MSG_UNAUTHORIZED)) user.update(**profile) result = {"user": user} return result
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_post(id): """get a post. Args: id: post id Returns: post: a dict of post""" key = format_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_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 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 create_user(**settings): """create a new user. Args: settings: a dict of user settings Returns: user: a dict of user""" if "email" not in settings: raise Exception(lazy_gettext("email is required")) if "password" not in settings: raise Exception(lazy_gettext("password is required")) user = apis.User.create_user(**settings) result = {"user": user} return result
def get_category(id=None, url=None): """get category by id or url Args: id: category id url: category url Returns: category: a dict of category """ if id is not None: category = apis.Category.get_by_id(id) elif url is not None: category = apis.Category.get_by_url(url) else: raise Exception(lazy_gettext("pls input id or url")) if not category: raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=id)) return {"category": category}
def create_user(**settings): """create a new user. Args: settings: a dict of user settings Returns: user: a dict of user""" if "email" not in settings: raise Exception(lazy_gettext("email is required")) if "password" not in settings: raise Exception(lazy_gettext("password is required")) user = apis.User.create_user(**settings) result = { "user": user } return result
def delete_user(id): """delete a user. Args: id: string of user id Returns: user: a dict of deleted user""" user = apis.User.get_by_id(id) if not user: raise Exception(lazy_gettext(MSG_NO_USER, id=id)) if user.is_owner(): raise Exception(lazy_gettext("can not delete owner")) result = { "user": user.to_dict(), } user.delete() return result
def get_photo(id): """get photo by id. Args: id: photo id Returns: photo: photo""" photo = apis.Photo.get_by_id(id) if not photo: raise Exception(lazy_gettext(MSG_NO_PHOTO, id=id)) return {"photo": photo}
def get_comment(id): """get comment by id. Args: id: comment id Returns: comment: comment""" comment = apis.Comment.get_by_id(id) if not comment: raise Exception(lazy_gettext(MSG_NO_COMMENT, id=id)) return {"comment": comment}
def get_category(id=None, url=None): """get category by id or url Args: id: category id url: category url Returns: category: a dict of category """ if id is not None: category = apis.Category.get_by_id(id) elif url is not None: category = apis.Category.get_by_url(url) else: raise Exception(lazy_gettext("pls input id or url")) if not category: raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=id)) return { "category": category }
def get_photo(id): """get photo by id. Args: id: photo id Returns: photo: photo""" photo = apis.Photo.get_by_id(id) if not photo: raise Exception(lazy_gettext(MSG_NO_PHOTO, id=id)) return { "photo": photo }
def delete_category(id): """delete a category. Args: id: string of category id Returns: category: category id""" category = apis.Category.get_by_id(id) if not category: raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=id)) if category.url == "": raise Exception(lazy_gettext("can not delete Home category")) _delete_category_posts_cache(category) result = { "category": category.to_dict(), } category.delete() return result
def get_comment(id): """get comment by id. Args: id: comment id Returns: comment: comment""" comment = apis.Comment.get_by_id(id) if not comment: raise Exception(lazy_gettext(MSG_NO_COMMENT, id=id)) return { "comment": comment }
def update_category(id, **settings): """update category settings. Args: id: string of category id settings: a dict of category settings Returns: category: a dict of category""" category = apis.Category.get_by_id(id) if not category: raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=id)) _delete_category_posts_cache(category) category.update(**settings) result = {"category": category} return result
def delete_photo(id): """date a photo. Args: id: photo id Returns: photo: a dict of photo""" photo = apis.Photo.get_by_id(id) if not photo: raise Exception(lazy_gettext(MSG_NO_PHOTO, id=id)) result = { "photo": photo.to_dict(), } photo.delete() return result
def update_photo(id, **settings): """update a photo. Args: id: photo id settings: settings of photo Returns: photo: a dict of photo""" photo = apis.Photo.get_by_id(id) if not photo: raise Exception(lazy_gettext(MSG_NO_PHOTO, id=id)) photo.update(**settings) result = { "photo": photo, } return result
def update_category(id, **settings): """update category settings. Args: id: string of category id settings: a dict of category settings Returns: category: a dict of category""" category = apis.Category.get_by_id(id) if not category: raise Exception(lazy_gettext(MSG_NO_CATEGORY, id=id)) _delete_category_posts_cache(category) category.update(**settings) result = { "category": category } return result
def create_comment(post_id, author, content, parent_id=-1): """create a new comment in a post. Args: post_id: id of post author: author name content: content Returns: comment: comment""" post = apis.Post.get_by_id(post_id) if not post: raise Exception(lazy_gettext(MSG_NO_POST, id=post_id)) comment = apis.Comment.add_comment(author, content, post_id, parent_id) _delete_comments_cache(post_id) result = {"comment": comment} return result
def delete_comment(id): """date a comment. Args: id: comment id Returns: comment: a dict of comment""" comment = apis.Comment.get_by_id(id) if not comment: raise Exception(lazy_gettext(MSG_NO_COMMENT, id=id)) _delete_comments_cache(comment.post_id) comment.delete() result = { "comment": comment, } 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 = 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 update_post(id, **settings): """update a post. Args: id: post id settings: a dict of post settings Returns: post: a dict of post""" post = apis.Post.get_by_id(id) if not post: raise Exception(lazy_gettext(MSG_NO_POST, id=id)) _delete_post_cache(post) _delete_category_posts_cache(post.category) post.update(**settings) _delete_category_posts_cache(post.category) result = {"post": post} return result
def create_comment(post_id, author, content, parent_id=-1): """create a new comment in a post. Args: post_id: id of post author: author name content: content Returns: comment: comment""" post = apis.Post.get_by_id(post_id) if not post: raise Exception(lazy_gettext(MSG_NO_POST, id=post_id)) comment = apis.Comment.add_comment(author, content, post_id, parent_id) _delete_comments_cache(post_id) result = { "comment": comment } return result
def delete_post(id): """delete a post. Args: id: post id Returns: post: a dict of post""" post = apis.Post.get_by_id(id) if not post: raise Exception(lazy_gettext(MSG_NO_POST, id=id)) _delete_post_cache(post) _delete_category_posts_cache(post.category) result = { "post": post.to_dict(), } post.delete() 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
def update_post(id, **settings): """update a post. Args: id: post id settings: a dict of post settings Returns: post: a dict of post""" post = apis.Post.get_by_id(id) if not post: raise Exception(lazy_gettext(MSG_NO_POST, id=id)) _delete_post_cache(post) _delete_category_posts_cache(post.category) post.update(**settings) _delete_category_posts_cache(post.category) result = { "post": post } return result
"user": User.get_current_user(), "settings": get_site_settings(), }) return flask_render_template(template_name_or_list, **context) ############################################ ## Users ############################################ from settings import login_manager, lazy_gettext from flask.ext.login import login_required, logout_user from flask.ext.login import login_user as _login_user login_manager.anonymous_user = Anonymous login_manager.login_view = "login" login_manager.login_message = lazy_gettext("Please login to access this page") @login_manager.user_loader def load_user(user_id): """flask.login user_loader callback""" return User.load_user(user_id) def login_user(email, password, remember=False): """check and login user""" user = User.check_user(email, password) if user and user.is_active(): _login_user(user, remember) return user return False
def _get_method_doc(method_name): method = AJAX_METHODS.get(method_name) if method: return method.func_doc else: return lazy_gettext("unsupported method [ %(name)s]", name=method_name)
context.update({ "user": User.get_current_user(), "settings": get_site_settings(), }) return flask_render_template(template_name_or_list, **context) ############################################ ## Users ############################################ from settings import login_manager, lazy_gettext from flask.ext.login import login_required,logout_user from flask.ext.login import login_user as _login_user login_manager.anonymous_user = Anonymous login_manager.login_view = "login" login_manager.login_message = lazy_gettext("Please login to access this page") @login_manager.user_loader def load_user(user_id): """flask.login user_loader callback""" return User.load_user(user_id) def login_user(email, password, remember=False): """check and login user""" user = User.check_user(email, password) if user and user.is_active(): _login_user(user, remember) return user return False