예제 #1
0
파일: adm_post.py 프로젝트: txf626/osroom
def adm_post_audit():

    ids = json_to_pyseq(request.argget.all('ids', []))
    score= int(request.argget.all("score", 0))
    for i in range(0, len(ids)):
        ids[i] = ObjectId(ids[i])
    r = mdb_web.db.post.update_many({"_id":{"$in":ids}},
                               {"$set":{"audited":1, "audit_score":score,
                                        "audit_way":"artificial", "audit_user_id":current_user.str_id}})
    if r.modified_count:
        if score >= get_config("content_inspection", "ALLEGED_ILLEGAL_SCORE"):

            # 审核不通过,给用户通知
            posts = mdb_web.db.post.find({"_id": {"$in": ids}},
                                         {"user_id":1, "title":1, "_id":1, "audit_score":1})
            for p in posts:

                insert_user_msg(user_id=p["user_id"], ctype="notice", label="audit_failure",
                                title=gettext("Post allegedly violated"), content={"text": p["title"]},
                                target_id=str(p["_id"]), target_type="post")


        data = {"msg":gettext("Submitted successfully, {}").format(r.modified_count),
                "msg_type":"s", "http_status":201}
    else:
        data = {"msg":gettext("Submitted failed"), "msg_type":"w", "http_status":400}
    return data
예제 #2
0
def comment_issue():

    if not get_config("comment", "OPEN_COMMENT"):
        data = {
            "msg": gettext("Comment feature is not open"),
            "msg_type": "w",
            "custom_status": 401
        }
        return data

    target_id = request.argget.all('target_id')  # 目标ID指的是什么事件的评论
    target_type = request.argget.all('target_type', "post")
    content = request.argget.all('content')
    reply_id = request.argget.all('reply_id')  # 回复哪条评论
    reply_user_id = request.argget.all('reply_user_id')  # 回复的评论的用户ID
    reply_username = request.argget.all('reply_username')  # 回复的评论的用户名

    s, r = arg_verify(reqargs=[(gettext("comment"), content)],
                      min_len=1,
                      max_len=int(get_config("comment", "MAX_LEN")))
    if not s:
        return r
    s, r = arg_verify(reqargs=[("target_id", target_id),
                               ("target_type", target_type)],
                      required=True)
    if not s:
        return r

    if reply_id:
        s, r = arg_verify(reqargs=[("reply_user_id", reply_user_id),
                                   ("reply_username", reply_username)],
                          required=True)
        if not s:
            return r
    """
    查看最后一次评论时间
    """
    tquery = {
        "issue_time": {
            "$gt": time.time() - int(get_config("comment", "INTERVAL"))
        }
    }
    if current_user.is_authenticated:
        user_id = current_user.str_id
        username = current_user.username
        email = None
        tquery["user_id"] = user_id

    elif get_config("comment", "TRAVELER_COMMENT"):
        user_id = None
        username = request.argget.all('username')
        email = request.argget.all('email')
        # 用户名格式验证
        r, s = short_str_verifi(username)
        if not r:
            data = {'msg': s, 'msg_type': "e", "custom_status": 422}
            return data

        # 邮箱格式验证
        r, s = email_format_ver(email)
        if not r:
            data = {'msg': s, 'msg_type': "e", "custom_status": 422}
            return data

        tquery["email"] = email

    else:
        data = {
            "msg":
            gettext(
                "Guest reviews feature is not open, please login account comments"
            ),
            "msg_type":
            "w",
            "custom_status":
            401
        }
        return data

    if mdbs["web"].db.comment.find(tquery).count(True) >= int(
            get_config("comment", "NUM_OF_INTERVAL")):
        # 频繁评论
        data = {
            "msg": gettext("You comment too often and come back later"),
            "msg_type": "e",
            "custom_status": 400
        }
        return data

    target = None
    if target_type == "post":
        target = mdbs["web"].db.post.find_one({
            "_id": ObjectId(target_id),
            "issued": {
                "$in": [1, True]
            }
        })
        if not target:
            data = {
                "msg":
                gettext("Articles do not exist or have not been published"),
                "msg_type": "w",
                "custom_status": 400
            }
            return data

        target_user_id = str(target["user_id"])
        target_brief_info = target["title"]

    if not target:
        data = {
            "msg": gettext("Your comment goal does not exist"),
            "msg_type": "w",
            "custom_status": 400
        }
        return data

    issue_time = time.time()
    # 自动审核内容
    r = content_inspection_text(content)

    audit_score = r["score"]
    audit_label = r["label"]
    if r["label"] == "detection_off" or ("suggestion" in r
                                         and r["suggestion"] == "review"):
        # 未开启审核或无法自动鉴别, 等待人工审核
        audited = 0
        audit_way = "artificial"
    elif r["label"] == "no_plugin":
        # 没有检查插件
        audited = 0
        audit_way = "artificial"

    else:
        audit_label = r["label"]
        audited = 1
        audit_way = "auto"

    comment = {
        "target_id": str(target_id),
        "target_user_id": target_user_id,
        "target_brief_info": target_brief_info,
        "type": target_type,
        "user_id": user_id,
        "username": username,
        "email": email,
        "content": content,
        "issued": 1,
        "audited": audited,
        "audit_score": audit_score,
        "audit_label": audit_label,
        "audit_way": audit_way,
        "audit_user_id": None,
        "issue_time": issue_time,
        "word_num": len(content),
        "is_delete": 0,
        "like_user_id": [],
        "like": 0
    }

    if reply_id:
        comment["reply_id"] = reply_id
        comment["reply_user_id"] = reply_user_id
        comment["reply_username"] = reply_username

    r = mdbs["web"].db.comment.insert_one(comment)

    # 如果已审核, 并且违规分数高于正常
    if audited and audit_score >= get_config("content_inspection",
                                             "ALLEGED_ILLEGAL_SCORE"):
        # 通知评论不通过
        msg_content = {"text": content}
        insert_user_msg(
            user_id=user_id,
            ctype="notice",
            label="audit_failure",
            title=gettext("[Label:{}]Comment on alleged violations").format(
                audit_label),
            content=msg_content,
            target_id=str(r.inserted_id),
            target_type="comment")

    elif audit_score < get_config("content_inspection",
                                  "ALLEGED_ILLEGAL_SCORE"):
        # 更新文章中的评论数目
        if target_type == "post":
            mdbs["web"].db.post.update_one({"_id": ObjectId(target_id)},
                                           {"$inc": {
                                               "comment_num": 1
                                           }})

        if current_user.is_authenticated:
            # 评论正常才通知被评论用户
            user_ids = [target_user_id]
            if reply_id:
                user_ids.append(reply_user_id)
            user_ids = list(set(user_ids))
            if user_id in user_ids:
                user_ids.remove(user_id)

            msg_content = {
                "id": str(r.inserted_id),
                "reply_id": reply_id,
                "reply_user_id": reply_user_id,
                "reply_username": reply_username,
                "user_id": user_id,
                "username": username,
                "text": content
            }
            insert_user_msg(user_id=user_ids,
                            ctype="notice",
                            label="comment",
                            title=target_brief_info,
                            content=msg_content,
                            target_id=target_id,
                            target_type=target_type)

    if current_user.is_authenticated:
        data = {
            "msg": gettext("Successful reviews"),
            "msg_type": "s",
            "custom_status": 201
        }
    else:
        data = {
            "msg": gettext("Success back, waiting for the system audit."),
            "msg_type": "s",
            "custom_status": 201
        }

    return data
