Example #1
0
def client_login(params):
    code = params.get("code")
    if not code or len(code) != 36:
        return {"error_code": 1, "msg": "params invalid"}
    key = "jump-%s" % code
    obj = misc.misc_get(key)
    if not obj:
        return {"error_code": 1, "msg": "code not exists"}
    dic = utils.loads(obj.value)

    obj.delete_instance()
    if dic['expire_at'] < utils.stamp():
        return {"error_code": 1, "msg": "code expire"}

    user = User.select().where(User.id == dic['user_id']).first()
    res = {"error_code": 0, "msg": "ok"}
    res['session_token'] = generate_token()
    sess = Session()
    sess.user = user
    sess.session_key = res['session_token']
    sess.expire_at = utils.timedelta(utils.now(), days=1)
    res['expire_at'] = 0
    sess.save()
    res['identify'] = user.identify
    return res
Example #2
0
def client_login(params):
    code = params.get("code")
    if not code or len(code) != 36:
        return {"error_code":1, "msg":"params invalid"}
    key = "jump-%s" % code
    obj = misc.misc_get(key)
    if not obj:
        return {"error_code":1, "msg":"code not exists"}
    dic = utils.loads(obj.value)

    obj.delete_instance()
    if dic['expire_at'] < utils.stamp():
        return {"error_code":1, "msg":"code expire"}

    user = User.select().where(User.id==dic['user_id']).first()
    res = {"error_code":0, "msg":"ok"}
    res['session_token'] = generate_token()
    sess = Session()
    sess.user = user
    sess.session_key = res['session_token']
    sess.expire_at = utils.timedelta(utils.now(), days=1)
    res['expire_at'] = 0
    sess.save()
    res['identify'] = user.identify
    return res
Example #3
0
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"}
Example #4
0
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"}
Example #5
0
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"}
Example #6
0
def user_profile(user, params, lang):
    origin_user = user
    uuid = params.get("uuid", "")
    if uuid:
        user = User.select().where(User.uuid == uuid).first()
        if not user:
            return {"error_code": 20002, "msg": "user not exist"}

    profile = user.profile.first()
    languages = []
    ul = UserLanguage.select().where(UserLanguage.user == user)
    for u in ul:
        languages.append(dict(
            language_id=u.id,
            name=u.name,
            level=u.level,
        ))
    out = dict()
    # user self can get private information
    if not uuid:
        out["reg_step"] = user.reg_step
        out["id_number"] = "%s********%s" % (profile.id_number[:6], profile.id_number[14:]) if profile.id_number else ""
        out["alipay"] = profile.alipay
        out["email"] = user.email
        out["phone"] = user.phone

    out["name"] = profile.name
    out["username"] = user.username
    out["avatar"] = widget.avatar(profile.avatar)
    out["completeness"] = profile.completeness
    out["visibility"] = profile.visibility
    out["level"] = profile.level
    out["title"] = profile.title
    out["overview"] = profile.overview
    out["hourly"] = profile.hourly
    out["english"] = profile.english
    out["skills"] = utils.loads(profile.skills) if profile.skills else []
    out["available"] = profile.available
    out["workload"] = profile.workload
    location = {}
    if profile.location_id:
        city = widget.get_location(profile.location_id)
        province = widget.get_location(city.parent_id)

        location["location_id"] = profile.location_id
        location["name"] = utils.lang_map_name(city.name, city.ename, lang)
        location["parent_id"] = province.id
        location["parent_name"] = utils.lang_map_name(province.name, province.ename, lang)
    out["location"] = location
    out["address"] = profile.address
    out["postcode"] = profile.postcode
    out['id'] = user.uuid
    out["languages"] = languages
    out["imid"] = user.id
    if uuid:
        favorites = Favorite.select().where(Favorite.user==origin_user, Favorite.target_id==profile.user_id, Favorite.ftype == 'REQ').first()
        if favorites:
            out["favorite"] = True
        else:
            out["favorite"] = False
    # 开发者的评价数量和平均得分
    score = UserStatistics.select(UserStatistics.eveluate_num, UserStatistics.aver_score).where(UserStatistics.user == user).first()
    out["eveluate_num"] = score.eveluate_num if score else 0
    out["aver_score"] = score.aver_score if score else 0
    return {"error_code": 0, "msg": "ok", "profile": out}
