def api_tag_articles_tag_id(tag_id): """ Get the article list with the tag of tag_id. Method: GET :return: Error: { "ok": False } Success: { "ok": True, "articles": [{ "id": <article.id>, "title": <article.title>, "date_time": <aritcle.date_time>, }] } """ tag = Tag.query.filter_by(id_=tag_id).first() if tag is None: return not_ok() print(tag.articles) return ok(articles=[{ "id": article.id_, "title": article.title_, "date_time": "%04d-%02d-%02d" % (article.date_time_.year, article.date_time_.month, article.date_time_.day) } for article in tag.articles])
def api_tag_post(): """ Add the tag with name of tag_name. Method: POST :return: Error: { "ok": False } Success: { "ok": True "id": <new_tag.id> "name": <new_tag.name> } """ data = request.get_json() try: name = data["name"] except KeyError: return not_ok() new_tag = Tag(name) db.session.add(new_tag) try: db.session.commit() except IntegrityError: db.session.rollback() return not_ok() return ok(id=new_tag.id_, name=new_tag.name_)
def api_article_article_id_get(article_id): """ Get article with id of article_id. Method: GET :return: Error: { "ok": False } Success: { "ok": True, "title": <article.title>, "date_time": <article.date>, "html_content": <article.html_content>, "page_id": <article.page_id> } """ article = Article.query.filter_by(id_=article_id).first() if article is None: return not_ok() date_time = article.date_time_ return ok(title=article.title_, date_time="%04d-%02d-%02d" % (date_time.year, date_time.month, date_time.day), html_content=article.html_content_, page_id=article.page_id_)
def api_user(): """ Get all users info. Method: GET :return: If success: { "ok": True "users: [{ "id": <user.id>, "name": <user.name>, "email": <user.email> }] } Else: { "ok": False } """ users = User.query.all() return ok(users=[{ "id": user.id_, "name": user.name_, "email": user.email_ } for user in users])
def api_user_me(): """ Get information of this user. Method: GET :return: If current user is anonymous: { "ok": False } If current user is not anonymous: { "ok": True, "id": <this user's id>, "name": <this user's name>, "email": <this user's email> } """ if current_user.is_anonymous: return not_ok() else: return ok(id=current_user.id_, name=current_user.name_, email=current_user.email_)
def api_user_user_id_get(user_id): """ Get user with id of user_id. Method: GET :return: Error: { "ok": False } Success: { "ok": True, "id": <user.id>, "name": <user.name>, "email": <user.email>, "passwd_hash": <user.passwd_hash> } """ user = User.query.filter_by(id_=user_id).first() if user is None: return not_ok() return ok(id=user.id_, name=user.name_, email=user.email_, passwd_hash=user.passwd_hash_)
def api_tag_tags_article_id(article_id): """ Get the tag list of the article of article_id. Method: GET :return: Error: { "ok": False } Success: { "ok": True, "article_id": <article.id> "article_name": <article.title> "tags": [{ "id": <tag.id> "title": <tag.name> }] } """ article = Article.query.filter_by(id_=article_id).first() if article is None: return not_ok() return ok(article_id=article.id_, article_title=article.title_, tags=[{ "id": tag.id_, "name": tag.name_ } for tag in article.tags])
def api_comment_comment_id_delete(comment_id): comment = Comment.query.filter_by(id_=comment_id).first() if comment is None: return not_ok() comment.is_deleted_ = True try: db.session.commit() except IntegrityError: db.session.rollback() return not_ok() return ok(id=comment.id_, is_deleted=comment.is_deleted_)
def api_user_login(): """ Login the user with information in parameters. Method: POST Parameter: name, passwd :return: If have already logged in: { "ok": False, "msg": "already logged in" } If the parameters is invalid: { "ok": False, "msg": "invalid" } If login successful: { "ok": True, "id": <id of the user> } """ data = request.get_json() if data is None: return not_ok() if not current_user.is_anonymous: return not_ok(msg="already logged in") else: try: name = data["name"] passwd = data["passwd"] except KeyError: return not_ok(msg="invalid") user = User.query.filter_by(name_=name).first() if user is None: return not_ok(msg="invalid") elif user.passwd_hash_ != md5_with_salt(passwd, user.salt_): return not_ok(msg="invalid") else: login_user(user) return ok(id=user.id_)
def api_article_article_id_delete(article_id): """ Delete the article with id of article_id. Method: DELETE :return: Error: { "ok": False } Success: { "ok": True } """ Article.query.filter_by(id_=article_id).delete() db.session.commit() return ok()
def api_user_user_id_delete(user_id): """ Delete the user with id of user_id. Method: DELETE :return: Error: { "ok": False } Success: { "ok": True } """ User.query.filter_by(id_=user_id).delete() db.session.commit() return ok()
def api_article_get(): """ Get all articles info. Method: GET Parameter: :return: Error: { "ok": False } Success: { "ok": True, "articles": [ { "id": <id of article>, "title": <title of article>, "date_time": <date string of article> }, ] } """ title_match = request.args.get("title_match") content_match = request.args.get("content_match") query_args = [] if title_match and len(title_match) > 0: query_args.append(Article.title_.contains(title_match)) if content_match and len(content_match) > 0: query_args.append(Article.markdown_content_.contains(content_match)) if len(query_args) == 0: articles = Article.query.order_by(db.desc(Article.date_time_)).all() else: articles = Article.query.filter(*query_args).order_by(db.desc(Article.date_time_)).all() return ok(articles=[{ "id": article.id_, "title": article.title_, "date_time": "%04d-%02d-%02d" % (article.date_time_.year, article.date_time_.month, article.date_time_.day) } for article in articles])
def api_user_post(): """ Create a new user (Register). Method: POST Parameter: name, email, passwd :return: If the parameters is not valid: { "ok": False, } If success: { "ok": True, "id": <user's id> } """ data = request.get_json() try: name = data["name"] email = data["email"] passwd = data["passwd"] except KeyError: return not_ok() if len(passwd) == 0: return not_ok() if len(name) == 0: return not_ok() if not regex.email.match(email): return not_ok() salt = gen_salt() new_user = User(name, email, md5_with_salt(passwd, salt), salt) db.session.add(new_user) try: db.session.commit() except IntegrityError: db.session.rollback() return not_ok() return ok(id=new_user.id_)
def api_user_logout(): """ Logout current user. Method: GET :return: If success: { "ok": True } Else: { "ok": False } """ if current_user.is_anonymous: return not_ok() else: logout_user() return ok()
def api_tag_tag_id_get(tag_id): """ GET the info of the tag with the id of tag_id. Method: GET :return: Error: { "ok": False } Success: { "ok": True "id": <tag.id> "name": <tag.name> } """ tag = Tag.query.filter_by(id_=tag_id) if tag is None: return not_ok() return ok(id=tag.id_, name=tag.name_)
def api_comment(): author_match = request.args.get("author_match") text_match = request.args.get("text_match") query_args = [] if author_match and len(author_match) > 0: query_args.append(Comment.user.has(User.name_.contains(author_match))) if text_match and len(text_match) > 0: query_args.append(Comment.text_.contains(text_match)) if len(query_args) == 0: comments = Comment.query.all() else: comments = Comment.query.filter(*query_args).all() return ok(comments=[{ "id": comment.id_, "author": { "name": comment.user.name_, "id": comment.user.id_, }, "text": comment.text_, "is_deleted": comment.is_deleted_, "page_id": comment.page_id_, } for comment in comments])
def api_tag_tag_id_delete(tag_id): """ Delete the tag with id of tag_id. Method: DELETE :return: Error: { "ok": False } Success: { "ok": True } """ tag = Tag.query.filter_by(id_=tag_id) if tag is None: return not_ok() tag.delete() db.session.commit() return ok()
def api_comment_comment_id_get(comment_id): """ :param comment_id: :return: { "ok": True, "text": <comment's text>, "author_name": <comment's author's name> } """ comment = Comment.query.filter_by(id_=comment_id).first() if comment is None: return not_ok() if comment.is_deleted_: comment_text = "Deleted Comment" else: comment_text = comment.text_ if comment.user is None: author_name = "Deleted User" else: author_name = comment.user.name_ return ok(text=comment_text, author_name=author_name)
def api_article_article_id_get_markdown_content(article_id): """ Get article's markdown_content with id of article_id. Method: GET :return: Error: { "ok": False } Success: { "ok": True, "tags": <article.tags.name>, "category": <article.category.name>, "title": <article.title>, "date_time": <article.date>, "markdown_content": <article.markdown_content>, "page_id": <article.page_id>, "img_url": <article.img_url> } """ article = Article.query.filter_by(id_=article_id).first() if article is None: return not_ok() date_time = article.date_time_ category = article.category if category is not None: category = category.name_ return ok(title=article.title_, tags=[tag.name_ for tag in article.tags], category=category, date_time="%04d-%02d-%02d" % (date_time.year, date_time.month, date_time.day), markdown_content=article.markdown_content_, img_url=article.img_url_, page_id=article.page_id_)
def api_tag_get(): """ Get an tag list of all the existed tags. Method: GET :return: Error: { "ok": False } Success: { "ok": True, [{ "id": <tag.id> "name": <tag.name> }] } """ tags = Tag.query.all() if tags is None: return not_ok() return ok(tags=[{"id": tag.id_, "name": tag.name_} for tag in tags])
def api_article_post(): """ Create a new article. Method: POST Parameter: title, date, tags, category, markdown_content example: { "title": "Hello World", "date_time": "2017-10-01", "markdown_content": "", "tags": ["tagA"] } :return: Error: { "ok": False } Success: { "ok": True, "id": <id of new article> } """ data = request.get_json() try: date_time = datetime.datetime(*tuple(map(int, regex.date.fullmatch(data["date_time"]).groups()))) except AttributeError: return not_ok() try: title = data["title"] markdown_content = data["markdown_content"] category = data["category"] tags = data["tags"] except KeyError: return not_ok() try: img_url = data["img_url"] if len(img_url) == 0: img_url = config.DEFAULT_BACKGROUND except KeyError: img_url = config.DEFAULT_BACKGROUND html_content = markdown_to_html(markdown_content) new_article = Article(title, date_time, html_content, markdown_content, img_url) new_article.page = Page() for tag in get_tags_from_tag_names(tags): new_article.tags.append(tag) if category is not None: if len(category) > 0: _category = Category.query.filter_by(name_=category).first() if _category is None: _category = Category(category) new_article.category = _category db.session.add(new_article) try: db.session.commit() except IntegrityError: return not_ok() return ok(id=new_article.id_)
def api_article_article_id_patch(article_id): """ Update an article. Method: POST Parameter: title, date_time, markdown_content, tags All parameters are optional. example: { "title": "Hello World", "date_time": "2017-10-01", "markdown_content": "", "tags": ["tagA"], "category": "category" "img_url": } :return: Error: { "ok": False } Success: { "ok": True } """ data = request.get_json() article = Article.query.filter_by(id_=article_id).first() if article is None: return not_ok() try: article.title_ = data["title"] except KeyError: pass try: article.date_time_ = datetime.datetime(*tuple(map(int, regex.date.fullmatch(data["date_time"]).groups()))) except KeyError: pass try: article.markdown_content_ = data["markdown_content"] article.html_content_ = markdown_to_html(data["markdown_content"]) except KeyError: pass try: tags = data["tags"] article.tags[:] = [] for tag in get_tags_from_tag_names(tags): article.tags.append(tag) except KeyError: pass try: category_name = data["category"] if category_name is not None: if len(category_name) > 0: category = Category.query.filter_by(name_=category_name).first() if category is None: category = Category(category_name) article.category = category except KeyError: pass try: article.img_url_ = data["img_url"] except KeyError: pass try: db.session.commit() except IntegrityError: db.session.rollback() return not_ok() return ok()