def insert_form(cls, data: dict = None): if data is None: data = dict() meta = data.get('meta', {}) lesson_id = meta.get('lesson', {}).get('lesson_id', None) if lesson_id is None: raise CustomError(500, 200, '课程不能为空') if not FormService.check_lesson_meta(meta): raise CustomError(500, 200, '该督导在该时间段, 听过别的课!时间冲突!') dao.Form.insert_form(data) form_model = dao.Form.formatter_total(data) LessonService.refresh_notices( data.get("meta", {}).get("lesson", {}).get("lesson_id")) #刷新听课次数 ModelLessonService.refresh_vote_nums( data.get("meta", {}).get("lesson", {}).get("lesson_id")) #刷新好评课程次数 send_kafka_message(topic='form_service', method='add_form', term=meta.get('term', None), bind_meta_name=form_model.get( 'bind_meta_name', None), username=meta.get('guider', None), form_id=form_model.get('_id', ''), lesson_id=lesson_id) cls.push_new_form_message(form_model) return True
def update_supervisor(cls, ctx: bool = True, query_dict: dict = None, data: dict = None): if data is None: data = {} if query_dict is None: query_dict = {} query = Supervisor.query.filter(Supervisor.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, Supervisor) (supervisors, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for supervisor in supervisors: for key, value in data.items(): if hasattr(supervisor, key): setattr(supervisor, key, value) db.session.add(supervisor) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def update_model_lesson(cls, ctx: bool = True, query_dict: dict = None, data: dict = None): if data is None: data = {} if query_dict is None: query_dict = {} data = cls.reformatter_update(data) query = ModelLesson.query.filter(ModelLesson.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, ModelLesson) (model_lessons, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for model_lesson in model_lessons: for key, value in data.items(): if hasattr(model_lesson, key): setattr(model_lesson, key, value) db.session.add(model_lesson) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def update_lesson_record(cls, ctx: bool = True, query_dict: dict = None, data: dict = None): if data is None: data = dict() if query_dict is None: query_dict = dict() data = cls.reformatter_update(data) query = LessonRecord.query.filter(LessonRecord.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, LessonRecord) (lesson_records, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for lesson_record in lesson_records: for key, value in data.items(): if hasattr(lesson_record, key): setattr(lesson_record, key, value) db.session.add(lesson_record) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def query_forms(cls, query_dict: dict = None, unscoped: bool = False): from app.utils.mongodb import mongo if query_dict is None: query_dict = dict() if not unscoped: query_dict['using'] = [True] url_condition = mongodb_url_condition.UrlCondition(query_dict) if len(url_condition.filter_dict) == 0: try: datas = mongo.db.form.find() except Exception as e: raise CustomError(code=500, status_code=500, err_info=str(e)) return datas, datas.count() if '_id' in url_condition.filter_dict: url_condition.filter_dict['_id']['$in'] = [ mongodb_url_condition.ObjectId(item) for item in url_condition.filter_dict['_id']['$in'] ] try: datas = mongo.db.form.find(url_condition.filter_dict) except Exception as e: raise CustomError(500, 500, str(e)) datas = mongodb_url_condition.sort_limit(datas, url_condition.sort_limit_dict) paginate = mongodb_url_condition.Paginate(datas, url_condition.page_dict) datas = paginate.data_page return [cls.formatter_simple(data=data) for data in datas], paginate.total
def update_work_plan(cls, ctx: bool = True, query_dict: dict = None, data: dict = None): if data is None: data = dict() if query_dict is None: query_dict = dict() data = cls.reformatter_update(data) query = WorkPlan.query.filter(WorkPlan.using == True) url_condition = mysql_url_condition.UrlCondition(query_dict) try: query = mysql_url_condition.process_query( query, url_condition.filter_dict, url_condition.sort_limit_dict, WorkPlan) (work_plans, total) = mysql_url_condition.page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for work_plan in work_plans: for key, value in data.items(): if hasattr(work_plan, key): setattr(work_plan, key, value) db.session.add(work_plan) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def update_activity_user(cls, ctx: bool = True, query_dict: dict = None, data: dict = None): if data is None: data = {} if query_dict is None: query_dict = {} data = cls.reformatter_update(data) query = ActivityUser.query.filter(ActivityUser.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, ActivityUser) (activity_users, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for activity_user in activity_users: for key, value in data.items(): if hasattr(activity_user, key): setattr(activity_user, key, value) db.session.add(activity_user) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def update_form(cls, _id=None, data: dict = None): if data is None: data = dict() form = dao.Form.get_form(query_dict={'_id': _id}) if form is None: raise CustomError(404, 404, 'form not found') dao.Form.update_form({'_id': _id}, data) if 'status' in data: form = dao.Form.get_form(query_dict={'_id': _id}) if form is None: raise CustomError(404, 404, 'form not found') lesson_id = form.get('meta', {}).get('lesson', {}).get('lesson_id', None) if data.get('status') == '待提交': send_kafka_message(topic='form_service', method='repulse_form', term=form.get('meta', {}).get('term', None), username=form.get('meta', {}).get('guider', None), form=form, lesson_id=lesson_id) cls.push_put_back_form_message(form) if data.get('status') == '已提交': send_kafka_message(topic='form_service', method='add_form', lesson_id=lesson_id) return True
def update_consult_type(cls, ctx: bool = True, query_dict: dict = None, data: dict = None): if data is None: data = {} if query_dict is None: query_dict = {} data = cls.reformatter_update(data) query = ConsultType.query.filter(ConsultType.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, ConsultType) (consult_types, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for consult_type in consult_types: for key, value in data.items(): if hasattr(consult_type, key): setattr(consult_type, key, value) db.session.add(consult_type) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def update_group(cls, ctx=True, query_dict: dict = None, data: dict = None): if data is None: data = {} if query_dict is None: query_dict = {} url_condition = UrlCondition(query_dict) query = Group.query.filter(Group.using == True) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, Group) (groups, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for group in groups: for key, value in data.items(): if hasattr(group, key): setattr(group, key, value) db.session.add(group) if ctx: try: db.session.commit() except Exception as e: db.session.rollback() raise CustomError(500, 500, str(e)) return True
def delete_form(cls, where_dict: dict = None): from app.utils.mongodb import mongo if where_dict is None: raise CustomError(500, 500, 'condition can not be None') try: mongo.db.form.update(where_dict, {'$set': {'using': False}}) except Exception as e: raise CustomError(500, 500, str(e)) return True
def get_form_meta(cls, query_dict: dict = None, unscoped: bool = False): if query_dict is None: query_dict = dict() if not query_dict.get('name', None): raise CustomError(500, 200, 'name must be given') from app.utils.mongodb import mongo if not unscoped: query_dict['using'] = [True] url_condition = mongodb_url_condition.UrlCondition(query_dict) try: data = mongo.db.form_meta.find_one(url_condition.filter_dict) except Exception as e: raise CustomError(500, 500, str(e)) return cls.formatter_total(data)
def delete_user(cls, ctx=True, username: str = None): try: user = User.query.filter(User.username == username).first() except Exception as e: raise CustomError(500, 500, str(e)) user.using = False db.session.add(user) if ctx: try: db.session.commit() except Exception as e: db.session.rollback() raise CustomError(500, 500, str(e)) return True
def update_form(cls, where_dict: dict = None, data: dict = None): if data is None: data = dict() from app.utils.mongodb import mongo if where_dict is None: condition = dict() condition['using'] = True if data is None: raise CustomError(200, 500, 'change data can not be None') try: mongo.db.form.update(where_dict, {'$set': data}) except Exception as e: raise CustomError(500, 500, str(e)) return True
def query_lessons(cls, query_dict: dict = None, unscoped: bool = False): if query_dict is None: query_dict = {} query = Lesson.query if not unscoped: query = query.filter(Lesson.using == True) lesson_or_teacher_name = query_dict.get('lesson_or_teacher_name_or', None) if lesson_or_teacher_name is not None and len( lesson_or_teacher_name) != 0: del query_dict['lesson_or_teacher_name_or'] lesson_or_teacher_name = lesson_or_teacher_name[0] query = query.filter( or_( Lesson.lesson_name.like(lesson_or_teacher_name + "%"), Lesson.lesson_teacher_name.like(lesson_or_teacher_name + "%"))) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, Lesson) (lessons, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) return [cls.formatter(lesson) for lesson in lessons], total
def query_teacher_names(cls, query_dict: dict = None, unscoped: bool = False): if query_dict is None: query_dict = {} query = NoticeLesson.query.with_entities( NoticeLesson.lesson_teacher_id, NoticeLesson.lesson_teacher_name, NoticeLesson.lesson_teacher_unit, NoticeLesson.lesson_attention_reason, NoticeLesson.group_name).distinct() if not unscoped: query = query.filter(NoticeLesson.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, NoticeLesson) (lessons, total) = page_query(query, url_condition.page_dict) print(lessons) except Exception as e: raise CustomError(500, 500, str(e)) return [{ 'lesson_teacher_name': data.lesson_teacher_name, 'lesson_teacher_id': data.lesson_teacher_id, 'lesson_teacher_unit': data.lesson_teacher_unit, 'lesson_attention_reason': data.lesson_attention_reason, 'group_name': data.group_name } for data in lessons], total
def login(cls, username: str, password: str): user = User.query.filter(User.username == username).filter( User.using == True).first() if user is None or not check_password_hash(user.password_hash, password): raise CustomError(401, 401, '用户名或密码错误') login_user(user, remember=False)
def get_group(cls, group_name: str): try: group = Group.query.filter(Group.using == True).filter( Group.name == group_name).first() except Exception as e: raise CustomError(500, 500, str(e)) return cls.formatter(group)
def formatter(cls, lesson_record): if lesson_record is None: return None try: lesson_record_dict = misc.model_to_dict(lesson_record) except Exception as e: raise CustomError(500, 500, str(e)) return lesson_record_dict
def get_term(cls, term_name: str, unscoped: bool = False): term = Term.query if not unscoped: term = term.filter(Term.using == True) try: term = term.filter(Term.name == term_name).first() except Exception as e: raise CustomError(500, 500, str(e)) return cls.formatter(term)
def get_now_term(cls): date = datetime.now() (terms, num) = dao.Term.query_terms(query_dict={ 'begin_time_lte': [str(date)], 'end_time_gte': [str(date)] }) if num == 0: raise CustomError(500, 200, 'term not exist') return terms[0]
def insert_user(cls, ctx=True, data=None): if data is None: raise CustomError(500, 200, 'data must be given') data = cls.reformatter_insert(data) user = User() for key, value in data.items(): if key == 'password': user.password = value if hasattr(user, key): setattr(user, key, value) db.session.add(user) if ctx: try: db.session.commit() except Exception as e: db.session.rollback() raise CustomError(500, 500, str(e)) return True
def delete_model_lesson(cls, ctx: bool = True, query_dict: dict = None): if query_dict is None: query_dict = {} query = ModelLesson.query.filter(ModelLesson.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, ModelLesson) (model_lessons, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for model_lesson in model_lessons: model_lesson.using = False db.session.add(model_lesson) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def update_user(cls, ctx=True, username: str = None, data: dict = None): if data is None: data = {} data = cls.reformatter_update(data) try: user = User.query.filter(User.username == username).filter(User.using == True).first() except Exception as e: raise CustomError(500, 500, str(e)) for key, value in data.items(): if hasattr(user, key): setattr(user, key, value) db.session.add(user) if ctx: try: db.session.commit() except Exception as e: db.session.rollback() raise CustomError(500, 500, str(e)) return True
def delete_event(cls, ctx: bool = True, query_dict: dict = None): if query_dict is None: query_dict = {} query = Event.query.filter(Event.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, Event) (events, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for event in events: event.using = False db.session.add(event) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def delete_lesson_case(cls, ctx: bool = True, query_dict: dict = None): if query_dict is None: query_dict = {} query = cls.query.filter(cls.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, cls) (lesson_cases, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for lesson_case in lesson_cases: lesson_case.using = False db.session.add(lesson_case) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def delete_activity_user(cls, ctx: bool = True, query_dict: dict = None): if query_dict is None: query_dict = {} query = ActivityUser.query.filter(ActivityUser.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, ActivityUser) (activity_users, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for activity_user in activity_users: activity_user.using = False db.session.add(activity_user) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def delete_supervisor(cls, ctx: bool = True, query_dict: dict = None): if query_dict is None: query_dict = {} query = Supervisor.query.filter(Supervisor.using == True) url_condition = UrlCondition(query_dict) try: query = process_query(query, url_condition.filter_dict, url_condition.sort_limit_dict, Supervisor) (supervisors, total) = page_query(query, url_condition.page_dict) except Exception as e: raise CustomError(500, 500, str(e)) for supervisor in supervisors: supervisor.using = False db.session.add(supervisor) if ctx: try: db.session.commit() except Exception as e: raise CustomError(500, 500, str(e)) return True
def get_user(cls, query_dict: dict, unscoped=False): user = User.query if not unscoped: user = user.filter(User.using == True) url_condition = UrlCondition(query_dict) try: user = process_query(user, url_condition.filter_dict, url_condition.sort_limit_dict, User).first() except Exception as e: raise CustomError(500, 500, str(e)) return cls.formatter(user)
def get_event(cls, query_dict: dict, unscoped: bool = False): event = Event.query if not unscoped: event = event.filter(Event.using == True) url_condition = UrlCondition(query_dict) try: event = process_query(event, url_condition.filter_dict, url_condition.sort_limit_dict, Event).first() except Exception as e: raise CustomError(500, 500, str(e)) return cls.formatter(event)