示例#1
0
文件: auth.py 项目: Quizp2p/NoMagic
def update_user(user_id, data):
    #valid name
    user = nomagic._get_entity_by_id(user_id)
    user_json1 = nomagic._pack(user)
    result = {}

    if "password0" in data and "password1" in data and data["password0"] != data["password1"] and data["password1"] != "":
        #normal update password with old password0 and new password1
        if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest():
            user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest()
            result["password_updated"] = True
        del data["password0"]
        del data["password1"]

    elif "password" in data and data["password"] != "":
        #force update password
        user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest()
        result["password_updated"] = True
        del data["password"]

    if user:
        user.update(data)
        user_json2 = nomagic._pack(user)
        if user_json1 != user_json2:
            assert nomagic._node(user_id).execute_rowcount("UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id))
    return result
示例#2
0
def update_user(user_id, data):
    #valid name
    user = nomagic._get_entity_by_id(user_id)
    user_json1 = nomagic._pack(user)
    result = {}

    if "password0" in data and "password1" in data and data[
            "password0"] != data["password1"] and data["password1"] != "":
        #normal update password with old password0 and new password1
        if user["password"] == hashlib.sha1(data["password0"] +
                                            user.get("salt", "")).hexdigest():
            user["password"] = hashlib.sha1(data["password1"] +
                                            user.get("salt", "")).hexdigest()
            result["password_updated"] = True
        del data["password0"]
        del data["password1"]

    elif "password" in data and data["password"] != "":
        #force update password
        user["password"] = hashlib.sha1(data["password"] +
                                        user.get("salt", "")).hexdigest()
        result["password_updated"] = True
        del data["password"]

    if user:
        user.update(data)
        user_json2 = nomagic._pack(user)
        if user_json1 != user_json2:
            assert nomagic._node(user_id).execute_rowcount(
                "UPDATE entities SET body = %s WHERE id = %s",
                nomagic._pack(user), nomagic._key(user_id))
            BIG_CACHE.unset(user_id)
    return result
示例#3
0
def update_user(user_id, data):
    #valid name
    user = nomagic._get_entity_by_id(user_id)
    user_json1 = nomagic._pack(user)
    if user:
        user.update(data)
        user_json2 = nomagic._pack(user)
        if user_json1 != user_json2:
            assert ring[nomagic._number(user_id)].execute_rowcount("UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id))
示例#4
0
def update_user(user_id, data):
    #valid name
    user = nomagic._get_entity_by_id(user_id)
    user_json1 = nomagic._pack(user)
    if user:
        user.update(data)
        user_json2 = nomagic._pack(user)
        if user_json1 != user_json2:
            assert ring[nomagic._number(user_id)].execute_rowcount(
                "UPDATE entities SET body = %s WHERE id = %s",
                nomagic._pack(user), nomagic._key(user_id))
示例#5
0
def new_status(user_id, data):
    now = datetime.datetime.now()
    data["type"] = "status"
    data["user_id"] = user_id
    data["datetime"] = now.isoformat()
    data["likes"] = []
    data["comment_ids"] = []
    assert data.get("title")

    new_id = nomagic._new_key()
    assert nomagic._node(new_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(data))

    user = nomagic._get_entity_by_id(user_id)
    activity = user.get("activity", [])
    activity.append(new_id)
    user["activity"] = activity
    nomagic._update_entity_by_id(user_id, user)

    data["user"] = user
    data["like_count"] = 0
    data["like"] = False
    data["comment_count"] = 0

    assert conn.execute_rowcount(
        "INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id,
        new_id)
    return new_id, data
示例#6
0
def create_user(user):
    email = user["email"]
    assert not conn.get("SELECT * FROM index_login WHERE login = %s", email)

    user["type"] = "user"
    user["name"] = user.get("name", "")
    user["salt"] = "".join(
        random.choice(string.ascii_uppercase + string.digits)
        for x in range(10))
    if "password" in user:
        user["password"] = hashlib.sha1(user["password"] +
                                        user["salt"]).hexdigest()
    user["title"] = ""
    user["department"] = ""
    user["locatiion"] = ""
    user["mobile"] = ""
    user["tel"] = ""
    user["about"] = ""
    user["profile_img"] = ""
    user["datetime"] = datetime.datetime.now().isoformat()

    new_id = nomagic._new_key()
    assert nomagic._node(new_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(user))

    #update indexes: email
    assert "@" in email
    assert conn.execute_rowcount(
        "INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email,
        new_id)

    return (new_id, user)
示例#7
0
def new_comment(user_id, entity_id, data):
    entity = nomagic._get_entity_by_id(entity_id)

    data["type"] = "comment"
    data["likes"] = []
    data["user_id"] = user_id
    data["activity_id"] = entity_id
    data["datetime"] = datetime.datetime.now().isoformat()
    data["comment_ids"] = []
    if entity["type"] == "comment":
        data["activity_id"] = entity.get("activity_id")
    else:
        data["activity_id"] = entity_id
    #content valid
    assert data.get("content")

    new_comment_id = nomagic._new_key()
    assert nomagic._node(new_comment_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id,
        nomagic._pack(data))

    comment_ids = entity.get("comment_ids", [])
    comment_ids.append(new_comment_id)
    entity["comment_ids"] = comment_ids
    nomagic._update_entity_by_id(entity_id, entity)

    return comment_ids, dict(data,
                             id=new_comment_id,
                             like_count=0,
                             like=False,
                             user=nomagic._get_entity_by_id(user_id))
示例#8
0
def create_user(user):
    user["type"] = "user"
    user["name"] = user.get("name", "")
    user["salt"] = "".join(
        random.choice(string.ascii_uppercase + string.digits)
        for x in range(10))
    user["password"] = hashlib.sha1(user["password"] +
                                    user["salt"]).hexdigest()
    user["datetime"] = datetime.datetime.now().isoformat()
    user["createtime"] = int(time.time())

    new_id = nomagic._new_key()
    while True:
        new_user = nomagic._get_entity_by_id(new_id)
        if not new_user:
            break
        else:
            new_id = nomagic._new_key()

    rowcount = nomagic._node(new_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(user))
    assert rowcount

    #update indexes: email
    # assert "@" in email
    # rowcount = conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id)
    # assert rowcount

    return (new_id, user)
示例#9
0
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name):
    index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name)
    index_data = nomagic._unpack(index["data"] or "{}")
    index_connection = set(index_data.get(index_connection_name, []))
    if entity_id in index_connection:
        index_connection.remove(entity_id)
        index_data[index_connection_name] = list(index_connection)
        index_data_updated = nomagic._pack(index_data)
        conn.execute("UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name)

    entity = nomagic._get_entity_by_id(entity_id)
    entity_connection = set(entity.get(entity_connection_name, []))
    if index_name in entity_connection:
        entity_connection.remove(index_name)
        entity[entity_connection_name] = list(entity_connection)
        nomagic._update_entity_by_id(entity_id, entity)
示例#10
0
def create_event(event):
    order["type"] = "event"
    order["owner"] = order.get("owner", "")
    order["datetime"] = datetime.datetime.now().isoformat()
    order["createtime"] = int(time.time())

    new_id = nomagic._new_key()
    while True:
        new_order = nomagic._get_entity_by_id(new_id)
        if not new_order:
            break
        else:
            new_id = nomagic._new_key()
    rowcount = nomagic._node(new_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(order))
    assert rowcount
    return (new_id, order)
示例#11
0
文件: feeds.py 项目: WifiPi/router
def update_fileinfo(filehash, user_id, id3info = None, title = None, rev = None):
    if rev:
        if not conn.get("SELECT * FROM index_dropbox WHERE rev=%s", rev):
            assert conn.execute_rowcount("INSERT INTO index_dropbox (rev, entity_id) VALUES (%s, %s)", rev, filehash)

    fileinfo = nomagic._get_entity_by_id(filehash)
    if fileinfo:
        changed = False

        owners = fileinfo.get("owners", [])
        if user_id not in owners:
            owners.append(user_id)
            fileinfo["owners"] = owners
            changed = True

        if id3info and id3info != fileinfo.get("id3info"):
            fileinfo["id3info"] = id3info
            changed = True

        if title and title != fileinfo.get("title"):
            fileinfo["title"] = title
            changed = True

        if changed:
            nomagic._update_entity_by_id(filehash, fileinfo)

    else:
        fileinfo = {"owners":[user_id]}
        if id3info:
            fileinfo["id3info"] = id3info
        if title:
            fileinfo["title"] = title
        assert nomagic._node(filehash).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)",\
                                                                filehash, nomagic._pack(fileinfo))

    user = nomagic._get_entity_by_id(user_id)
    if user:
        files = user.get("files", [])
        if filehash not in files:
            files.append(filehash)
            user["files"] = files
            nomagic._update_entity_by_id(user_id, user)

    return fileinfo, user
