def new_agent(): agent = Agent() db.session.add(agent) db.session.commit() agent.name = names[agent.id - 1] db.session.commit() return agent
def generateNewToken(agent_id): generateTokenForm = GenerateTokenForm() if generateTokenForm.validate_on_submit(): myAgent = Agent.load_agent(agent_id) myAgent.token = Agent.generate_token() db.session.commit() flash("Agent %s has a new key of: %s" % (myAgent.agentid, myAgent.token), "success") return redirect(request.referrer) else: flash("Couldn't generate new token", "danger") return redirect(request.referrer)
def generate_new_token(agent_id): generateTokenForm = GenerateTokenForm() if generateTokenForm.validate_on_submit(): myAgent = Agent.load_agent(agent_id) myAgent.token = Agent.generate_token() db.session.commit() flash(f"Agent {myAgent.agentid} has a new key of: {myAgent.token}", "success") else: flash("Couldn't generate new token", "danger") return redirect(url_for("user.profile"))
def agent_register(): form = AgentRegistrationForm() if form.validate_on_submit(): agent = Agent(username=form.username.data, email=form.email.data, name=form.name.data, proffession=form.proffession.data) agent.set_password(form.password.data) db.session.add(agent) db.session.commit() flash('Congratulations, you are now a registered user!') return redirect(url_for('agent_login')) return render_template('agent/register.html', title='Register', form=form)
def newAgent(): newAgentForm = AgentNameForm() if newAgentForm.validate_on_submit(): myAgent = Agent(user_id=current_user.id, agentid=Agent.generate_agentid(), token=Agent.generate_token(), \ friendly_name=newAgentForm.agent_name.data) db.session.add(myAgent) db.session.commit() flash("New Agent named %s created. Agent ID: %s Agent Token: %s" \ % (myAgent.friendly_name, myAgent.agentid, myAgent.token), "success") return redirect(request.referrer) else: flash("Couldn't create new agent", "danger") return redirect(request.referrer)
def agent_registration(): form = AgentRegisterForm() if form.validate_on_submit(): email = form.email.data if User.query.filter(User.email == email).first() is not None: flash('Email has already been registered') return redirect(url_for('auth.agent_registration')) # create agent object first agent = Agent(first_name=form.first_name.data, last_name=form.last_name.data, phone=form.phone.data, agency_name=form.agency_name.data) db.session.add(agent) # create user object next role = Role.get_role('agent') # note the agent and role backrefs which creates the table relationships user = User(email=form.email.data, agent=agent, role=role) user.set_password(form.password.data) db.session.add(user) # commit both user and agent to database db.session.commit() # automatically log in user login_user(user) flash('You have successfully registered!') return redirect(url_for('agent.agent_account')) return render_template('auth/agent-registration.html', form=form)
def signup(): form = RegisterForm() if form.validate_on_submit(): data = form.data if data["password"] != data["passwordtwo"]: flash("密码不一致") return redirect(url_for("home.signup")) if data["usertype"] == 1: customer = Customer.query.filter_by(email=data["email"]).count() if customer == 1: flash(generate_password_hash(data["password"])) return redirect(url_for("home.signup")) customer = Customer( email = data["email"], password = generate_password_hash(data["password"]) ) db.session.add(customer) db.session.commit() flash("Registration success!") return redirect(url_for("home.login")) if data["usertype"] == 2: agent = Agent.query.filter_by(email=data["email"]).count() if agent == 1: flash("The email already exists!") return redirect(url_for("home.signup")) agent = Agent( email=data["email"], password=generate_password_hash(data["password"]), booking_agent_id = ''.join(random.sample(string.ascii_letters + string.digits, 8)) ) db.session.add(agent) db.session.commit() flash("Registration success!") return redirect(url_for("home.login")) return render_template("home/signup.html",form=form)
def new_agent(): newAgentForm = AgentNameForm() if newAgentForm.validate_on_submit(): myAgent = Agent( user_id=current_user.id, agentid=Agent.generate_agentid(), token=Agent.generate_token(), friendly_name=newAgentForm.agent_name.data, ) db.session.add(myAgent) db.session.commit() flash( f"New Agent named {myAgent.friendly_name} created. Agent ID: {myAgent.agentid} Agent Token: {myAgent.token}", "success", ) else: flash("Couldn't create new agent", "danger") return redirect(url_for("user.profile"))
def changeAgentName(agent_id): agentNameForm = AgentNameForm() if agentNameForm.validate_on_submit(): myAgent = Agent.load_agent(agent_id) oldname = myAgent.friendly_name myAgent.friendly_name = agentNameForm.agent_name.data db.session.commit() flash("Agent name changed from %s to %s" % (oldname, myAgent.friendly_name), "success") return redirect(request.referrer) else: flash("Couldn't change agent name", "danger") return redirect(request.referrer)
def change_agent_name(agent_id): agentNameForm = AgentNameForm() if agentNameForm.validate_on_submit(): myAgent = Agent.load_agent(agent_id) oldname = myAgent.friendly_name myAgent.friendly_name = agentNameForm.agent_name.data db.session.commit() flash(f"Agent name changed from {oldname} to {myAgent.friendly_name}", "success") else: flash("Couldn't change agent name", "danger") return redirect(url_for("user.profile"))
def decorated_function(*args, **kwargs): if current_app.config['AGENT_AUTHENTICATION']: if not request.headers.has_key("Authorization"): return json.dumps({ 'status': 403, 'message': 'Authorization is required to access this endpoint', 'retry': False }), \ 403, {'content-type':'application/json'} authz = request.headers["Authorization"].split() if authz[0].lower() != "bearer": return json.dumps({ 'status': 403, 'message': 'Authorization is required to access this endpoint', 'retry': False }), \ 403, {'content-type':'application/json'} agent_id = authz[1].split(':', 1)[0] agent_token = authz[1].split(":", 1)[1] agent = Agent.load_agent(agent_id) if not agent or not agent.verify_token(agent_token): return json.dumps({ 'status': 403, 'message': 'Authorization is required to access this endpoint', 'retry': False }), \ 403, {'content-type':'application/json'} return f(*args, **kwargs)
def decorated_function(*args, **kwargs): # if we don't require agent authentication then don't bother if not current_app.config["AGENT_AUTHENTICATION"]: return f(*args, **kwargs) if not (request.headers.get("Authorization", None) and Agent.verify_agent(request.headers["Authorization"])): status_code = 403 response_body = json.dumps({ "status": status_code, "message": "Authorization is required to access this endpoint", "retry": False, }) return Response( response=response_body, status=status_code, content_type="application/json", ) return f(*args, **kwargs)
def agents_add_process(request): """ Admin: Process add agent form""" args = _get_args(request) if not user_logged_in(request): return redirect_login() if request.POST.get('form_name') == 'add_agent': agent = Agent() agent.first_name = request.POST.get('first_name') agent.last_name = request.POST.get('last_name') agent.username = request.POST.get('username') agent.phone = request.POST.get('phone') agent.address = request.POST.get('address') try: agent.id_filename = util.handle_uploaded_file(request.FILES.get('id')) agent.sig_filename = util.handle_uploaded_file(request.FILES.get('sig')) password = "******" agent.password_salt = util.generate_uuid() agent.password = hashlib.md5(password + agent.password_salt) \ .hexdigest() agent.save() return redirect('/admin/manage/agents?msg=1') except ValueError, ex: return redirect('/admin/manage/agents/add?msg=' + str(ex))