def put_user(id, data): user = UserModel.objects(id=ObjectId(id)) origin_classId = str(user[0]['classId']['id']) user.update(name=data['name'], avatar=data['avatar'], schoolId=data['schoolId'], classId=ObjectId(data['classId'])) # 如果换了班级,则修改班级人数 if origin_classId != str(data['classId']): ClassModel.objects(id=ObjectId(origin_classId)).update_one( dec__stuNum=1) ClassModel.objects(id=ObjectId(data['classId'])).update_one( inc__stuNum=1)
def add_user(data): wxId = data['wxId'] + current_app.config['SECRET_KEY'] m = hashlib.md5() m.update(wxId.encode("utf8")) wxId = m.hexdigest() clsId = ObjectId(data['classId']) user = UserModel(wxId=wxId, name=data['name'], avatar=data['avatar'], schoolId=data['schoolId'], classId=clsId) user.save() # 给对应班级添加人数 ClassModel.objects(id=clsId).update_one(inc__stuNum=1)
def validate_id(self, value): try: id = ObjectId(value.data) except: raise FormValidateError(msg='班级id无效') cls = ClassModel.objects(id=id).first() if not cls: raise FormValidateError(msg='班级id无效')
def validate_classId(self, value): try: classId = ObjectId(value.data) except: raise FormValidateError(msg='无效的班级') cls = ClassModel.objects(id=classId).first() if not cls: raise FormValidateError(msg='无效的班级')
def validate_classId(self, value): if value.data is not None: try: classId = ObjectId(value.data) except: raise ValueError('班级ID有误') cls = ClassModel.objects(id=classId).first() if not cls: raise ValueError('班级ID有误')
def get_classroom(): classrooms = ClassModel.objects().order_by('name') data = [] for cls in classrooms: data.append({ 'id': str(cls['id']), 'name': cls['name'], 'stuNum': cls['stuNum'] }) return Success(msg='获取班级列表成功!', data=data)
def validate_classroomIds(self, value): classroomIds = value.data.split('|') for classroomId in classroomIds: try: classroomId = ObjectId(classroomId) except: raise ValueError('无效的班级') classroom = ClassModel.objects(id=classroomId).first() if not classroom: raise ValueError('无效的班级')
def get_user_list(): data = request.args or {} form = UserGetListForm(data=data) form.validate_for_api() page = form.page.data size = form.size.data userName = data['userName'] or '' schoolId = data['schoolId'] or '' classroom = data['classroom'] or '' if classroom != '': try: classroom = ObjectId(classroom) except: return FormValidateError(msg='班级id有误') cls = ClassModel.objects(id=classroom).first() if cls is None: return FormValidateError(msg='班级id有误') count = UserModel.objects(name__contains=userName, schoolId__contains=schoolId, classId=cls).count() users = UserModel.objects( name__contains=userName, schoolId__contains=schoolId, classId=cls).order_by('classId', 'schoolId').skip( (int(page) - 1) * int(size)).limit(int(size)) else: count = UserModel.objects(name__contains=userName, schoolId__contains=schoolId).count() users = UserModel.objects( name__contains=userName, schoolId__contains=schoolId).order_by('classId', 'schoolId').skip( (int(page) - 1) * int(size)).limit(int(size)) list = [] for user in users: list.append({ 'id': str(user['id']), 'name': user['name'], 'classId': str(user['classId']['id']), 'classname': str(user['classId']['name']), 'schoolId': user['schoolId'], 'avatar': user['avatar'] }) data = { 'pagination': { 'count': count, 'size': int(size), 'page': int(page) }, 'list': list } return Success(msg='获取成功!', data=data)
def post(self): """新增班级""" parser = reqparse.RequestParser() parser.add_argument("class_name", required=True, nullable=False, type=non_empty_string) parser.add_argument("info") args = parser.parse_args() _class = ClassModel.query.filter_by(name=args["class_name"]).first() if _class: return {"status": "failed", "message": "此班级已经存在"} new_class = ClassModel(name=args["class_name"], info=args["info"]) db.session.add(new_class) db.session.commit() return {"status": "ok", "message": "添加成功"}
def get_sign_list(page, size, classId): if classId is not None: classId = ObjectId(classId) cls = ClassModel.objects(id=classId).first() if cls: clss = SignModel.objects( classroomIds__in=[classId]).order_by('-created_at').skip( (page - 1) * size).limit(size) count = SignModel.objects(classroomIds__in=[classId]).count() else: clss = [] count = 0 else: clss = SignModel.objects().order_by('-created_at').skip( (page - 1) * size).limit(size) count = SignModel.objects().count() data = [] for cls in clss: classrooms = [] for room in cls['classroomIds']: classrooms.append({ 'id': str(room['id']), 'name': room['name'], 'stuNum': room['stuNum'] }) data.append({ 'id': str(cls['id']), 'name': cls['name'], 'adminName': cls['adminId']['name'], 'classrooms': classrooms, 'created_at': cls['created_at'] }) res = { 'pagination': { 'count': count, 'size': size, 'page': page }, 'list': data } return res
def delete_classroom(id): data = {'id': id} ClassRoomDeleteForm(data=data).validate_for_api() ClassModel.delete_classroom(id=id) return Success(msg='删除班级成功!')
def put_classroom(id): data = request.json or {} data['id'] = id ClassRoomPutForm(data=data).validate_for_api() ClassModel.put_classroom(id=id, name=data['name']) return Success(msg='修改班级成功!')
def post_classroom(): data = request.json ClassRoomForm(data=data).validate_for_api() ClassModel.add_classroom(name=data['name']) return Success(msg='添加班级成功!')
def validate_name(self, value): name = value.data cls = ClassModel.objects(name=name).first() if cls: raise FormValidateError(msg='班级名重复')
def delete_user(id): user = UserModel.objects(id=ObjectId(id)) clsId = user[0]['classId']['id'] user.delete() # 给对应班级减去人数 ClassModel.objects(id=clsId).update_one(dec__stuNum=1)