예제 #3
0
def post_issue():

    tid = request.argget.all('id')
    title = request.argget.all('title', "").strip()
    content = request.argget.all('content', "")
    content_text = request.argget.all('content_text', "")
    editor = request.argget.all('editor')
    category = request.argget.all('category')
    tags = json_to_pyseq(request.argget.all('tags', []))
    issue_way = request.argget.all('issue_way', 'issue')
    cover_url = request.argget.all('cover_url')

    # 标签处理验证
    tag_max_num = get_config("post", "TAG_MAX_NUM")
    if len(tags) > tag_max_num:
        data = {
            "msg": gettext("Up to {} tags are used").format(tag_max_num),
            "msg_type": "w",
            "custom_status": 403
        }
        return data

    tags = list(set(tags))
    temp_tags = ""
    for tag in tags:
        s, r = arg_verify(reqargs=[(gettext("tag"), tag)],
                          max_len=get_config("post", "TAG_MAX_LEN"))
        if not s:
            return r
        temp_tags = "{} {}".format(tag, temp_tags)

    # 分类验证
    try:
        ObjectId(category)
    except BaseException:
        category = None
    # Title 处理
    s, r = arg_verify(reqargs=[(gettext("title"), title.strip())],
                      max_len=get_config("post", "TITLE_MAX_LEN"),
                      required=True)
    if not s:
        return r
    # content
    s, r = arg_verify(reqargs=[(gettext("content"), content.strip()),
                               ("editor", editor)],
                      required=True)
    if not s:
        return r

    text_l = len(content_text)
    if text_l > get_config("post", "BRIEF_LEN"):
        brief_content = content_text[0:get_config("post", "BRIEF_LEN")]
    else:
        brief_content = content_text
    s, r = arg_verify(reqargs=[(gettext("content"), content_text)],
                      max_len=int(get_config("post", "MAX_LEN")))
    if not s:
        data = r
    else:
        if issue_way == "issue":
            issue_way = 1
        else:
            issue_way = 0
        # 获取已上传的文章图片
        old_imgs = []
        if tid:
            # 文章更新
            post = mdbs["web"].db.post.find_one({
                "_id": ObjectId(tid),
                "user_id": current_user.str_id
            })
            if post["issue_time"]:
                # 有发布时间,则发布时间不改变
                issue_time = post["issue_time"]
            elif issue_way:
                # 第一次发布
                issue_time = time.time()
            else:
                # 不发布
                issue_time = 0

            old_imgs = post["imgs"]

        elif issue_way:
            # 发布时间
            issue_time = time.time()
        else:
            # 不发布就不需要发布时间
            issue_time = 0

        # 获取文章中使用的图片
        # 如果是markdown
        if editor == "markdown":
            srcs = richtext_extract_img(richtext=markdown.markdown(content))
        else:
            srcs = richtext_extract_img(richtext=content)
        imgs = clean_tempfile(user_id=current_user.str_id,
                              type="image",
                              old_file=old_imgs,
                              keey_file=srcs)

        if not cover_url and len(imgs) > 0:
            cover_url = imgs[0]

        if issue_way:
            r = content_inspection_text("{} {} {}".format(
                title, content, temp_tags))
            audit_score = r["score"]
            audit_label = r["label"]
            if r["label"] == "detection_off" or ("suggestion" in r and
                                                 r["suggestion"] == "review"):
                # 未开启审核或无法自动鉴别, 等待人工审核
                audited = 0
                audit_way = "artificial"

            elif r["label"] == "no_plugin":
                # 没有检查插件
                audited = 0
                audit_way = "artificial"

            else:
                audit_label = r["label"]
                audited = 1
                audit_way = "auto"
        else:
            # 草稿
            audit_label = None
            audited = audit_score = 0
            audit_way = "auto"
        content = content_attack_defense(content)["content"]
        brief_content = content_attack_defense(brief_content)["content"]
        post = {
            "title": title.strip(),
            "content": content.strip(),
            "brief_content": brief_content,
            "category": category,
            "tags": tags,
            "issued": issue_way,
            "issue_time": issue_time,
            "update_time": time.time(),
            "audited": audited,
            "audit_score": audit_score,
            "audit_user_id": None,
            "audit_way": audit_way,
            "audit_label": audit_label,
            "word_num": text_l,
            "is_delete": 0,
            "imgs": imgs,
            "cover_url": cover_url
        }

        if tid:
            mdbs["web"].db.post.update_one(
                {
                    "_id": ObjectId(tid),
                    "user_id": current_user.str_id
                }, {"$set": post},
                upsert=True)
        else:
            post["comment_num"] = 0
            post["like"] = 0
            post["like_user_id"] = []
            post["user_id"] = current_user.str_id
            post["editor"] = editor
            r = mdbs["web"].db.post.insert_one(post)
            tid = r.inserted_id

        # 如果已审核, 并且分数高于最高检查违规分, 给用户通知
        if audited and issue_way and audit_score >= get_config(
                "content_inspection", "ALLEGED_ILLEGAL_SCORE"):
            insert_user_msg(
                user_id=post["user_id"],
                ctype="notice",
                label="audit_failure",
                title=gettext("[Label:{}]Post allegedly violated").format(
                    audit_label),
                content={"text": post["brief_content"]},
                target_id=str(tid),
                target_type="post")
        if issue_way:
            data = {
                "msg": gettext("Issue success"),
                "msg_type": "s",
                "custom_status": 201
            }
        else:
            data = {
                "msg": gettext("Save success"),
                "msg_type": "s",
                "custom_status": 201
            }
    return data
