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 status(request): cursor = connection.cursor() query = Query() response = {} for table in ['post', 'thread', 'forum', 'user']: query.clear() query.add_select(table, "count(*)") cursor.execute(query.get()) response[table] = cursor.fetchone()[0] cursor.close() return HttpResponse(dumps({'code': Code.OK, "response": 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 unsubscribe_user(thread, user): cursor = connection.cursor() code = Code try: query = Query() query.add_delete("subscribe") query.add_where_condition(" user = \"{}\" and thread = {} ".format(user, 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}
def unfollow_user(follower, followee): cursor = connection.cursor() code = Code try: query = Query() query.add_delete("follow") query.add_where_condition( " follower = \"{}\" and following = \"{}\" ".format( follower, followee)) cursor.execute(query.get()) except Exception as e: print str(e) cursor.close() return { 'code': code.NOT_FOUND, "response": "delete failed, user not found" } cursor.close() response = get_detail_user(follower) return {'code': code.OK, "response": response}