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
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
def create_user(user): email = user["email"] login = conn.get("SELECT * FROM index_login WHERE login = %s", email) assert not login 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["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() 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)
def new_comment(user_id, entity_id, data): data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] #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)) entity = nomagic._get_entity_by_id(entity_id) 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))
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("content") 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
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)
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)
def create_user(user): email = user["email"] login = conn.get("SELECT * FROM index_login WHERE login = %s", email) assert not login 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["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() 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)
def new_comment(user_id, entity_id, data): data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] #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)) entity = nomagic._get_entity_by_id(entity_id) 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))
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("content") 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