示例#12
0
def create_comment(comment):
    comment["type"] = "comment"
    comment["owner"] = comment.get("owner", "")
    comment["owner_type"] = comment.get("owner_type", "entity")
    comment["datetime"] = datetime.datetime.now().isoformat()
    comment["createtime"] = int(time.time())

    new_id = nomagic._new_key()
    while True:
        new_comment = nomagic._get_entity_by_id(new_id)
        if not new_comment:
            break
        else:
            new_id = nomagic._new_key()
    rowcount = nomagic._node(new_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(comment))
    assert rowcount
    return (new_id, comment)
示例#13
0
def create_user(user):
    user["type"] = "user"
    user["name"] = user.get("name", "")
    user["password"] = ""
    user["salt"] = ''.join(
        random.choice(string.ascii_uppercase + string.digits)
        for x in range(10))
    user["title"] = ""
    user["department"] = ""
    user["locatiion"] = ""
    user["mobile"] = ""
    user["tel"] = ""
    user["about"] = ""
    user["profile_img"] = ""
    user["datetime"] = datetime.datetime.now().isoformat()
    new_id = nomagic._new_key()
    assert ring[nomagic._number(new_id)].execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(user))

    #do we need a user_id index? currently no
    #user_id = conn.execute("INSERT INTO index_user_id (entity_id) VALUES(%s)", new_id)

    #update indexes: email
    email = user["email"]
    assert "@" in email
    assert conn.execute_rowcount(
        "INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email,
        new_id)
    """
    email_host = email.split("@")[1]
    organization_id, organization = nomagic.feeds.get_organization_by_email_host(email_host)
    users = organization.get("users", [])
    users.append(new_id)
    organization["users"] = users
    _update_entity_by_id(organization_id, organization)
    """

    return (new_id, user)
