def question_create(user, params): question_id = utils.safe_id(params.get("question_id")) answer = params.get("answer") if not question_id or not answer: return {"error_code": 80002, "msg": "no enough parameters"} question = Question.select().where(Question.id == question_id).first() if not question: return {"error_code": 20242, "msg": "protect question not exists"} uq = UserQuestion.select().where(UserQuestion.user == user).first() if uq: return {"error_code": 20243, "msg": "password question already exists"} u = UserQuestion() u.user = user u.question = question u.answer = answer u.save() queue.to_queue({ "type": "user_question_create", "user_id": user.id, "team_id": user.identify[1:] if user.identify[0] != "f" else None }) return {"error_code": 0, "msg": "ok"}
def count_weekstone(body): now = utils.now() start = utils.timedelta(now, days=-7) day_start = utils.datetime_day_min(start) day_end = utils.datetime_day_max(start) weekstones = WeekStone.select().where(WeekStone.start_at.between(day_start, day_end)) for ws in weekstones: # 合同状态和时薪周状态都是进行中的才自动提审 if ws.contract.status != "carry": continue if ws.status != "carry": continue with database.atomic() as txn: try: record = WeekStoneRecord() record.weekstone = ws record.save() ws.status = "carry_pay" ws.contract.stone_status = "carry_pay" ws.contract.save() ws.save() queue.to_queue({"type": "weekstone_freelancer_audit", "weekstone_id": ws.id}) except: txn.rollback()
def profile_resume(user, params): level = params.get('level') if not level: return {"error_code": 80002, "msg": "no enough parameters"} if level not in ("entry", "middle", "expert"): return {"error_code": 20016, "msg": "level invalid"} profile = user.profile.first() if not profile: return {"error_code": 20002, "msg": "user not exists"} user.reg_step = 'resume' user.save() profile.level = level profile.save() queue.to_queue({ "type": "user_completeness", "user_id": user.id, "columns": [ "level", ] }) return {"error_code": 0, "msg": "ok"}
def identification(user, params): name = params.get("name") id_number = params.get("id_number") if not name or not id_number: return {"error_code": 80002, "msg": "no enough parameters"} if not 15 <= len(id_number) <= 18 or not id_number[:-1].isdigit(): return {"error_code": 80002, "msg": "id_number invalid"} profile = user.profile.first() # verify user record into profile table if profile.id_number: return {"error_code": 20262, "msg": "already verify"} tmp = Profile.select().where(Profile.id_number == id_number).first() if tmp: return {"error_code": 20262, "msg": "already verify"} key, value = misc.generate_verify_init_data(user, "id_number") m = misc.misc_get_or_create(key=key, value=value) misc_value = utils.loads(m.value) if misc_value['id_verify_count'] >= 3: return {"error_code": 20263, "msg": "verify too many times"} # verify remote try: is_verify = id_verify_remote(name, id_number) except: return {"error_code": 20264, "msg": "verify failed"} if not is_verify: misc_value['id_verify_count'] += 1 misc_value['last_verify_time'] = utils.now() misc.misc_update(key, misc_value) return {"error_code": 20264, "msg": "verify failed"} else: misc.misc_delete(key) profile.name = name profile.id_number = id_number profile.save() team_id = user.identify[1:] if user.identify[0] != "f" else None queue.to_queue({ "type": "user_identify", "user_id": user.id, "team_id": team_id }) return {"error_code": 0, "msg": "ok"}
def check_offer_expire(body): # offer 2 天没处理就过期,钱退回到账户 now = utils.now() start = utils.timedelta(now, days=-2) # TODO 分页 records = Contract.select().where(Contract.create_at<=start, Contract.status=='paid') for contract in records: with database.atomic() as txn: try: contract.status = "expire" contract.expire_at = now contract.save() margin.refund_amount(contract.team.user, contract.team, contract.job, contract.amount) # send msg to user queue.to_queue({"type": "contract_expire", "contract_id": contract.id}) except: txn.rollback()
def identification(user, params): name = params.get("name") id_number = params.get("id_number") if not name or not id_number: return {"error_code": 80002, "msg": "no enough parameters"} if not 15 <= len(id_number) <= 18 or not id_number[:-1].isdigit(): return {"error_code": 80002, "msg": "id_number invalid"} profile = user.profile.first() # verify user record into profile table if profile.id_number: return {"error_code": 20262, "msg": "already verify"} tmp = Profile.select().where(Profile.id_number == id_number).first() if tmp: return {"error_code": 20262, "msg": "already verify"} key, value = misc.generate_verify_init_data(user, "id_number") m = misc.misc_get_or_create(key=key, value=value) misc_value = utils.loads(m.value) if misc_value['id_verify_count'] >= 3: return {"error_code": 20263, "msg": "verify too many times"} # verify remote try: is_verify = id_verify_remote(name, id_number) except: return {"error_code": 20264, "msg": "verify failed"} if not is_verify: misc_value['id_verify_count'] += 1 misc_value['last_verify_time'] = utils.now() misc.misc_update(key, misc_value) return {"error_code": 20264, "msg": "verify failed"} else: misc.misc_delete(key) profile.name = name profile.id_number = id_number profile.save() team_id = user.identify[1:] if user.identify[0] != "f" else None queue.to_queue({"type": "user_identify", "user_id": user.id, "team_id": team_id}) return {"error_code": 0, "msg": "ok"}
def other_create(user, params): amount = utils.decimal_two(params.get("amount", "")) location = utils.safe_id(params.get("location", "")) address = params.get("address", "") postcode = params.get("postcode", "") if not location or not amount: return {"error_code": 80002, "msg": "not enough parameters"} add = Address.select().where(Address.id == location).first() if not add or add and add.level != 3: return {"error_code": 20167, "msg": "location invalid"} if postcode and len(postcode) > 20 or len(address) > 100: return {"error_code": 20162, "msg": "too long"} if not amount: return {"error_code": 20163, "msg": "hourly invalid"} with database.atomic() as txn: try: user.reg_step = "other" user.status = "active" user.save() # 任务加分字断 columns = ["hourly", "location", "address"] profile = user.profile.first() profile.hourly = amount profile.location = location profile.address = address if postcode: profile.postcode = postcode columns.append("postcode") profile.save() queue.to_queue({ "type": "user_completeness", "user_id": user.id, "columns": columns }) except: return {"error_code": 20164, "msg": "save error"} return {"error_code": 0, "msg": "ok"}
def category_update(user, params): # category example: 1,2,3,4,5 category = params.get('category') try: category_list = set(category.split(",")) except: return {"error_code": 20101, "msg": "category invalid"} if len(category_list) != len(filter(utils.safe_id, category_list)): return {"error_code": 20103, "msg": "category invalid"} if len(category_list) == 0: return {"error_code": 20102, "msg": "category not empty"} cates = Category.select(Category.id) cates = set([str(x.id) for x in cates]) if not category_list <= cates: return {"error_code": 20104, "msg": "category error"} if len(category_list) > 10: return {"error_code": 20105, "msg": "category too large"} exist_list = UserCategory.select( UserCategory.category).where(UserCategory.user == user) exist_list = set([str(x.category_id) for x in exist_list]) if category_list == exist_list: return {"error_code": 0, "msg": "ok"} new_cates = list(category_list.difference(exist_list)) del_cates = list(exist_list.difference(category_list)) for c in new_cates: tmp_cate = UserCategory() tmp_cate.user = user tmp_cate.category = c tmp_cate.save() for d in del_cates: UserCategory.delete().where(UserCategory.user == user, UserCategory.category == d).execute() queue.to_queue({ "type": "user_completeness", "user_id": user.id, "columns": [ "category", ] }) return {"error_code": 0, "msg": "ok"}
def profile_resume(user, params): level = params.get('level') if not level: return {"error_code": 80002, "msg": "no enough parameters"} if level not in ("entry", "middle", "expert"): return {"error_code": 20016, "msg": "level invalid"} profile = user.profile.first() if not profile: return {"error_code":20002, "msg":"user not exists"} user.reg_step = 'resume' user.save() profile.level = level profile.save() queue.to_queue({"type": "user_completeness", "user_id": user.id, "columns": ["level", ]}) return {"error_code": 0, "msg": "ok"}
def other_create(user, params): amount = utils.decimal_two(params.get("amount", "")) location = utils.safe_id(params.get("location", "")) address = params.get("address", "") postcode = params.get("postcode", "") if not location or not amount: return {"error_code": 80002, "msg": "not enough parameters"} add = Address.select().where(Address.id == location).first() if not add or add and add.level != 3: return {"error_code": 20167, "msg": "location invalid"} if postcode and len(postcode) > 20 or len(address) > 100: return {"error_code": 20162, "msg": "too long"} if not amount: return {"error_code": 20163, "msg": "hourly invalid"} with database.atomic() as txn: try: user.reg_step = "other" user.status = "active" user.save() # 任务加分字断 columns = ["hourly", "location", "address"] profile = user.profile.first() profile.hourly = amount profile.location = location profile.address = address if postcode: profile.postcode = postcode columns.append("postcode") profile.save() queue.to_queue({"type": "user_completeness", "user_id": user.id, "columns": columns}) except: return {"error_code":20164, "msg":"save error"} return {"error_code": 0, "msg": "ok"}
def question_create(user, params): question_id = utils.safe_id(params.get("question_id")) answer = params.get("answer") if not question_id or not answer: return {"error_code": 80002, "msg": "no enough parameters"} question = Question.select().where(Question.id == question_id).first() if not question: return {"error_code": 20242, "msg": "protect question not exists"} uq = UserQuestion.select().where(UserQuestion.user == user).first() if uq: return {"error_code": 20243, "msg": "password question already exists"} u = UserQuestion() u.user = user u.question = question u.answer = answer u.save() queue.to_queue({"type":"user_question_create", "user_id":user.id, "team_id": user.identify[1:] if user.identify[0] != "f" else None}) return {"error_code": 0, "msg": "ok"}
def category_update(user, params): # category example: 1,2,3,4,5 category = params.get('category') try: category_list = set(category.split(",")) except: return {"error_code":20101, "msg":"category invalid"} if len(category_list) != len(filter(utils.safe_id, category_list)): return {"error_code":20103, "msg":"category invalid"} if len(category_list) == 0: return {"error_code":20102, "msg":"category not empty"} cates = Category.select(Category.id) cates = set([str(x.id) for x in cates]) if not category_list <= cates: return {"error_code":20104, "msg":"category error"} if len(category_list) > 10: return {"error_code":20105, "msg":"category too large"} exist_list = UserCategory.select(UserCategory.category).where(UserCategory.user == user) exist_list = set([str(x.category_id) for x in exist_list]) if category_list == exist_list: return {"error_code":0, "msg":"ok"} new_cates = list(category_list.difference(exist_list)) del_cates = list(exist_list.difference(category_list)) for c in new_cates: tmp_cate = UserCategory() tmp_cate.user = user tmp_cate.category = c tmp_cate.save() for d in del_cates: UserCategory.delete().where(UserCategory.user == user, UserCategory.category == d).execute() queue.to_queue({"type": "user_completeness", "user_id": user.id, "columns": ["category", ]}) return {"error_code":0, "msg":"ok"}
def user_profile_update(user, params): if len(params) == 0: return {"error_code": 0, "msg": "ok"} email = params.get("email") name = params.get("name") location = utils.safe_id(params.get('location')) address = params.get("address") postcode = params.get("postcode") available = params.get("available") workload = params.get("workload") title = params.get("title") overview = params.get("overview") hourly = params.get("hourly") skills = params.get("skills") english = params.get("english") other = params.get("other", "").strip() level = params.get("level") # 更新分值字断名称 columns = list() profile = user.profile.first() if email is not None: if not validate.is_email(email): return {"error_code": 202710, "msg": "email invalid"} u = User.select().where(User.email == email).first() if u and u.id != user.id: return {"error_code": 202711, "msg": "email is exists"} user.email = email user.save() columns.append("email") if level is not None: if level not in ("entry", "middle", "expert"): return {"error_code": 202712, "msg": "level invalid"} if profile.level != level: profile.level = level columns.append("level") if name is not None: if name.strip() != profile.name: if profile.id_number: return {"error_code": 20271, "msg": "already verify user, not allow modify name"} profile.name = name if location: add = Address.select().where(Address.id == location).first() if not add or add and add.level != 3: return {"error_code": 20272, "msg": "location invalid"} profile.location = location columns.append("location") if address is not None: if len(address) > 100: return {"error_code": 20273, "msg": "address too long"} profile.address = address columns.append("address") if postcode is not None: if len(postcode) > 20: return {"error_code": 20274, "msg": "postcode too long"} profile.postcode = postcode columns.append("postcode") if available is not None: if available == "true": profile.available = True elif available == "false": profile.available = False if workload is not None: if not (workload.isdigit() and 1 <= int(workload) <= 3): return {"error_code": 202713, "msg": "workload invalid"} profile.workload = workload columns.append("workload") if title is not None: if len(title) > 29: return {"error_code": 20275, "msg": "title too long"} profile.title = title columns.append("title") if overview is not None: if len(overview) > 1024 * 1024 * 4: return {"error_code": 20276, "msg": "overview too long"} profile.overview = overview columns.append("overview") if hourly is not None: try: hourly = float(hourly) except: return {"error_code": 20277, "msg": "hourly invalid"} profile.hourly = hourly columns.append("hourly") if english is not None: if english not in ("1", "2", "3", "4"): return {"error_code": 20278, "msg": "english level invalid"} profile.english = english columns.append("english") other_lan = None if other: try: other_lan = utils.loads(other) for y in other_lan: if y not in all_languages or str(other_lan[y]) not in ("1", "2", "3", "4"): other_lan.pop(y) except: return {"error_code":20128, "msg":"other language invalid"} if other_lan != None: _user_lang = UserLanguage.select().where(UserLanguage.user==user) for x in _user_lang: if x.name not in other_lan: x.delete_instance() else: if x.level != other_lan[x.name]: x.level = other_lan[x.name] x.save() other_lan.pop(x.name) else: other_lan.pop(x.name) for y in other_lan: UserLanguage.create(user=user, name=y, level=other_lan[y]) columns.append("other_language") if skills is not None: s = None try: s = [x for x in skills.split(",") if x in all_skills] except: return {"error_code": 20279, "msg": "skills invalid"} if s: profile.skills = utils.dumps(s) columns.append("skills") profile.save() queue.to_queue({"type": "user_completeness", "user_id": user.id, "columns": columns}) return {"error_code": 0, "msg": "ok"}
def register(params, device): rtype = params.get('rtype', None) name = params.get('name', None) phone = params.get('phone', None) password = params.get('password', None) vcode = params.get('vcode', None) uname = params.get('username', '') cname = params.get('cname', '') notice = params.get('notice', "true") if not phone or not password or not vcode or not name or not rtype or not notice or not rtype.isdigit(): return {"error_code":20021, "msg":"parameters required"} rtype = int(rtype) if not validate.is_phone(phone): return {"error_code":20022, "msg":"phone invalid"} # freelancer: 1, client: 2, client_person: 3 if rtype not in [1, 2, 3]: return {"error_code":20022, "msg":"user type invalid"} if rtype == 2 and not cname or len(cname) > 100: return {"error_code":20022, "msg":"user type invalid or cname too long"} if not validate.is_verify_code(vcode): return {"error_code":20024, "msg":"verify code invalid"} if User.select().where(User.phone == phone).first(): return {"error_code":20025, "msg":"user is exists"} if rtype != 1: username = phone else: # freelancer do not use username, just use phone to register # username = uname username = phone if not utils.is_username(username): return {"error_code":20023, "msg":"username invalid"} if User.select().where(User.username == username).first(): return {"error_code":20025, "msg":"user is exists"} res = verify_code(phone=phone, code=vcode) if res['error_code'] != 0: return res user_id = GUID.guid() team_id = GUID.guid() with database.atomic() as txn: try: pwd, salt = generate_password(password.encode('utf-8')) user = User() user.id = user_id user.username = username user.password = pwd user.salt = salt user.phone = phone #user.verifycode = vcode user.save(force_insert=True) profile = Profile() profile.user = user profile.name = name profile.avatar = widget.avatar() if notice == "true": profile.is_notice = True profile.save() party = Party() party.user = user party.vip = False party.connects = 60 party.agency_connects = 60 party.save() margin = Margin() margin.user = user margin.save() if rtype == 1: user.identify = 'f%s' % user.id user.app_identify = 'f%s' % user.id user.status = "unactive" user.to_dev = True user.to_req = False user.save() queue.to_queue({"type":"user_register", "user_id":user.id}) else: team = Team() team.id = team_id team.user = user if rtype == 2: team.name = cname else: team.name = "个人需求方" team.team_type = 'client' team.status = "normal" team.logo = widget.logo() team.save(force_insert=True) team_profile = TeamProfile() team_profile.team = team team_profile.phone = phone team_profile.save() user.identify = 'c%s' % team.id user.app_identify = 'c%s' % team.id user.status = "active" user.to_req = True user.to_dev = False user.save() queue.to_queue({"type":"user_register", "user_id":user.id, "team_id":team.id}) except Exception, e: logger.error(traceback.format_exc()) txn.rollback() return {"error_code":20026, "msg":"signup error"}
def user_profile_update(user, params): if len(params) == 0: return {"error_code": 0, "msg": "ok"} email = params.get("email") name = params.get("name") location = utils.safe_id(params.get('location')) address = params.get("address") postcode = params.get("postcode") available = params.get("available") workload = params.get("workload") title = params.get("title") overview = params.get("overview") hourly = params.get("hourly") skills = params.get("skills") english = params.get("english") other = params.get("other", "").strip() level = params.get("level") # 更新分值字断名称 columns = list() profile = user.profile.first() if email is not None: if not validate.is_email(email): return {"error_code": 202710, "msg": "email invalid"} u = User.select().where(User.email == email).first() if u and u.id != user.id: return {"error_code": 202711, "msg": "email is exists"} user.email = email user.save() columns.append("email") if level is not None: if level not in ("entry", "middle", "expert"): return {"error_code": 202712, "msg": "level invalid"} if profile.level != level: profile.level = level columns.append("level") if name is not None: if name.strip() != profile.name: if profile.id_number: return { "error_code": 20271, "msg": "already verify user, not allow modify name" } profile.name = name if location: add = Address.select().where(Address.id == location).first() if not add or add and add.level != 3: return {"error_code": 20272, "msg": "location invalid"} profile.location = location columns.append("location") if address is not None: if len(address) > 100: return {"error_code": 20273, "msg": "address too long"} profile.address = address columns.append("address") if postcode is not None: if len(postcode) > 20: return {"error_code": 20274, "msg": "postcode too long"} profile.postcode = postcode columns.append("postcode") if available is not None: if available == "true": profile.available = True elif available == "false": profile.available = False if workload is not None: if not (workload.isdigit() and 1 <= int(workload) <= 3): return {"error_code": 202713, "msg": "workload invalid"} profile.workload = workload columns.append("workload") if title is not None: if len(title) > 29: return {"error_code": 20275, "msg": "title too long"} profile.title = title columns.append("title") if overview is not None: if len(overview) > 1024 * 1024 * 4: return {"error_code": 20276, "msg": "overview too long"} profile.overview = overview columns.append("overview") if hourly is not None: try: hourly = float(hourly) except: return {"error_code": 20277, "msg": "hourly invalid"} profile.hourly = hourly columns.append("hourly") if english is not None: if english not in ("1", "2", "3", "4"): return {"error_code": 20278, "msg": "english level invalid"} profile.english = english columns.append("english") other_lan = None if other: try: other_lan = utils.loads(other) for y in other_lan: if y not in all_languages or str( other_lan[y]) not in ("1", "2", "3", "4"): other_lan.pop(y) except: return {"error_code": 20128, "msg": "other language invalid"} if other_lan != None: _user_lang = UserLanguage.select().where(UserLanguage.user == user) for x in _user_lang: if x.name not in other_lan: x.delete_instance() else: if x.level != other_lan[x.name]: x.level = other_lan[x.name] x.save() other_lan.pop(x.name) else: other_lan.pop(x.name) for y in other_lan: UserLanguage.create(user=user, name=y, level=other_lan[y]) columns.append("other_language") if skills is not None: s = None try: s = [x for x in skills.split(",") if x in all_skills] except: return {"error_code": 20279, "msg": "skills invalid"} if s: profile.skills = utils.dumps(s) columns.append("skills") profile.save() queue.to_queue({ "type": "user_completeness", "user_id": user.id, "columns": columns }) return {"error_code": 0, "msg": "ok"}
def register(params, device): rtype = params.get('rtype', None) name = params.get('name', None) phone = params.get('phone', None) password = params.get('password', None) vcode = params.get('vcode', None) uname = params.get('username', '') cname = params.get('cname', '') notice = params.get('notice', "true") if not phone or not password or not vcode or not name or not rtype or not notice or not rtype.isdigit( ): return {"error_code": 20021, "msg": "parameters required"} rtype = int(rtype) if not validate.is_phone(phone): return {"error_code": 20022, "msg": "phone invalid"} # freelancer: 1, client: 2, client_person: 3 if rtype not in [1, 2, 3]: return {"error_code": 20022, "msg": "user type invalid"} if rtype == 2 and not cname or len(cname) > 100: return { "error_code": 20022, "msg": "user type invalid or cname too long" } if not validate.is_verify_code(vcode): return {"error_code": 20024, "msg": "verify code invalid"} if User.select().where(User.phone == phone).first(): return {"error_code": 20025, "msg": "user is exists"} if rtype != 1: username = phone else: # freelancer do not use username, just use phone to register # username = uname username = phone if not utils.is_username(username): return {"error_code": 20023, "msg": "username invalid"} if User.select().where(User.username == username).first(): return {"error_code": 20025, "msg": "user is exists"} res = verify_code(phone=phone, code=vcode) if res['error_code'] != 0: return res user_id = GUID.guid() team_id = GUID.guid() with database.atomic() as txn: try: pwd, salt = generate_password(password.encode('utf-8')) user = User() user.id = user_id user.username = username user.password = pwd user.salt = salt user.phone = phone #user.verifycode = vcode user.save(force_insert=True) profile = Profile() profile.user = user profile.name = name profile.avatar = widget.avatar() if notice == "true": profile.is_notice = True profile.save() party = Party() party.user = user party.vip = False party.connects = 60 party.agency_connects = 60 party.save() margin = Margin() margin.user = user margin.save() if rtype == 1: user.identify = 'f%s' % user.id user.app_identify = 'f%s' % user.id user.status = "unactive" user.to_dev = True user.to_req = False user.save() queue.to_queue({"type": "user_register", "user_id": user.id}) else: team = Team() team.id = team_id team.user = user if rtype == 2: team.name = cname else: team.name = "个人需求方" team.team_type = 'client' team.status = "normal" team.logo = widget.logo() team.save(force_insert=True) team_profile = TeamProfile() team_profile.team = team team_profile.phone = phone team_profile.save() user.identify = 'c%s' % team.id user.app_identify = 'c%s' % team.id user.status = "active" user.to_req = True user.to_dev = False user.save() queue.to_queue({ "type": "user_register", "user_id": user.id, "team_id": team.id }) except Exception, e: logger.error(traceback.format_exc()) txn.rollback() return {"error_code": 20026, "msg": "signup error"}