Esempio n. 1
0
 def send(self, result):
     if type(result) == dict:
         return self.write(dumps(result))
     return self.write(result)
Esempio n. 2
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"}
Esempio n. 3
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"}
Esempio n. 4
0
 def render(self, name, value, attrs=None):
     if not isinstance(value, basestring):
         value = dumps(value, indent=2)
     return super(JSONWidget, self).render(name, value, attrs)
Esempio n. 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"}
Esempio n. 6
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"}