示例#14
0
def create_chat(chat):
    chat["type"] = "chat"
    chat["owner"] = chat.get("owner", "")
    chat["owner_type"] = chat.get("owner_type", "entity")
    chat["editors"] = chat.get("editors", [])
    chat["helpers"] = chat.get("helpers", {})
    chat["comment_members"] = chat.get("comment_members", [])
    chat["notifyers"] = chat.get("notifyers", [])
    chat["blackers"] = chat.get("blackers", [])
    chat["datetime"] = datetime.datetime.now().isoformat()
    chat["createtime"] = int(time.time())

    new_id = nomagic._new_key()
    while True:
        new_chat = nomagic._get_entity_by_id(new_id)
        if not new_chat:
            break
        else:
            new_id = nomagic._new_key()
    rowcount = nomagic._node(new_id).execute_rowcount(
        "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id,
        nomagic._pack(chat))
    assert rowcount
    return (new_id, chat)
示例#15
0
def new_status(user_id, data):
    now = datetime.datetime.now()
    data["type"] = "status"
    data["user_id"] = user_id
    data["datetime"] = now.isoformat()
    data["likes"] = []
    data["comment_ids"] = []
    assert data.get("title")

    new_id = nomagic._new_key()
    assert nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data))

    user = nomagic._get_entity_by_id(user_id)
    activity = user.get("activity", [])
    activity.append(new_id)
    user["activity"] = activity
    nomagic._update_entity_by_id(user_id, user)

    data["user"] = user
    data["like_count"] = 0
    data["like"] = False
    data["comment_count"] = 0

    assert conn.execute_rowcount("INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id)
    return new_id, data
示例#16
0
def update_chat(chat_id, data):
    rowcount = nomagic._node(chat_id).execute_rowcount(
        "UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(data),
        nomagic._key(chat_id))
    assert rowcount
示例#17
0
def new_comment(user_id, entity_id, data):
    entity = nomagic._get_entity_by_id(entity_id)

    data["type"] = "comment"
    data["likes"] = []
    data["user_id"] = user_id
    data["activity_id"] = entity_id
    data["datetime"] = datetime.datetime.now().isoformat()
    data["comment_ids"] = []
    if entity["type"] == "comment":
        data["activity_id"] = entity.get("activity_id")
    else:
        data["activity_id"] = entity_id
    #content valid
    assert data.get("content")

    new_comment_id = nomagic._new_key()
    assert nomagic._node(new_comment_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data))

    comment_ids = entity.get("comment_ids", [])
    comment_ids.append(new_comment_id)
    entity["comment_ids"] = comment_ids
    nomagic._update_entity_by_id(entity_id, entity)

    return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
示例#18
0
def create_user(user):
    email = user["email"]
    assert not conn.get("SELECT * FROM index_login WHERE login = %s", email)

    user["type"] = "user"
    user["name"] = user.get("name", "")
    user["salt"] = "".join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
    if "password" in user:
        user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest()
    user["title"] = ""
    user["department"] = ""
    user["locatiion"] = ""
    user["mobile"] = ""
    user["tel"] = ""
    user["about"] = ""
    user["profile_img"] = ""
    user["datetime"] = datetime.datetime.now().isoformat()

    new_id = nomagic._new_key()
    assert nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user))

    #update indexes: email
    assert "@" in email
    assert conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id)

    return (new_id, user)
示例#19
0
def create_user(user):
    user["type"] = "user"
    user["name"] = user.get("name", "")
    user["password"] = ""
    user["salt"] = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
    user["title"] = ""
    user["department"] = ""
    user["locatiion"] = ""
    user["mobile"] = ""
    user["tel"] = ""
    user["about"] = ""
    user["profile_img"] = ""
    user["datetime"] = datetime.datetime.now().isoformat()
    new_id = nomagic._new_key()
    assert ring[nomagic._number(new_id)].execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user))

    #do we need a user_id index? currently no
    #user_id = conn.execute("INSERT INTO index_user_id (entity_id) VALUES(%s)", new_id)

    #update indexes: email
    email = user["email"]
    assert "@" in email
    assert conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id)

    """
    email_host = email.split("@")[1]
    organization_id, organization = nomagic.feeds.get_organization_by_email_host(email_host)
    users = organization.get("users", [])
    users.append(new_id)
    organization["users"] = users
    _update_entity_by_id(organization_id, organization)
    """

    return (new_id, user)