def post(self): data = CompanyController.parser.parse_args() name = data['name'] old_company = CompanyModel.find_by_name(name) if old_company: abort(409, message="Company already exists.") admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") company = CompanyModel(name=data['name'], description=data['description']) admin_user.companies.append(company) company.admin_id = admin_user # save the database try: db.session.add(admin_user) db.session.add(company) db.session.commit() except (ArgumentError, DataError): abort(500, message="Server internal error due to invalid argument.") return company.json(), 201
def post(self): data = EmployeeController.parser.parse_args() username = data['username'] email = data['email'] if (UserModel.find_by_username(username) or UserModel.find_by_email(email)): return {'message': "User already exists."}, 400 # Because only admin can create employee admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") # Create a new Employee user employee = EmployeeModel(isAdmin=data['isAdmin']) user_data = data del user_data['isAdmin'] user = UserModel(**user_data) user = user.hash_password() user.employee_id = employee employee.user_id = user # save the database try: db.session.add(user) db.session.add(employee) db.session.commit() except (ArgumentError, DataError): abort(500, message="Server internal error due to invalid argument.") return employee.get_employee(user), 201
def get(self): user = UserModel.find_by_id(current_identity.id) if not user: abort(500, message="Some internal fault on user.") admin_user = AdminModel.find_by_user_id(current_identity.id) if not admin_user: abort(403, message="Please use admin or ask admin to crete company.") companies = CompanyModel.query.all() if not len(companies): return {"companies": companies} res = {"companies": []} for company in companies: res["companies"].append(company.json()) return res, 200