예제 #4
0
def send_msg():

    '''
    发送消息
    :return:
    '''

    title = request.argget.all("title")
    content = request.argget.all("content")
    content_html = request.argget.all("content_html")
    send_type = json_to_pyseq(request.argget.all("send_type",[]))
    username = json_to_pyseq(request.argget.all("username", []))

    s, r = arg_verify([(gettext(gettext("title")), title),
                       (gettext("content"), content_html),
                       (gettext("send type"), send_type),
                       (gettext("user name"), username)],
                      required=True)
    if not s:
        return r

    data = {"msg":"", "msg_type":"s"}
    query = {"is_delete": {"$in": [False, 0, ""]}, "active": {"$in": [True, 1]}}
    if len(username) > 1 or username[0].lower() != "all":
        # 不是发给全部用户
        query["username"] = {"$in": username}

    users = list(mdb_user.db.user.find(query, {"_id": 1, "email": 1, "mphone_num":1}))

    # 清理消息中的临时img
    if "email" not in send_type:
        # 删除所有上传的图片
        srcs = []
    else:
        # 保留邮件内容中使用的图片
        srcs = richtext_extract_img(richtext=content_html)
    imgs = clean_tempfile(user_id=current_user.str_id,
                          type="image", old_file=[],
                          keey_file=srcs)
    if imgs:
        # 保存邮件中上传的图片记录, 以便之后删除
        mdb_sys.db.sys_msg_img.insert({"time":time.time(), "imgs":imgs,
                                         "send_user_id":current_user.str_id,
                                         "title":title})
    for send_t in send_type:
        if send_t == "on_site":
            for user in users:
                insert_user_msg(user_id=user["_id"], ctype="notice", label="sys_notice",
                                title=title, content={"text":content}, is_sys_msg=True)

            if users:
                data["msg"] = "{}. {}".format(data["msg"], gettext("Station news success"))
            else:
                data["msg"] = "{}. {}".format(data["msg"], gettext("No relevant user"))
                data["msg_type"] = "w"

        elif send_t == "email":
            to_emails = []
            for user in users:
                to_emails.append(user["email"])
            if to_emails:
                send_email(subject=title,
                           recipients=to_emails,
                           html_msg=content_html)
                data["msg"] = "{}. {}".format(data["msg"], gettext("Mail message is being sent"))
            else:
                data["msg"] = "{}. {}".format(data["msg"], gettext("There is no such email address user"))
                data["msg_type"] = "w"

        elif send_t == "sms":
            # 发送短信
            to_mnumber = []
            for user in users:
                if "mphone_num" in user:
                    to_mnumber.append(user["mphone_num"])

            if to_mnumber:
                send_mobile_msg(to_mnumber, content)

                data["msg"] = "{}. {}".format(data["msg"], gettext("SMS sent"))
            else:
                data["msg"] = "{}. {}".format(data["msg"], gettext("No user mobile phone number was obtained"))
                data["msg_type"] = "w"

    data["msg"]= data["msg"].strip(". ")
    data["http_status"] = 201
    return data
