Exemplo n.º 1
0
def follow_user():
    """
    关注一个用户
    :return:
    """

    ids = json_to_pyseq(request.argget.all("ids", []))

    s, r = arg_verify(reqargs=[("id", ids)], required=True)
    if not s:
        return r

    cnt = 0
    for tid in ids[:]:

        if tid != current_user.str_id and get_one_user(user_id=str(tid)):
            r = mdb_user.db.user_follow.update_one(
                {
                    "user_id": current_user.str_id,
                    "type": "account"
                }, {"$addToSet": {
                    "follow": tid
                }},
                upsert=True)
            if r.modified_count or r.upserted_id:
                cnt += 1
                # 更新粉丝统计
                update_one_user(user_id=str(tid),
                                updata={"$inc": {
                                    "fans_num": 1
                                }})
            delete_user_info_cache(user_id=tid)

    if cnt:
        # 更新关注统计
        update_one_user(user_id=current_user.str_id,
                        updata={"$inc": {
                            "follow_user_num": cnt
                        }})
        delete_user_info_cache(user_id=current_user.str_id)
        data = {
            "msg": gettext("Followed"),
            "msg_type": "s",
            "http_status": 201
        }

    elif len(ids) == 1 and ids[0] == current_user.str_id:
        data = {
            "msg": gettext("You can't follow yourself"),
            "msg_type": "w",
            "http_status": 400
        }
    else:
        data = {
            "msg": gettext("You are already following this user"),
            "msg_type": "w",
            "http_status": 400
        }

    return data
Exemplo n.º 2
0
def unfollow():
    '''
    取消关注
    :return:
    '''

    ids = json_to_pyseq(request.argget.all("ids", []))

    s, r = arg_verify(reqargs=[("id", ids)], required=True)
    if not s:
        return r

    for id in ids[:]:
        if mdb_user.db.user_follow.find_one({"user_id":current_user.str_id, "type": "account", "follow":id}):
            # 更新粉丝统计
            update_one_user(user_id=str(id), updata={"$inc": {"fans_num": -1}})
        else:
            ids.remove(id)
        delete_user_info_cache(user_id=id)

    r = mdb_user.db.user_follow.update_one({"user_id":current_user.str_id, "type": "account"},
                                           {"$pullAll": {"follow": ids}})

    if r.modified_count:
        # 更新关注统计
        update_one_user(user_id=current_user.str_id,
                        updata={"$inc": {"follow_user_num": -len(ids)}})
        delete_user_info_cache(user_id=current_user.str_id)
        data = {"msg": gettext("Unfollow success"), "msg_type": "s", "http_status": 201}
    else:
        delete_user_info_cache(user_id=current_user.str_id)
        data = {"msg": gettext("Unfollow failed"), "msg_type": "w", "http_status": 400}

    return data
Exemplo n.º 3
0
def profile_update():
    """
    用户信息更新
    :return:
    """
    gender = request.argget.all('gender', 'secret')
    birthday = request.argget.all('birthday')
    homepage = request.argget.all('homepage')
    address = json_to_pyseq(request.argget.all('address', {}))
    info = request.argget.all('info')
    if len(birthday) != 8:
        data = {
            'msg':
            gettext("The date of birth requires an 8-digit date,Such as '{}'").
            format(time_to_utcdate(tformat="%Y%m%d")),
            'msg_type':
            "e",
            "custom_status":
            400
        }
        return data
    birthday = int(birthday)
    s, r = arg_verify(reqargs=[(gettext("gender"), gender)],
                      only=["secret", "m", "f"])
    if not s:
        return r
    addr_keys = ['countries', 'provinces', 'city', 'district', 'detailed']
    for k, v in address.items():
        if not (k in addr_keys) or not isinstance(v, str):
            data = {
                'msg':
                gettext(
                    "Address format is not in conformity with the requirements"
                ),
                'msg_type':
                "e",
                "custom_status":
                400
            }
            return data
    if homepage:
        s, r = url_format_ver(homepage)
        if not s:
            return {"msg": r, "msg_type": "w", "custom_status": 403}

    r = content_attack_defense(info)
    if r["security"] < 100:
        data = {
            'msg': gettext("User profile information is illegal"),
            'msg_type': "e",
            "custom_status": 400
        }
        return data
    update_data = {
        'gender': gender,
        'homepage': homepage,
        'introduction': info,
        'birthday': birthday,
        'address': address
    }
    r = update_one_user(user_id=current_user.str_id,
                        updata={"$set": update_data})

    if r.modified_count:
        # 清理user信息数据缓存
        delete_user_info_cache(user_id=current_user.str_id)
        data = {
            'msg': gettext("Update succeed"),
            'msg_type': "s",
            "custom_status": 201
        }
    else:
        data = {
            'msg': gettext("No changes"),
            'msg_type': "w",
            "custom_status": 201
        }
    return data
