def dis_follow(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'user_id': '', }) a_id = UtilsController.get_id_by_token(_param['access_token']) if a_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') p = 0 b_id = _param['user_id'] if int(a_id) > int(b_id): a_id, b_id = b_id, a_id p = -1 print(a_id) print(b_id) queryset = models.FollowMapping.objects.filter(user_left_id=a_id, user_right_id=b_id) obj = None for k in queryset: obj = k break print(obj.to_list_dict()) if obj: if p == 0: obj.left_to_right = False else: obj.right_to_left = False print(obj.to_list_dict()) obj.save() return rS.success() else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '关系不存在')
def dis_follow(request: HttpRequest): _param = validate_and_return(request,{ 'access_token':'', 'user_id':'', }) a_id = UtilsController.get_id_by_token(_param['access_token']) if a_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR,'此账号已在别处登陆') p = 0 b_id = _param['user_id'] if a_id < b_id: a_id,b_id = b_id,a_id p = -1 obj = models.FollowMapping.objects.get(user_left_id=a_id,user_right_id=b_id) if obj: if p == 0: obj.left_to_right = False else: obj.right_to_left = False if obj.save(): return rS.success() else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR,'取消关注失败') else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR,'没有关注此人')
def modify_enable_visited_list(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'sex': '', 'phone': '', 'status': '', 'birth': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') enable_visited_list = 0 list_dict = { 'sex': _param['sex'], 'birth': _param['birth'], 'phone': _param['phone'], 'status': _param['status'], } for k, v in list_dict: enable_visited_list = enable_visited_list << 1 | v obj = models.User.objects.get(pk=user_id) obj.enable_visited_list = enable_visited_list if obj.save(): return rS.success() else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '修改失败')
def is_follow(request: HttpRequest): _param = validate_and_return(request,{ 'access_token':'', 'user_id':'', }) a_id = UtilsController.get_id_by_token(_param['access_token']) if a_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') b_id = _param['user_id'] p = 0 if a_id > b_id: a_id,b_id = b_id,a_id p = -1 obj = models.FollowMapping.objects.get(user_left_id=a_id,user_right_id=b_id) if obj is None: return rS.success({ 'is_follow':False, }) if p == 0: return rS.success({ 'is_follow':obj.left_to_right, }) else: return rS.success({ 'is_follow': obj.right_to_left, })
def modify_enable_visited_list(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'birth': '', 'phone': '', 'status': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') enable_visited_list = 0 list_dict = { 'birth': _param['birth'], 'phone': _param['phone'], 'status': _param['status'], } for v in list_dict: print(v) enable_visited_list = enable_visited_list << 1 | int(list_dict[v]) queryset = models.User.objects.filter(pk=user_id) obj = None for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此用户不存在') obj.enable_visited_list = enable_visited_list obj.save() return rS.success()
def get_session_by_id(request: HttpRequest): _param = validate_and_return(request,{ 'access_token': '', 'session_id': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, "该用户已在别处登录") queryset = models.Session.objects.filter(pk=_param['session_id']) obj = None for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR,"该会话不存在") if models.Session.objects.get(pk=obj.id).type == 0: session_name = models.Group.objects.get(pk=models.Session.objects.get(pk=obj.id).left_id).name else: if obj.left_id == user_id: session_name = models.User.objects.get(pk=obj.right_id).nickname else: session_name = models.User.objects.get(pk=obj.left_id).nickname return rS.success({ "id": obj.id, 'type': obj.type, 'title': session_name, })
def modify_password(request: HttpRequest): _param = validate_and_return( request, { 'access_token': '', 'ex_password': '', 'new_password': '', 'confirm_password': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') queryset = models.User.objects.filter(pk=user_id) obj = None for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, "该用户不存在") pwd = obj.password if _param['ex_password'] != pwd: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '密码错误') if _param['new_password'] != _param['confirm_password']: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '两次输出的密码不一致') obj.password = _param['new_password'] obj.save() return rS.success()
def follow(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'user_id': '', }) a_id = UtilsController.get_id_by_token(_param['access_token']) if a_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') p = 0 b_id = _param['user_id'] if int(a_id) > int(b_id): a_id, b_id = b_id, a_id p = -1 queryset = models.FollowMapping.objects.filter(user_left_id=a_id).filter( user_right_id=b_id) obj = None for k in queryset: obj = k break print(obj) if obj is not None: print(obj.to_list_dict()) if p == 0: obj.left_to_right = True NotificationController.create_notification( 1, b_id, models.User.objects.get(pk=a_id).nickname + '关注了你') else: obj.right_to_left = True NotificationController.create_notification( 1, a_id, models.User.objects.get(pk=b_id).nickname + '关注了你') obj.save() return rS.success() else: if p == 0: rs = models.FollowMapping.objects.create(user_left_id=a_id, user_right_id=b_id, left_to_right=True, right_to_left=False) NotificationController.create_notification( 1, b_id, models.User.objects.get(pk=a_id).nickname + '关注了你') if rs: return rS.success() else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '关注失败') else: rs = models.FollowMapping.objects.create(user_left_id=a_id, user_right_id=b_id, left_to_right=False, right_to_left=True) NotificationController.create_notification( 1, a_id, models.User.objects.get(pk=b_id).nickname + '关注了你') if rs: return rS.success() else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '关注失败')
def check_token(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail() else: return rS.success()
def get_information_by_id(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'user_id': '', }) a_id = UtilsController.get_id_by_token(_param['access_token']) if a_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') obj = models.User.objects.get(pk=_param['user_id']) _ = obj.to_list_dict() return rS.success(_)
def is_admin(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'group_id': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) obj = models.UserFollowGroupMapping.objects.get(user=user_id, group=_param['group_id']) ans = ((obj.role == 1) | (obj.role == 2)) return rS.success({ 'is_admin': ans })
def get_notification_list(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'page': 'int', 'size': 'int', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登录') queryset = models.Notification.objects.all().order_by("-create_time") list_data = [] count = 0 for k in queryset: if k.notification_type == 0: list_data.append(k.to_list_dict()) count += 1 if k.notification_type == 1: if k.to_id == user_id: list_data.append(k.to_list_dict()) count += 1 if k.notification_type == 2: if models.UserFollowGroupMapping.objects.filter(group=k.to_id, user=user_id): list_data.append(k.to_list_dict()) count += 1 if k.notification_type == 3: if models.UserFollowGroupMapping.objects.filter(group=k.to_id, user=user_id): if models.UserFollowGroupMapping.objects.get(group=k.to_id, user=user_id).role == 1 | \ models.UserFollowGroupMapping.objects.get(group=k.to_id, user=user_id).role == 2: list_data.append(k.to_list_dict()) count += 1 if k.notification_type == 4: if models.UserAttendActivityMapping.objects.filter( user=user_id, activity=k.to_id): list_data.append(k.to_list_dict()) count += 1 page = _param['page'] size = _param['size'] return rS.success({ 'count': count, 'list': list_data[(page - 1) * size:page * size], })
def is_enable_searched(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') obj = models.User.objects.get(pk=user_id) return rS.success({ 'is_enable_searched': obj.enable_searched, })
def modify_information(request: HttpRequest): _param = validate_and_return( request, { 'access_token': '', 'sex': 'nullable', 'nickname': 'nullable', 'phone': 'nullable', 'status': 'nullable', 'birth': 'nullable', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登录') _param.pop('access_token') models.User.objects.filter(pk=user_id).update(**_param) return rS.success()
def is_admin(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'group_id': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) queryset = models.UserFollowGroupMapping.objects.filter( user=user_id, group=_param['group_id']) obj = None for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '该用户不在此群组') ans = ((obj.role == 1) | (obj.role == 2)) return rS.success({'is_admin': ans})
def get_session_list(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'page': 'int', 'size': 'int', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR,'此账号已在别处登录') page = _param['page'] size = _param['size'] # obj = models.User.objects.get(pk=user_id) session_list = models.Session.objects.all().order_by("-latest_update_time") count = 0 list_data = list() for k in session_list: if not k.is_active: continue print(k.id) if k.type == 0: queryset = models.UserFollowGroupMapping.objects.filter(user_id=user_id, group_id=k.left_id) if queryset: dict_data = k.to_list_dict() dict_data.setdefault("title") dict_data['latest_update_time'] = str(dict_data['latest_update_time']) dict_data['title'] = models.Group.objects.get(id=k.left_id).name list_data.append(dict_data) count += 1 else: dict_data = k.to_list_dict() dict_data.setdefault("title") dict_data['latest_update_time'] = str(dict_data['latest_update_time']) if k.left_id == user_id: dict_data['title'] = models.User.objects.get(id=k.right_id).nickname list_data.append(dict_data) count += 1 if k.right_id == user_id: dict_data['title'] = models.User.objects.get(id=k.left_id).nickname list_data.append(dict_data) count += 1 return rS.success({ "count": count, "list": list_data[(page-1)*size:page*size] })
def delete(request: HttpRequest): """ 删除活动 :param request: :return: """ _param = validate_and_return(request, { 'activity_id': '', 'access_token': '' }) _activity = models.Activity.objects.filter(pk=_param['activity_id']) _user_id = UtilsController.get_id_by_token(_param['access_token']) if _activity.count() != 0: _activity.delete() return rS.success() else: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')
def get_information(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') queryset = models.User.objects.filter(pk=user_id) obj = None for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '用户不存在') rs = obj.to_list_dict() print(rs['birth']) print(rs) return rS.success(rs)
def get_follow_list(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'page': 'int', 'size': 'int' }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, "此账号已在别处登陆") page = _param['page'] size = _param['size'] list_data = [] follow_mapping_list = models.FollowMapping.objects.filter( user_left_id=user_id) count = follow_mapping_list.count() for k in follow_mapping_list: if not k.left_to_right: continue data = { 'id': k.user_right_id, 'nickname': models.User.objects.get(id=k.user_right_id).nickname, } list_data.append(data) follow_mapping_list = models.FollowMapping.objects.filter( user_right_id=user_id) count += follow_mapping_list.count() for k in follow_mapping_list: if not k.right_to_left: continue data = { 'id': k.user_left_id, 'nickname': models.User.objects.get(id=k.user_left_id).nickname, } list_data.append(data) list_data.sort(key=lambda k: (k.get('nickname', ''))) return rS.success({ 'count': count, 'list': list_data[(page - 1) * size:page * size], })
def modify_enable_searched(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'enable_searched': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') queryset = models.User.objects.filter(pk=user_id) obj = None for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '用户不存在') obj.enable_searched = _param['enable_searched'] obj.save() return rS.success()
def get_session(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'type': '', 'left_id': '', # type = 0 left_id = group_id else = cur_user_id 'right_id': '', # type = 0 right = 0 else = to_id }) if UtilsController.get_id_by_token(_param['access_token']) == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, "该用户已在别处登录") l_id, r_id = _param['left_id'], _param['right_id'] int(l_id) int(r_id) session_type = int(_param['type']) if session_type == 1: if l_id > r_id: l_id, r_id = r_id, l_id session_id = is_session_exist(l_id, r_id) if session_id == -1: session_id = create_session(session_type, l_id, r_id) if session_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, "此会话不存在,请重新尝试") else: if session_type == 0: session_name = models.Group.objects.get(pk=_param['left_id']).name else: session_name = models.User.objects.get(id=_param['right_id']).nickname return rS.success({ "id": session_id, "type": models.Session.objects.get(pk=session_id).type, "title": session_name, }) else: if session_type == 0: session_name = models.Group.objects.get(pk=_param['left_id']).name else: session_name = models.User.objects.get(id=_param['right_id']).nickname return rS.success({ "id": session_id, 'type': _param['type'], 'title': session_name, })
def get_enable_visited_list(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') obj = models.User.objects.get(pk=user_id) temp = int(obj.enable_visited_list) print(temp) dict_data = { 'birth': 0, 'phone': 0, 'status': 0, } times = 2 for k in dict_data: dict_data[k] = (temp >> times) & 1 times = times - 1 return rS.success(dict_data)
def create_message(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'session_id': '', 'content': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) _param.pop('access_token') if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '该用户已在别处登录') if len(_param['content']) == 0: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '内容为空,请输入信息再重新发送') _param.setdefault("from_id") _param["from_id"] = user_id if models.Session.objects.get(pk=_param['session_id']).type == 0: perminssion_check = models.UserFollowGroupMapping.objects.all() for k in perminssion_check: print(k.user_id) print(k.group_id) if k.user_id == user_id | k.group_id == models.Session.objects.get( pk=_param['session_id']).left_id: if k.role != 1 | k.role != 2: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, "没有权限发信息") rs = models.Message.objects.create(**_param) rs.save() SessionController.update_session_time(_param['session_id'], rs) if rs: if models.Session.objects.get(pk=_param['session_id']).type == 0: create_notification(2, models.Session.objects.get(pk=_param['session_id']).left_id, \ "来自" + \ models.Group.objects.get(\ pk=models.Session.objects.get(\ pk=_param['session_id']).left_id).name\ + "的消息"+models.User.objects.get(pk=user_id).nickname \ + " @全体成员 "+ _param['content']) return rS.success() else: return rS.fail((rS.ReturnResult.UNKNOWN_ERROR, "发送失败"))
def is_follow(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'user_id': '', }) a_id = UtilsController.get_id_by_token(_param['access_token']) if a_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') b_id = _param['user_id'] p = 0 if int(a_id) > int(b_id): a_id, b_id = b_id, a_id p = -1 print(a_id) print(b_id) queryset = models.FollowMapping.objects.filter(user_left_id=a_id).filter( user_right_id=b_id) print(queryset) obj = None for k in queryset: obj = k break if obj is None: print('关系不存在') return rS.success({ 'is_follow': False, }) if p == 0: return rS.success({ 'is_follow': obj.left_to_right, }) else: return rS.success({ 'is_follow': obj.right_to_left, })
def get_message_list_by_session_id(request: HttpRequest): _param = validate_and_return(request, { 'access_token': '', 'session_id': '', 'page': 'int', 'size': 'int', }) print(_param['session_id']) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '该用户已在别处登录') page = _param['page'] size = _param['size'] queryset = models.Session.objects.filter(pk=_param['session_id']) obj = None msg_list = models.Message.objects.all().order_by("send_time") list_data = [] count = 0 for k in queryset: obj = k break if obj is None: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此会话不存在') for k in msg_list: # print(k.session_id == int(_param['session_id'])) if k.session_id == int(_param['session_id']): # print(k.to_list_dict()) data = k.to_list_dict() list_data.append(data) count += 1 return rS.success({ 'count': count, 'list': list_data[(page - 1) * size:page * size], })
def modify_password(request: HttpRequest): _param = validate_and_return( request, { 'access_token': '', 'ex_password': '', 'new_password': '', 'confirm_password': '', }) user_id = UtilsController.get_id_by_token(_param['access_token']) if user_id == -1: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '此账号已在别处登陆') obj = models.User.objects.get(pk=user_id) pwd = obj.password if _param['password'] != pwd: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '密码错误') if _param['new_password'] != _param['confirm_password']: return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '两次输出的密码不一致') obj.password = _param['new_password'] obj.save() return rS.ReturnResult()