예제 #5
0
def adm_comment_audit():

    ids = json_to_pyseq(request.argget.all('ids', []))
    score = int(request.argget.all("score", 0))

    for i, tid in enumerate(ids):
        ids[i] = ObjectId(tid)

    r = mdbs["web"].db.comment.update_many({"_id": {
        "$in": ids
    }}, {
        "$set": {
            "audited": 1,
            "audit_score": score,
            "audit_way": "artificial",
            "audit_user_id": current_user.str_id
        }
    })
    if r.modified_count:

        if score >= get_config("content_inspection", "ALLEGED_ILLEGAL_SCORE"):

            # 审核不通过,给用户通知
            coms = mdbs["web"].db.comment.find({"_id": {
                "$in": ids
            }}, {
                "user_id": 1,
                "content": 1,
                "_id": 1,
                "audit_score": 1
            })
            for com in coms:
                msg_content = {"text": com["content"]}
                insert_user_msg(user_id=com["user_id"],
                                ctype="notice",
                                label="comment",
                                title=gettext("Comment on alleged violations"),
                                content=msg_content,
                                target_id=str(com["_id"]),
                                target_type="comment")
        else:
            # 审核通过, 给被评论对象通知
            coms = mdbs["web"].db.comment.find({"_id": {"$in": ids}})
            for com in coms:
                msg_content = {
                    "id": str(com["_id"]),
                    "user_id": str(com["user_id"]),
                    "username": com["username"],
                    "text": com["content"]
                }

                user_ids = [com["target_user_id"]]
                if "reply_id" in com:
                    user_ids.append(com["reply_id"])
                    msg_content["reply_id"] = com["reply_id"],
                    msg_content["reply_user_id"] = com["reply_user_id"],
                    msg_content["reply_username"] = com["reply_username"]

                insert_user_msg(user_id=user_ids,
                                ctype="notice",
                                label="comment",
                                title=com["target_brief_info"],
                                content=msg_content,
                                target_id=com["target_id"],
                                target_type=com["type"])

        data = {
            "msg":
            gettext("Submitted successfully, {}").format(r.modified_count),
            "msg_type": "s",
            "custom_status": 201
        }
    else:
        data = {
            "msg": gettext("Submitted failed"),
            "msg_type": "w",
            "custom_status": 400
        }
    return data