def consumer_update_with_numbers(numbers, **kwargs): """ 更新用户资料 :param numbers: 用户电话号码 :param kwargs: {"numbers": "18688982240", "nickname": "trace deng", "introduce": "ego cogito ergo sum", "avatar": "", "qrcode": "", "email": "*****@*****.**", "country": "RPC", "location": "magic city", "sexy": "male", "age": 18} :return: (20400, "yes")/成功,(>20400, "errmsg")/失败 """ try: # 检查合法账号 if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 20411, "invalid phone number" value = {} # 昵称不能超过16字节,超过要截取前16字节 nickname = kwargs.get("nickname") if nickname: if len(nickname) > 32: g_log.warning("too long nickname %s", nickname) nickname = nickname[0:16] value["nickname"] = nickname # 不合理的性别当作未知处理 sexy = kwargs.get("sexy") if sexy: value["sexy"] = sexy_string_2_number(sexy) age = kwargs.get("age") if age: if age > 500: g_log.warning("too old age %s", age) age = 500 value["age"] = age # 个人介绍不能超过512字节,超过要截取前512字节 introduce = kwargs.get("introduce") if introduce: if len(introduce) > 512: g_log.warning("too long introduce %s", introduce) introduce = introduce[0:512] value["introduce"] = introduce # TODO... 头像、logo、国家、地区检查 avatar = kwargs.get("avatar") if avatar: value["avatar"] = avatar email = kwargs.get("email") if email and email_is_valid(email): value["email"] = email country = kwargs.get("country") if country: value["country"] = country location = kwargs.get("location") if location: value["location"] = location qrcode = kwargs.get("qrcode") if qrcode: value["qrcode"] = qrcode g_log.debug("update consumer material: %s", value) # 存入数据库 collection = get_mongo_collection("consumer") if not collection: g_log.error("get collection consumer failed") return 20412, "get collection consumer failed" consumer = collection.find_one_and_update({"numbers": numbers, "deleted": 0}, {"$set": value}) if not consumer: g_log.error("consumer %s exist", numbers) return 20413, "consumer not exist" return 20400, "yes" except Exception as e: g_log.error("%s", e) return 20415, "exception"
def consumer_create(**kwargs): """ 增加用户资料 :param kwargs: {"numbers": "18688982240", "nickname": "trace deng", "introduce": "ego cogito ergo sum", "avatar": "", "qrcode": "", "email": "*****@*****.**", "country": "RPC", "location": "magic city", "sexy": "female", "age": 18} :return: (20100, "yes")/成功,(>20100, "errmsg")/失败 """ try: # 检查要创建的用户numbers numbers = kwargs.get("numbers", "") if not account_is_valid_consumer(numbers): g_log.warning("invalid customer account %s", numbers) return 20111, "invalid phone number" # 昵称不能超过16字节,超过要截取前16字节 nickname = kwargs.get("nickname", numbers) if len(nickname) > 32: g_log.warning("too long nickname %s", nickname) nickname = nickname[0:16] # 不合理的性别当作未知处理 sexy = sexy_string_2_number(kwargs.get("sexy", "unknow")) age = kwargs.get("age", 0) if age > 500: g_log.warning("too old age %s", age) age = 500 # 个人介绍不能超过512字节,超过要截取前512字节 introduce = kwargs.get("introduce", "") if len(introduce) > 512: g_log.warning("too long introduce %s", introduce) introduce = introduce[0:512] avatar = kwargs.get("avatar", "") email = kwargs.get("email", "") if 0 == email_is_valid(email): email = "" # TODO... logo、国家、地区检查 country = kwargs.get("country", "") location = kwargs.get("location", "") qrcode = kwargs.get("qrcode", "") value = {"numbers": numbers, "nickname": nickname, "avatar": avatar, "email": email, "introduce": introduce, "sexy": sexy, "age": age, "country": country, "location": location, "qrcode": qrcode, "deleted": 0, "create_time": datetime.now()} # 存入数据库 collection = get_mongo_collection("consumer") if not collection: g_log.error("get collection consumer failed") return 20112, "get collection consumer failed" consumer = collection.find_one_and_replace({"numbers": numbers}, value, upsert=True, return_document=ReturnDocument.AFTER) if not consumer: g_log.error("create consumer %s failed", numbers) return 20113, "create consumer failed" return 20100, "yes" except Exception as e: g_log.error("%s", e) return 20114, "exception"