def get(self, subject_id=None, page=None, per_page=None): if ( request.args.get("page") and request.args.get("per_page") and request.args.get("username") ): page = int(request.args.get("page")) name = request.args.get("username") per_page = int(request.args.get("per_page")) list_subject = SubjectModel.find_list_by_name(name, page, per_page) if list_subject is None: return {"messages": err_404.format("list_subject")}, 404 return ( { "list": SubjectModel.to_json(list_subject), "count ": len(list_subject), }, 200, ) if subject_id is None: list = [] for subject in SubjectModel.query.paginate(page, per_page, False).items: list.append(subject.json()) return {"list": list, "count": len(SubjectModel.query.all())}, 200 subject = SubjectModel.find_by_subject_id(subject_id) if subject is None: return {"messages": err_404.format("subject")}, 404 return subject.json(), 200
def post(self): data = Subject.parser.parse_args() if SubjectModel.find_by_name(data["name"]): return {"messages": err_duplicate.format("subject")}, 400 subject = SubjectModel(**data) try: subject.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def post(self): data = self.parser.parse_args()['subjects'] body = data['body'] date = data['date'] name = data['name'] user_id = get_jwt_identity() user: UserModel = UserModel.find_by_id(user_id) user.subjects_date = date for subject in body: subject_model = SubjectModel(name, subject, user_id) subject_model.save_to_db() if date is None: signuper = SubjectSignuper(body, user.get_cookies()) signuper.execute() return {"message": "Successfully added subjects"}, 201
def get(self): data = request.args subject_id = data.get("subject_id") subject_name = data.get("subject_name") subject_code = data.get("subject_code") print(data) subjects = None if subject_name: subject = SubjectModel.find_by_subject_name(subject_name) elif subject_id: subject = SubjectModel.find_by_subject_id(subject_id) elif subject_code: subject = SubjectModel.find_by_subject_code(subject_code) if subject: return subject.json() return {'message': 'Subject not found'}, 404
def post(self): data = Mark.parser.parse_args() if MarkModel.find(data["user_id"], data["exam_date"], data["subject_id"]): return {"messages": "this row existed"}, 400 if UserModel.find_by_user_id(data["user_id"]).job != 1: return {"messages": "Bạn chỉ gán điểm được cho học sinh"}, 400 ## check khóa ngoại if UserModel.find_by_user_id(user_id=data["user_id"]) is None: return {"messages": err_404.format("user")} if ExamModel.find_by_exam_date(exam_date=data["exam_date"]) is None: return {"messages": err_404.format("exam")} if SubjectModel.find_by_subject_id( subject_id=data["subject_id"]) is None: return {"messages": err_404.format("subject")} mark = MarkModel( mark=data["mark"], user_id=data["user_id"], exam_date=data["exam_date"], subject_id=data["subject_id"], ) try: mark.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def find_assessment_skill_by_any(cls, **kwargs): cols = ['skill_name', 'skill_code', 'subject_id', 'subject_name' 'category_id', 'isactive'] filter_str = 'cls.query' if 'skill_name' in kwargs.keys(): filter_str = filter_str + '.filter_by(skill_name="' + str( kwargs['skill_name']) + '")' if 'skill_code' in kwargs.keys(): filter_str = filter_str + '.filter_by(skill_code="' + str( kwargs['skill_code']) + '")' if 'subject_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(subject_id="' + str( kwargs['subject_id']) + '")' if 'category_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(category_id="' + str( kwargs['category_id']) + '")' if 'isactive' in kwargs.keys(): filter_str = filter_str + '.filter_by(isactive="' + str( kwargs['isactive']) + '")' if 'subject_name' in kwargs.keys(): subject = SubjectModel.find_by_subject_name(kwargs["subject_name"]) if subject: subject_id = subject.id filter_str = filter_str + '.filter_by(subject_id="'\ + str(subject_id) + '")' if 'category_name' in kwargs.keys(): assessment_category = AssessmentCategoryModel.find_by_category_name( kwargs['category_name']) if assessment_category: assessment_category_id = assessment_category.id filter_str = filter_str + '.filter_by(category_id="' + str(category_id) + '")' filter_str = filter_str + '.all()' return eval(filter_str)
def find_by_subject_name(cls, subject_name): subject = SubjectModel.find_by_subject_name(subject_name) if subject: subject_id = subject.id return cls.query.filter_by(subject_id=subject_id).first() else: return None
def post(self): data = Subject_And_Class.parser.parse_args() if Subject_And_ClassModel.find_row(data["class_id"], data["subject_id"]): return {"messages": err_duplicate.format("class")}, 400 # check khóa ngoại if SubjectModel.find_by_subject_id(data["subject_id"]) is None: return {"messages": err_404.format("subject")}, 404 if ClasssModel.find_by_class_id(data["class_id"]) is None: return {"messages": err_404.format("class")}, 404 if datetime.datetime.now() > datetime.datetime.strptime( data["exam_date"], "%Y-%m-%d"): return {"messages": "exam date you set is in the past"}, 400 row = Subject_And_ClassModel( class_id=data["class_id"], subject_id=data["subject_id"], exam_date=datetime.datetime.strptime(data["exam_date"], "%Y-%m-%d"), ) try: row.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def put(self, mark_id): data = Mark.parser.parse_args() mark = MarkModel.find_by_mark_id(mark_id) if mark is None: return {"messages": err_404.format("mark")}, 404 # check khóa ngoại if UserModel.find_by_user_id(user_id=data["user_id"]) is None: return {"messages": err_404.format(data["user_id"])} if (ExamModel.find_by_exam_date_and_subject_id( exam_date=data["exam_date"], subject_id=data["subject_id"]) is None): return { "messages": "Không tìm thấy có môn bạn tìm vào ngày {0}".format( data["exam_date"]) } if SubjectModel.find_by_subject_id( subject_id=data["subject_id"]) is None: return {"messages": err_404.format(data["subject_id"])} if data["mark"]: mark.mark = data["mark"] if data["exam_id"]: mark.exam_id = data["exam_id"] if data["subject_id"]: mark.subject_id = data["subject_id"] if data["user_id"]: mark.user_id = data["user_id"] try: mark.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def set_attribute(self, payload): cols = [ 'attempt_id', 'student_id', 'subject_id', 'category_id', 'skill_id', 'period_id', 'year', 'month', 'day', 'session', 'score', 'full_score', 'grade', 'creation_date', 'modified_date', 'modified_by' ] for col in cols: if col in payload.keys(): setattr(self, col, payload[col]) if 'subject_name' in payload.keys(): subjects = SubjectModel.find_by_subject_name( payload["subject_name"]) if subjects: self.subject_id = subjects[0].id if 'category_name' in payload.keys(): data = {'category_name': payload["category_name"]} assessment_categories = AssessmentCategoryModel.find_assessment_category_by_any( **data) if assessment_categories: self.category_id = assessment_categories[0].id if 'skill_name' in payload.keys(): # data = {'skill_name': payload["skill_name"]} assessment_skill = AssessmentSkillModel.find_by_skill_name( payload["skill_name"]) if assessment_skill: self.skill_id = assessment_skill.id if 'subject_name' in payload.keys(): data = {'category_name': payload["subject_name"]} subject = StudentModel.find_by_student_name( payload["subject_name"]) if subject: self.subject_id = subject.id
def put(self, exam_date, subject_id): data = Exam.parser.parse_args() exam = ExamModel.find_by_exam_date_and_subject_id( exam_date, subject_id) if exam is None: return {"messages": err_404.format("exam")}, 404 if data["exam_room"]: exam.exam_room = data["exam_room"] if data["exam_date"]: exam.exam_date = data["exam_date"] if data["exam_start_time"]: exam.exam_start_time = data["exam_start_time"] if data["exam_time"]: exam.exam_time = data["exam_time"] if data["subject_id"]: exam.subject_id = data["subject_id"] # check khóa ngoại if SubjectModel.find_by_subject_id( subject_id=data["subject_id"]) is None: return {"messages": err_404.format("subject")}, 404 try: exam.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def get(self): data = request.args subjects = SubjectModel.find_by_any(**data) if subjects: if not data.get("hierarchy", False): resp = [] for subject in subjects: resp.append(subject.json()) return resp, 200 else: subject_resp_out = [] for subject in subjects: subject_resp_in = subject.json() subject_id = subject.id data1 = {'subject_id': subject_id} categories = AssessmentCategoryModel.find_assessment_category_by_any( **data1) category_resp_out = [] for category in categories: category_resp_in = category.json() category_id = category.id data2 = {'category_id': category_id} skills = AssessmentSkillModel.find_assessment_skill_by_any( **data2) skill_resp = [] for skill in skills: skill_resp.append(skill.json()) category_resp_in["skill"] = skill_resp category_resp_out.append(category_resp_in) subject_resp_in["category"] = category_resp_out subject_resp_out.append(subject_resp_in) return subject_resp_out
def put(self, class_id, subject_id): data = Subject_And_Class.parser.parse_args() row = Subject_And_ClassModel.find_row(class_id, subject_id) if row is None: return {"messages": err_404.format("row")}, 404 else: if SubjectModel.find_by_subject_id(data["subject_id"]) is None: return {"messages": err_404.format("subject")}, 404 if ClasssModel.find_by_class_id(data["class_id"]) is None: return {"messages": err_404.format("class")}, 404 if datetime.datetime.now() > datetime.datetime.strptime( data["exam_date"], "%Y-%m-%d"): return {"messages": "exam date you set is in the past"}, 400 try: if data["class_id"]: row.class_id = data["class_id"] if data["exam_date"]: row.exam_date = datetime.datetime.strptime( data["exam_date"], "%Y-%m-%d") if data["subject_id"]: row.user_id = data["user_id"] row.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def json(self): student = StudentModel.find_by_student_id(self.student_id) cluster = ClusterModel.find_by_cluster_id(student.cluster_id) kalika_kendra = KalikaKendraModel.find_by_kalika_kendra_id( student.kalika_kendra_id) return { 'result_id': self.id, 'attempt_id': self.attempt_id, 'student_id': self.student_id, 'student_name': student.student_name if student else None, 'cluster_id': cluster.id if cluster else None, 'cluster_name': cluster.cluster_name if cluster else None, 'kalika_kendra_id': kalika_kendra.id if kalika_kendra else None, 'kalika_kendra_name': kalika_kendra.kalika_kendra_name if kalika_kendra else None, 'subject_id': self.subject_id, 'subject_name': SubjectModel.find_by_subject_id(self.subject_id).subject_name, 'assessment_category_id': self.category_id, 'assessment_category_name': AssessmentCategoryModel.find_by_category_id( self.category_id).category_name, 'assessment_skill_id': self.skill_id, 'assessment_skill_name': AssessmentSkillModel.find_by_skill_id(self.skill_id).skill_name if AssessmentSkillModel.find_by_skill_id(self.skill_id) else None, 'assessment_period_id': self.period_id, 'assessment_year': self.year, 'assessment_month': self.month, 'assessment_day': self.day, 'assessment_session': self.session, 'assessment_score': self.score, 'assessment_full_score': self.full_score, 'assessment_grade': self.grade, 'creation_date': str(self.creation_date), 'modified_date': str(self.modified_date), 'modified_by': self.modified_by }
def mutate(self, info, name, department): q = Department.get_query(info) subject_department = q.filter_by(id=department).one() new_subject = SubjectModel(name=name, department=subject_department) db_session.add(new_subject) db_session.commit() return CreateSubject(id=new_subject.id, name=new_subject.name, department=new_subject.department)
def delete(self, subject_id): subject = SubjectModel.find_by_subject_id(subject_id) if subject is None: return {"messages": err_404.format("subject")}, 404 try: subject.delete_from_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def set_attribute(self, payload): cols = ['category_name', 'category_code', 'subject_id', 'isactive'] for col in cols: if col in payload.keys(): setattr(self, col, payload[col]) if 'subject_name' in payload.keys(): subject = SubjectModel.find_by_subject_name(payload["subject_name"]) if subject: self.subject_id = subject.id
def json(self): return {'skill_id': self.id, 'skill_name': self.skill_name, 'skill_code': self.skill_code, 'subject_id': self.subject_id, 'subject_name': SubjectModel.find_by_subject_id(self.subject_id).subject_name, 'category_id': self.category_id, 'category_name': AssessmentCategoryModel.find_by_category_id( self.category_id).category_name, 'isactive': self.isactive}
def put(self, subject_id): data = Subject.parser.parse_args() subject = SubjectModel.find_by_subject_id(subject_id) if subject is None: return {"messages": err_404.format("subject")}, 404 else: try: subject.name = data["name"] subject.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}
def set_attribute(self, payload): cols = ['skill_name', 'skill_code', 'subject_id', 'category_id','isactive'] for col in cols: if col in payload.keys(): setattr(self, col, payload[col]) if 'subject_name' in payload.keys(): subjects = SubjectModel.find_by_subject_name(payload["subject_name"]) if subjects: self.subject_id = subjects[0].id if 'category_name' in payload.keys(): data = {'category_name': payload["category_name"]} assessment_categories = AssessmentCategoryModel.find_assessment_category_by_any( **data) if assessment_categories: self.assessment_category_id = assessment_categories[0].id
def post(self): data = Exam.parser.parse_args() if ExamModel.find_by_exam_date_and_subject_id(data["exam_date"], data["subject_id"]): return {"messages": err_duplicate.format("exam")}, 400 try: exam_date = datetime.datetime.strptime(data["exam_date"], "%Y-%m-%d") exam_start_time = datetime.datetime.strptime( data["exam_start_time"], "%H-%M") except: return ( { "messages": "start date or end date was not valid a date form. Please try again" }, 400, ) # check date if exam_date > datetime.datetime.now(): return {"messages": "exam date can't be the day in the past"}, 400 # check khóa ngoại if SubjectModel.find_by_subject_id( subject_id=data["subject_id"]) is None: return {"messages": err_404.format("subject")}, 404 exam = ExamModel( exam_room=data["exam_room"], exam_date=exam_date, exam_start_time=exam_start_time, exam_time=data["exam_time"], subject_id=data["subject_id"], ) exam.save_to_db() try: exam.save_to_db() except: return {"messages": err_500}, 500 return {"messages": noti_201}, 201
def json(self): return {'category_id': self.id, 'category_name': self.category_name, 'category_code': self.category_code, 'subject_id': self.subject_id, 'subject_name': SubjectModel.find_by_subject_id(self.subject_id).subject_name, 'isactive': self.isactive}
def find_assessment_result_by_any(cls, **kwargs): cols = [ 'attempt_id', 'student_id', 'subject_id', 'assessment_category_id', 'assessment_skill_id', 'assessment_period_id', 'assessment_year', 'assessment_month', 'assessment_day', 'assessment_session', 'assessment_score', 'assessment_full_score', 'assessment_grade', 'creation_date', 'modified_date', 'modified_by' ] filter_str = 'cls.query' if 'result_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(id="' + str( kwargs['result_id']) + '")' if 'attempt_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(attempt_id="' + str( kwargs['attempt_id']) + '")' if 'student_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(student_id="' + str( kwargs['student_id']) + '")' if 'subject_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(subject_id="' + str( kwargs['subject_id']) + '")' if 'category_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(category_id="' + str( kwargs['category_id']) + '")' if 'skill_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(skill_id="' + str( kwargs['skill_id']) + '")' if 'period_id' in kwargs.keys(): filter_str = filter_str + '.filter_by(period_id="' + str( kwargs['period_id']) + '")' if 'year' in kwargs.keys(): filter_str = filter_str + '.filter_by(year="' + str( kwargs['year']) + '")' if 'month' in kwargs.keys(): filter_str = filter_str + '.filter_by(month="' + str( kwargs['month']) + '")' if 'day' in kwargs.keys(): filter_str = filter_str + '.filter_by(day="' + str( kwargs['day']) + '")' if 'session' in kwargs.keys(): filter_str = filter_str + '.filter_by(session="' + str( kwargs['session']) + '")' if 'score' in kwargs.keys(): filter_str = filter_str + '.filter_by(score="' + str( kwargs['score']) + '")' if 'full_score' in kwargs.keys(): filter_str = filter_str + '.filter_by(full_score="' + str( kwargs['full_score']) + '")' if 'grade' in kwargs.keys(): filter_str = filter_str + '.filter_by(grade="' + str( kwargs['grade']) + '")' if 'creation_date' in kwargs.keys(): filter_str = filter_str + '.filter_by(creation_date="' + str( kwargs['creation_date']) + '")' if 'modified_date' in kwargs.keys(): filter_str = filter_str + '.filter_by(modified_date="' + str( kwargs['modified_date']) + '")' if 'modified_by' in kwargs.keys(): filter_str = filter_str + '.filter_by(modified_by="' + str( kwargs['modified_by']) + '")' if 'subject_name' in kwargs.keys(): subject = SubjectModel.find_by_subject_name(kwargs['subject_name']) if subject: subject_id = subject.id filter_str = filter_str + '.filter_by(subject_id="'\ + str(subject_id) + '")' if 'category_name' in kwargs.keys(): assessment_category = AssessmentCategoryModel.find_by_category_name( kwargs['category_name']) if assessment_category: category_id = assessment_category.id filter_str = filter_str + '.filter_by(category_id="' + str( category_id) + '")' if 'student_name' in kwargs.keys(): student = StudentModel.find_by_student_name(kwargs['student_name']) if student: student_id = student.id filter_str = filter_str + '.filter_by(student_id="' + str( student_id) + '")' if 'skill_name' in kwargs.keys(): skill = AssessmentSkillModel.find_by_skill_name( kwargs['skill_name']) if skill: skill_id = skill.id filter_str = filter_str + '.filter_by(skill_id="' + str( skill_id) + '")' if 'cluster_id' in kwargs.keys(): payload = {"cluster_id": kwargs["cluster_id"]} students = StudentModel.find_by_student_by_any(**payload) if students: student_ids = [s.id for s in students] # print(student_ids) filter_str = filter_str + '.filter(cls.student_id.in_(' + str( student_ids) + '))' if 'kalika_kendra_id' in kwargs.keys(): payload = {"kalika_kendra_id": kwargs["kalika_kendra_id"]} students = StudentModel.find_by_student_by_any(**payload) if students: student_ids = [s.id for s in students] filter_str = filter_str + '.filter(cls.student_id.in_(' + str( student_ids) + '))' if 'cluster_name' in kwargs.keys(): payload = {"cluster_name": kwargs["cluster_name"]} students = StudentModel.find_by_student_by_any(**payload) if students: student_ids = [s.id for s in students] filter_str = filter_str + '.filter(cls.student_id.in_(' + str( student_ids) + '))' if 'kalika_kendra_name' in kwargs.keys(): payload = {"kalika_kendra_name": kwargs["kalika_kendra_name"]} students = StudentModel.find_by_student_by_any(**payload) if students: student_ids = [s.id for s in students] filter_str = filter_str + '.filter(cls.student_id.in_(' + str( student_ids) + '))' filter_str = filter_str + '.all()' return eval(filter_str)