def create_user(data): cursor = connection.cursor() # insert user in db try: query = Query() query.add_insert("user", data.items()) cursor.execute(query.get()) # if insert failed, that means the user with this name is existed except Exception as e: print str(e) cursor.close() return None # get just insert user for answer try: query.clear() query.select_last_insert_id() cursor.execute(query.get()) user_id = cursor.fetchone()[0] if data["isAnonymous"] == 1: name = None about = None username = None else: name = data["name"] username = data["username"] about = data["about"] response = data response["name"] = name response["username"] = username response["about"] = about response["id"] = user_id # unknown error except Exception as e: print str(e) cursor.close() return None cursor.close() return response
def subscribe_user(thread, user): cursor = connection.cursor() code = Code try: query = Query() query.add_insert("subscribe", (("user", user), ("thread", thread))) cursor.execute(query.get()) except Exception as e: print str(e) cursor.close() return { 'code': code.NOT_FOUND, "response": "insert failed, user or thread not found" } cursor.close() response = {"thread": thread, "user": user} return {'code': code.OK, "response": response}
def follow_user(follower, followee): cursor = connection.cursor() code = Code try: query = Query() query.add_insert("follow", (("follower", follower), ("following", followee))) cursor.execute(query.get()) except Exception as e: print str(e) cursor.close() return { 'code': code.NOT_FOUND, "response": "insert failed, user or thread not found" } cursor.close() response = get_detail_user(follower) return {'code': code.OK, "response": response}