def add_user(request): ''' add users to a session url: /im2/add_user http method: POST @param users: a list of user id @param sesson_id: @return: { failed_list: a list of users {user_id: , user_name:} session_info: { 'session_id' : 'session_name' : 'users' : 'status' : } } ''' post = json.loads(request.raw_post_data) session_id = post["session_id"] channel = Channel() session = channel.check_session(session_id) if not session: pass channel.add_users(session_id, *post["users"]) render = { 'failed_list' : [], 'session_info': { 'session_id' : session_id, 'session_name' : session["session_name"], 'users' : session["users"], 'status' : True, } } return HttpResponse(json.dumps(render), mimetype="application/json")
def quit(request): ''' quit a session http method: POST url: /im2/quit @param session_id: @return: success or failed ''' post = json.loads(request.raw_post_data) session_id = post["session_id"] channel = Channel() session = channel.check_session(session_id) if not session: return HttpResponse(json.dumps('failed, session not exist'), mimetype="application/json") channel.quit(session_id, request.user.pk) return HttpResponse(json.dumps('success'), mimetype="application/json")
def new_im(request): ''' send message url: /im2/new_message http method: POST @param session_id: @param message: @return: { 'session_info' : { 'session_id' : 'session_name' : 'users' : 'status' : }, sender_id: sender_name: type: 'im' message: the content } ''' post = json.loads(request.raw_post_data) session_id = post["session_id"] channel = Channel() session = channel.check_session(session_id) if not session: pass session_status = { 'session_id' : session_id, 'session_name': session["session_name"], 'status' : True, 'users' : session["users"], } message = { 'sender_id' : request.user.pk, 'sender_name': request.user.get_profile().name, 'type' : 'im', 'message' : post["message"], 'session_info': session_status, } channel.new_im(message, session_id) return HttpResponse(json.dumps(message), mimetype="application/json")