def get(self,user_id): '''获取用户所在班级信息''' response['code']=200 user_type = int(request.args.get('user_type')) items = [] if user_type == 1: members = Member.objects(Q(user=ObjectId(user_id)) & Q(user_type=user_type) & Q(status=1)) for member in members: clazzs = Clazz.objects(id=member.clazz_id).all() for clazz in clazzs: item = clazz.json() item['is_creator']=member.is_creator del item['creator'] items.append(item) else: students = Student.objects(user=ObjectId(user_id)).all() for student in students: members = Member.objects(Q(user=ObjectId(user_id)) & Q(student=student.id) & Q(status=1)) clazzs_json = [] for member in members: clazzs = Clazz.objects(id=member.clazz_id).all() for clazz in clazzs: clazz_json = clazz.json() del clazz_json['creator'] clazz_json['is_creator']=member.is_creator clazzs_json.append(clazz_json) if len(clazzs_json) > 0: student_json = student.json() del student_json['user'] student_json['clazzs'] = clazzs_json items.append(student_json) response['data']=items return response
def put(self,member_id,clazz_id): '''同意或拒接加班申请''' is_agree = int(request.form.get('is_agree')) response['code']=200 if is_agree == 1: Member.objects(id=member_id).update(status=1) Clazz.objects(id=clazz_id).update(inc__member_count=1) response['msg']='老师同意申请加班' threading.Thread(target=send_agree_join_class_push, args=(clazz_id,member_id)).start() else: threading.Thread(target=send_refuse_join_class_push, args=(clazz_id,member_id)).start() response['msg']='老师拒绝申请加班' return response
def get(self,clazz_id): '''获取班级成员列表''' members = Member.objects(clazz_id=clazz_id).exclude('status').all() response['code'] = 200 response['msg']='班级列表' items=[] for member in members: item = member.json() items.append(item) response['data']=items return response
def send_refuse_join_class_push(clazz_id,member_id): redis_helper = RedisOperation() clazz = Clazz.objects(id=clazz_id).first() redis_helper.set_channel('class') redis_helper.set_push_type(103) push_ids = [] member = Member.objects(id=member_id).first() push_ids.append(str(member.user.id)) redis_helper.set_push_ids(push_ids) push_content_json={} push_content_json['classId']=str(clazz.id) push_content_json['memberId']=str(member.id) push_content_json['content']=clazz.creator.name + "老师拒绝你加入" + clazz.clazz_name redis_helper.set_push_content(push_content_json) redis_helper.push()
def post(self): '''发布消息''' response = {} args = publish_message_parser.parse_args() message = Message() message.content = args['content'] message.title = args['title'] user = User.objects(id=args['user_id']).only('name').first() message.publisher = user.name message.publisher_id = args['user_id'] message.clazz_id = args['clazz_id'] message.create_time = time.time()*1000 members = Member.objects(Q(clazz_id=args['clazz_id']) & Q(status=1)).only('user') unread_users = [] for member in members: if str(member.user.id) != args['user_id']: unread_users.append(member.user) message.unread_users = unread_users if args['attachments'] != None: items = eval(args['attachments']) mongo_attachments = [] for item in items: attachment = Attachment() attachment.width = item['width'] attachment.height = item['height'] attachment.url = "http://qiniu1.com/" + item['key'] mongo_attachments.append(attachment) message.attachments = mongo_attachments message.save() response['code']=200 response['msg']='发送消息成功' response['data']=message.json(False) threading.Thread(target=send_publish_message_push, args=(unread_users,message.id)).start() return response
def post(self): '''创建班级 ''' user_id = request.form.get('user_id') clazz_name = request.form.get('clazz_name') clazz = Clazz.objects.order_by('-clazz_num').first() if clazz == None: clazz = Clazz() clazz.creator = User.objects(id=user_id).first() clazz.clazz_name = clazz_name clazz.create_time = time.time()*1000 clazz.save() ##添加成员 member = Member() member.clazz_id = str(clazz.id) member.user = clazz.creator member.user_type = 1 member.is_creator = True member.status = 1 member.save() response['code']=200 response['msg']='创建班级成功' response['data']=clazz.json() else: clazz_new = Clazz() clazz_new.creator = User.objects(id=user_id).first() clazz_new.clazz_name = clazz_name clazz_new.clazz_num = clazz.clazz_num + 1 clazz_new.create_time = time.time()*1000 clazz_new.save() ##添加成员 member = Member() member.clazz_id = str(clazz_new.id) member.user = clazz_new.creator member.user_type = 1 member.is_creator = True member.status = 1 member.save() response['code']=200 response['msg']='创建班级成功' response['data']=clazz_new.json() return response
def post(self,clazz_id): '''申请加班''' user_id = request.form.get('user_id') user_type = int(request.form.get('user_type')) student_id = request.form.get('student_id') response['code']=200 response['msg']='申请加班成功' if user_type == 1: member = Member.objects(Q(clazz_id=clazz_id) & Q(user=ObjectId(user_id)) & Q(status=2)).first() if member: threading.Thread(target=send_join_class_push, args=(clazz_id,member)).start() return response else: member = Member.objects(Q(clazz_id=clazz_id) & Q(user=ObjectId(user_id)) & Q(student=ObjectId(student_id)) & Q(status=2)).first() if member: threading.Thread(target=send_join_class_push, args=(clazz_id,member)).start() return response member = Member() member.user = User.objects(id=user_id).first() member.clazz_id = clazz_id member.user_type = user_type member.status = 2 if student_id != None: member.student = Student.objects(id=student_id).first() member.is_creator=False member.save() threading.Thread(target=send_join_class_push, args=(clazz_id,member)).start() return response