def post(self): data = parser.parse_args() if DepartmentModel.find_by_name(data['name']): return { 'message': 'This department {} already exists '.format(data['name']) } if DepartmentModel.find_by_code(data['code']): return { 'message': 'This code {} already exists '.format(data['code']) } #CREATE MODEL DATA FOR SAVE new_department = DepartmentModel( name=data['name'], code=data['code'], class_id=data['class_id'], ) try: new_department.save_to_db() #CALL THIS FUNCTION FOR COMMIT DATA return { 'message': ' This {} department data created successfully'.format( data['name']) }, 200 except: return {'message': 'Something went wrong'}, 500
def post(self, name): department = DepartmentModel.find_by_name(name) if department: return { "message": "A department with name {} already exists.".format(name) }, 400 department = DepartmentModel(name) try: department.save_to_db() except: return { "message": "An error occurred while inserting the details." }, 500 return { "message": "Department added successfully", "Department": department.json() }, 201