Example #7
0
def profile_create(user, params):
    title = params.get("title", "").strip()
    overview = params.get("intro", "").strip()
    email = params.get("email", "").strip()
    skills = params.get("skills", "").strip()
    english = params.get("english", "").strip()
    other = params.get("other", "").strip()
    workload = params.get("workload", "").strip()

    if not title or not overview or not email or not skills or not english or not workload:
        return {"error_code":20121, "msg":"parameters not enough"}
    if len(title) > 29:
        return {"error_code":20122, "msg":"title too long"}

    if len(overview) > 1024 * 1024 * 4:
        return {"error_code":20123, "msg":"overview too long"}

    if not validate.is_email(email):
        return {"error_code":20124, "msg":"email invalid"}
    if not workload.isdigit():
        return {"error_code":20125, "msg":"workload invalid"}
    workload = int(workload)
    if not 1<= workload <= 3:
        return {"error_code":20125, "msg":"workload must between 1-3"}

    if english not in ("1", "2", "3", "4"):
        return {"error_code":20126, "msg":"english level invalid"}

    s = None
    try:
        s = [x for x in skills.split(",") if x in all_skills]
    except:
        return {"error_code":20127, "msg":"skills invalid"}

    other_lan = None
    if other:
        try:
            other_lan = utils.loads(other)
            for y in other_lan:
                if y not in all_languages or other_lan[y] not in ("1", "2", "3", "4"):
                    other_lan.pop(y)
        except:
            return {"error_code":20128, "msg":"other language invalid"}

    # 添加时不娇艳邮箱是否已经绑定
    #u = User.select().where(User.email == email).first()
    #if u and u.id != user.id:
    #    return {"error_code":20129, "msg":"user is exists"}

    with database.atomic() as txn:
        try:
            user.email = email
            user.reg_step = "profile"
            user.save()

            profile = user.profile.first()
            profile.title = title
            profile.overview = overview
            profile.english = english
            profile.workload = workload
            if s:
                profile.skills = utils.dumps(s)
            profile.save()

            if other_lan:
                for x in other_lan:
                    ul = UserLanguage.select().where(UserLanguage.user==user,UserLanguage.name==x).first()
                    if ul and ul.level != other_lan[x]:
                        ul.level = other_lan[x]
                        ul.save()
                    elif not ul:
                        UserLanguage.create(user=user, name=x, level=other_lan[x])
        except Exception, e:
            txn.rollback()
            return {"error_code":20129, "msg":"update error"}
Example #8
0
 def clean(self, value):
     if not value: return
     try:
         return loads(value)
     except Exception, exc:
         raise forms.ValidationError(u'JSON decode error: %s' % (unicode(exc),))