Exemplo n.º 4
0
def user_basic_edit():
    """
    用户基础设置编辑
    :return:
    """
    username = request.argget.all('username')
    custom_domain = request.argget.all('custom_domain', '')
    editor = request.argget.all('editor')
    # username
    s, r = arg_verify(reqargs=[(gettext("username"), username)], required=True)
    if not s:
        return r
    r, s = short_str_verifi(username, "username")
    if not r:
        data = {'msg': s, 'msg_type': "e", "custom_status": 422}
        return data

    update_data = {}
    # custom_domain
    if mdbs["user"].db.user.find_one({
            "_id": current_user.id,
            "custom_domain": -1
    }) and custom_domain.strip():
        r, s = ver_user_domainhacks(custom_domain)
        if r:
            update_data["custom_domain"] = custom_domain
        else:
            data = {'msg': s, 'msg_type': "e", "custom_status": 422}
            return data

    update_data["username"] = username
    # editor
    if editor and editor in ['rich_text', 'markdown']:
        update_data["editor"] = editor
    else:
        data = {
            'msg': gettext("The editor saves failure"),
            'msg_type': "e",
            "custom_status": 400
        }
        return data

    update_data["update_time"] = time.time()

    # 是否被使用
    if mdbs["user"].db.user.find_one({
            "_id": {
                "$ne": current_user.id
            },
            "username": username
    }):
        data = {
            'msg': gettext("Name has been used"),
            'msg_type': "w",
            "custom_status": 403
        }
    elif "custom_domain" in update_data.keys() \
            and mdbs["user"].db.user.find_one({"_id": {"$ne": current_user.id}, "custom_domain": custom_domain}):
        data = {
            'msg': gettext("Domain has been used"),
            'msg_type': "w",
            "custom_status": 403
        }
    elif "custom_domain" in update_data.keys(
    ) and mdbs["user"].db.user.find_one({
            "_id": current_user.id,
            "custom_domain": {
                "$ne": -1
            }
    }):
        data = {
            'msg': gettext("Personality custom domain cannot be modified"),
            'msg_type': "w",
            "custom_status": 400
        }
    else:
        r = update_one_user(user_id=current_user.str_id,
                            updata={"$set": update_data})
        if not r.modified_count:
            data = {
                'msg': gettext("No changes"),
                'msg_type': "w",
                "custom_status": 201
            }
        else:
            delete_user_info_cache(user_id=current_user.str_id)
            data = {
                'msg': gettext("Update success"),
                'msg_type': "s",
                "custom_status": 201
            }

    return data
Exemplo n.º 5
0
def avatar_upload():
    '''
    头像上传
    :return:
    '''
    result = None
    imgfile_base = request.argget.all("imgfile_base")
    max_size_mb = get_config("account", "USER_AVATAR_MAX_SIZE")
    max_size_b = max_size_mb * 1024 * 1024
    if imgfile_base:
        if len(imgfile_base) > max_size_b:
            data = {
                "msg":
                gettext("Upload avatar image can not exceed {}M".format(
                    max_size_mb)),
                "msg_type":
                "w",
                "http_status":
                413
            }
            return data
        else:
            result = fileup_base_64(uploaded_files=[imgfile_base],
                                    prefix="user_avatar/")
    else:
        file = request.files['upfile']
        if len(file.read()) > max_size_b:
            data = {
                "msg":
                gettext("Upload avatar image can not exceed {}M".format(
                    max_size_mb)),
                "msg_type":
                "w",
                "http_status":
                413
            }
            return data

        if file:
            tailoring = request.argget.all('tailoring')
            if tailoring:
                if not isinstance(tailoring, dict):
                    tailoring = json.loads(tailoring)
                for k in ["width", "height", "x", "y", "rotate"]:
                    tailoring.setdefault(k, 0)
            result = file_up(uploaded_files=[file],
                             prefix="user_avatar/",
                             tailoring=tailoring)

    data = {}
    if result:
        result = result[0]
        user = mdb_user.db.user.find_one({"_id": current_user.id})
        if user:
            if user['avatar_url'] and "key" in user['avatar_url'] \
                    and  result["key"] != user['avatar_url']["key"]:
                # 当使用了不同的名字删除老的头像
                file_del(user['avatar_url'])

            update_data = {"avatar_url": result}
            r = mdb_user.db.user.update_one({"_id": current_user.id},
                                            {"$set": update_data})
            if not r.matched_count:
                data = {
                    'msg': gettext("Save failed"),
                    'msg_type': "w",
                    "http_status": 400
                }
            else:
                if result["type"] == "local":
                    # 如果保存再本地的话, 保存为一定尺寸大小
                    path = "{}{}".format(APPS_PATH, get_file_url(result))
                    imgcp = ImageCompression(path, path)
                    ava_size = get_config("account", "USER_AVATAR_SIZE")
                    imgcp.custom_pixels(ava_size[0], ava_size[1])
                data = {
                    'msg': gettext("Save successfully"),
                    'msg_type': "s",
                    "http_status": 201
                }
    if not data:
        data = {
            'msg': gettext("Upload failed"),
            'msg_type': "w",
            "http_status": 400
        }

    # 清理user信息数据缓存
    delete_user_info_cache(user_id=current_user.str_id)
    return data