def post(self): data = SubjectRegister.parser.parse_args() subject = SubjectModel.find_by_subject_name(data['subject_name']) if subject: return {"message": "subject already exits."}, 400 subject = SubjectModel(data['subject_name'], data['parent_id']) subject.save_to_db() return {"subject_id": subject.id, "subject": subject.name}, 201
def delete(self, id): subject = SubjectModel.find_by_subject_id(id) if subject: check_child = SubjectModel.find_by_child_id(subject.parent_id) if check_child.parent_id is not None: subject.delete_from_db() return {'message': 'Subject deleted.'}, 201 else: return {'message': 'This subject is a main category.'} return {'message': 'Subject not found.'}, 404
def get(self, id): subject = SubjectModel.find_by_subject_id(id) if subject: return{ 'subject': subject.name, 'contents': list(map(lambda x: x.json(), subject.query.filter_by(id=subject.id).first().content))}, 200 return {'message': 'Subject not found.'}, 404
def delete(self, id): content_name = ContentModel.find_by_id(id) subjects_id = content_name.query.filter_by( id=content_name.id).first().subject if content_name: if subjects_id: for subject in subjects_id: rm = SubjectModel.find_by_subject_id(subject.id) content_name.subject.remove(rm) content_name.save_to_db() content_name.delete_from_db() return {'message': 'Content is deleted'}, 201 return {'message': 'Content not found.'}, 404
def put(self, id): data = Content.parser.parse_args() subject = SubjectModel.find_by_subject_id(data['subject_id']) content = ContentModel.find_by_id(id) if content: try: content.text = data['content'] content.name = data['content_name'] content.subject.append(subject) content.save_to_db() return content.json(), 201 except: return {"message": "An error occurred inserting the content."}, 500 return {'message': 'Content not found.'}, 404
def post(self): claims = get_jwt_claims() data = ContentPost.parser.parse_args() content = ContentModel.find_by_name(data['content_name']) subject = SubjectModel.find_by_subject_id(data['subject_id']) user = UserModel.find_by_id(claims['user_id']) if content: return {'message': 'Content is already exists.'}, 400 else: content = ContentModel(data['content_name'], data['content']) content.subject.append(subject) content.owner.append(user) content.save_to_db() return {'content_id': content.id, 'content_name': content.name}, 201
def put(self, id): data = Subject.parser.parse_args() subject = SubjectModel.find_by_subject_id(id) if subject: try: subject.name = data['subject_name'] subject.parent_id = data['parent_id'] subject.save_to_db() return subject.json(), 201 except: return { "message": "An error occurred inserting the subject." }, 500 return {'message': 'Subject not found.'}, 404
def get(self, id): subject = SubjectModel.find_by_subject_id(id) if subject: return subject.json() return {'message': 'subject not found'}, 404