Example #9
0
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"}
Example #10
0
def user_profile(user, params, lang):
    origin_user = user
    uuid = params.get("uuid", "")
    if uuid:
        user = User.select().where(User.uuid == uuid).first()
        if not user:
            return {"error_code": 20002, "msg": "user not exist"}

    profile = user.profile.first()
    languages = []
    ul = UserLanguage.select().where(UserLanguage.user == user)
    for u in ul:
        languages.append(dict(
            language_id=u.id,
            name=u.name,
            level=u.level,
        ))
    out = dict()
    # user self can get private information
    if not uuid:
        out["reg_step"] = user.reg_step
        out["id_number"] = "%s********%s" % (
            profile.id_number[:6],
            profile.id_number[14:]) if profile.id_number else ""
        out["alipay"] = profile.alipay
        out["email"] = user.email
        out["phone"] = user.phone

    out["name"] = profile.name
    out["username"] = user.username
    out["avatar"] = widget.avatar(profile.avatar)
    out["completeness"] = profile.completeness
    out["visibility"] = profile.visibility
    out["level"] = profile.level
    out["title"] = profile.title
    out["overview"] = profile.overview
    out["hourly"] = profile.hourly
    out["english"] = profile.english
    out["skills"] = utils.loads(profile.skills) if profile.skills else []
    out["available"] = profile.available
    out["workload"] = profile.workload
    location = {}
    if profile.location_id:
        city = widget.get_location(profile.location_id)
        province = widget.get_location(city.parent_id)

        location["location_id"] = profile.location_id
        location["name"] = utils.lang_map_name(city.name, city.ename, lang)
        location["parent_id"] = province.id
        location["parent_name"] = utils.lang_map_name(province.name,
                                                      province.ename, lang)
    out["location"] = location
    out["address"] = profile.address
    out["postcode"] = profile.postcode
    out['id'] = user.uuid
    out["languages"] = languages
    out["imid"] = user.id
    if uuid:
        favorites = Favorite.select().where(
            Favorite.user == origin_user,
            Favorite.target_id == profile.user_id,
            Favorite.ftype == 'REQ').first()
        if favorites:
            out["favorite"] = True
        else:
            out["favorite"] = False
    # 开发者的评价数量和平均得分
    score = UserStatistics.select(
        UserStatistics.eveluate_num,
        UserStatistics.aver_score).where(UserStatistics.user == user).first()
    out["eveluate_num"] = score.eveluate_num if score else 0
    out["aver_score"] = score.aver_score if score else 0
    return {"error_code": 0, "msg": "ok", "profile": out}
Example #11
0
def profile_create(user, params):
    title = params.get("title", "").strip()
    overview = params.get("intro", "").strip()
    email = params.get("email", "").strip()
    skills = params.get("skills", "").strip()
    english = params.get("english", "").strip()
    other = params.get("other", "").strip()
    workload = params.get("workload", "").strip()

    if not title or not overview or not email or not skills or not english or not workload:
        return {"error_code": 20121, "msg": "parameters not enough"}
    if len(title) > 29:
        return {"error_code": 20122, "msg": "title too long"}

    if len(overview) > 1024 * 1024 * 4:
        return {"error_code": 20123, "msg": "overview too long"}

    if not validate.is_email(email):
        return {"error_code": 20124, "msg": "email invalid"}
    if not workload.isdigit():
        return {"error_code": 20125, "msg": "workload invalid"}
    workload = int(workload)
    if not 1 <= workload <= 3:
        return {"error_code": 20125, "msg": "workload must between 1-3"}

    if english not in ("1", "2", "3", "4"):
        return {"error_code": 20126, "msg": "english level invalid"}

    s = None
    try:
        s = [x for x in skills.split(",") if x in all_skills]
    except:
        return {"error_code": 20127, "msg": "skills invalid"}

    other_lan = None
    if other:
        try:
            other_lan = utils.loads(other)
            for y in other_lan:
                if y not in all_languages or other_lan[y] not in ("1", "2",
                                                                  "3", "4"):
                    other_lan.pop(y)
        except:
            return {"error_code": 20128, "msg": "other language invalid"}

    # 添加时不娇艳邮箱是否已经绑定
    #u = User.select().where(User.email == email).first()
    #if u and u.id != user.id:
    #    return {"error_code":20129, "msg":"user is exists"}

    with database.atomic() as txn:
        try:
            user.email = email
            user.reg_step = "profile"
            user.save()

            profile = user.profile.first()
            profile.title = title
            profile.overview = overview
            profile.english = english
            profile.workload = workload
            if s:
                profile.skills = utils.dumps(s)
            profile.save()

            if other_lan:
                for x in other_lan:
                    ul = UserLanguage.select().where(
                        UserLanguage.user == user,
                        UserLanguage.name == x).first()
                    if ul and ul.level != other_lan[x]:
                        ul.level = other_lan[x]
                        ul.save()
                    elif not ul:
                        UserLanguage.create(user=user,
                                            name=x,
                                            level=other_lan[x])
        except Exception, e:
            txn.rollback()
            return {"error_code": 20129, "msg": "update error"}