Пример #1
0
def archive():
    """
    归档页面获取数据
    :return:
    """
    articles = Article.get_by_archive()
    if articles:
        publish_years = Article.get_archive_year()
        archive_list = []
        for year in publish_years:
            article_list = []
            for article in articles:
                if year[0] == article[1]:
                    article_list.append({
                        "id":
                        article[0].id,
                        "publish_time":
                        article[0].date_publish.strftime('%b %d,%Y'),
                        "title":
                        article[0].title
                    })
            archive_list.append({
                'publish_date': year[0],
                'articles': article_list
            })
        app.logger.info("request result: {}".format(article_list))
        return jsonify(
            response.return_message(archive_list, Message.SUCCESS.value,
                                    Code.SUCCESS.value))
    return jsonify(
        response.return_message(None, Message.BAD_REQUEST.value,
                                Code.BAD_REQUEST.value))
Пример #2
0
def detail_article(message):
    """
    文章详细页面数据
    :param message:
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    article_id = message['data']['article_id']
    app.logger.info("request params - article_id: {}".format(article_id))
    article = Article.get_by_id(article_id)
    if article:
        tags = []
        for tag in article.tags.all():
            sin_tag = {"id": tag.id, "tag": tag.tag}
            tags.append(sin_tag)
        previous = Article.get_previous(article.date_publish)
        next = Article.get_next(article.date_publish)
        result = {
            "id":
            article.id,
            "title":
            article.title,
            "desc":
            article.desc,
            "content":
            article.content,
            "publish_time":
            'Posted by yinan on ' + article.date_publish.strftime('%b %d,%Y'),
            "back_url":
            article.back_img,
            "tags":
            tags,
            "previous":
            previous[0] if previous else None,
            "next":
            next[0] if next else None
        }
        Article.update_click_count(article_id)
        app.logger.info("request result: {}".format(result))
        return jsonify(
            response.return_message(data={
                "article": result,
            },
                                    msg=Message.SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.BAD_REQUEST.value,
                                code=Code.BAD_REQUEST.value))
Пример #3
0
def get_editor_article(message):
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    params = request.values.to_dict()
    start_time = params['beginTime']
    end_time = params['endTime']
    page_no = params['pageNo']
    result = Article.get_all_by_date(start_time, end_time, page_no)
    articles = []
    for item in result.items:
        tags = [tag.tag for tag in item.tags.all()]
        article = {
            "id": item.id,
            "title": item.title,
            "tags": tags,
            "datetime": item.date_publish.strftime("%Y-%m-%d")
        }
        articles.append(article)
    return jsonify(
        response.return_message(data={
            "articles":
            articles,
            "token":
            Verificate.encode_auth_token(
                message['data']['id'], message['data']['last_login']).decode(),
            "total":
            result.total
        },
                                msg=Message.SUCCESS.value,
                                code=Code.SUCCESS.value))
Пример #4
0
def get_publish(message):
    """
    查询文章
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    article_id = request.values.get("article_id")
    article = Article.get_by_id(article_id)
    if article:
        tags = [tag.tag for tag in article.tags.all()]
        result = {
            "id": article.id,
            "title": article.title,
            "tags": tags,
            "desc": article.desc,
            "content": article.content
        }
        return jsonify(
            response.return_message(data={
                "article":
                result,
                "token":
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.BAD_REQUEST.value,
                                code=Code.BAD_REQUEST.value))
Пример #5
0
 def get_article(id):
     cursor = DBHelper.mysql.get_db().cursor()
     sql = 'select * from article where id=%s'
     cursor.execute(sql, id)
     article = cursor.fetchone()
     article = Article(article)
     return article
Пример #6
0
 def get_articles_by_username(username, index):
     cursor = DBHelper.mysql.get_db().cursor()
     sql = 'select * from article where username=%s order by article.create_at desc limit %s, %s'
     cursor.execute(sql, (username, index, 20))
     articleTuples = cursor.fetchall()
     articles = []
     for article in articleTuples:
         articles.append(Article(article))
     if len(articles) == 0:
         articles = None
     return articles
Пример #7
0
 def get_articles_by_comments(username, index):
     cursor = DBHelper.mysql.get_db().cursor()
     sql = 'select * from article where id in (select articleId from comment where username=%s) limit %s, %s'
     cursor.execute(sql, (username, index, 20))
     articleTuples = cursor.fetchall()
     articles = []
     for article in articleTuples:
         articles.append(Article(article))
     if len(articles) == 0:
         articles = None
     return articles
Пример #8
0
def put_publish(message):
    """
    修改文章
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    results = request.values.to_dict()
    if Article.get_by_id(results['article_id']) is None:
        return jsonify(
            response.return_message(data={
                "token":
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.ARTICLE_NOT_EXISTS.value,
                                    code=Code.NOT_FOUND.value))
    article = Article(results['title'], results['desc'], results['content'],
                      datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                      results['back_img'])
    article.id = results['article_id']
    # 将前台传来的字符串,转换成列表,再转换成元组,然后通过标签查询id
    tag_ids = Tag.get_id_by_tag(tuple(eval(results['tags'])))

    if Article.update(article, tag_ids) is None:
        return jsonify(
            response.return_message(data={
                "token":
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.BAD_REQUEST.value,
                                code=Code.BAD_REQUEST.value))
Пример #9
0
def index(message):
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    page_num = message['data']['page']
    app.logger.info("request params - page: {}".format(page_num))
    articles = Article.get_article_by_pageno(page_num)
    if articles:
        article_list = []
        for item in articles.items:
            article = {
                "id":
                item.id,
                "title":
                item.title,
                "desc":
                item.desc,
                "content":
                item.content,
                "publish_time":
                'Posted by yinan on ' + item.date_publish.strftime('%b %d,%Y'),
            }
            article_list.append(article)
        top_articles = []
        for item in Article.get_top5():
            top_articles.append({'id': item.id, 'title': item.title})
        app.logger.info(
            "request result - total: {}, articles: {}, top_articles: {}".
            format(articles.total, article_list, top_articles))
        return jsonify(
            response.return_message(
                {
                    "total": articles.total,
                    "articles": article_list,
                    'top_articles': top_articles
                }, Message.SUCCESS.value, Code.SUCCESS.value))
    return jsonify(
        response.return_message(None, Message.BAD_REQUEST.value,
                                Code.BAD_REQUEST.value))
Пример #10
0
def search(message):
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    input_search = message['data']['search_params']
    app.logger.info("request params - search_params: {}".format(input_search))
    articles = Article.get_by_search(input_search)
    if articles:
        article_list = []
        for item in articles:
            article = {
                "id": item.id,
                "title": item.title,
                "content": item.content
            }
            article_list.append(article)
        app.logger.info("request result: {}".format(article_list))
        return jsonify(
            response.return_message({"articles": article_list},
                                    Message.SUCCESS.value, Code.SUCCESS.value))
    return jsonify(
        response.return_message(None, Message.BAD_REQUEST.value,
                                Code.BAD_REQUEST.value))
Пример #11
0
def delete_publish(message):
    """
    删除文章
    :return:
    """
    if message['code'] != Code.SUCCESS.value:
        return jsonify(message)
    article_id = request.values.get('article_id')
    result = Article.delete(article_id)
    if result is None:
        return jsonify(
            response.return_message(data={
                'token':
                Verificate.encode_auth_token(
                    message['data']['id'],
                    message['data']['last_login']).decode()
            },
                                    msg=Message.DELETE_SUCCESS.value,
                                    code=Code.SUCCESS.value))
    return jsonify(
        response.return_message(data=None,
                                msg=Message.DELETE_FAILED.value,
                                code=Code.BAD_REQUEST.value))