def create(): email = request.form.get('email') email_taken = Employee.get_or_none(Employee.email == email) if email_taken: flash('Email already taken.', 'danger') return redirect(url_for('employees.new')) username = request.form.get('username') username_taken = Employee.get_or_none(Employee.username == username) if username_taken: flash('Username already taken', 'danger') return redirect(url_for('employees.new')) password = request.form.get('password') hashed_password = generate_password_hash(password) signup = Employee(email=email, username=username, password=hashed_password) try: signup.save() flash('Employee successfully signed up', 'success') employee = Employee.get_or_none(Employee.username == username) login_user(employee) return redirect(url_for('events.index')) except: flash('Error creating Employee', 'danger') return redirect(url_for('employees.new'))
def post(self): body = request.get_json() hased_password = Employee.hash_password(body["password"]) emp = Employee( body["name"], body["age"], body["email"], hased_password, body["phone"], datetime.strptime(body["hire_date"], r"%Y/%m/%d"), ) try: emp.save() except Exception as e: return {"msg": "Something went wrong", "description": e}, 500 if emp.json()["id"] != None: return { "msg": "New Employee created.", "created_employee": emp.json() }, 201 return {"msg": "Something went wrong"}, 500