def register_as_candidate(): if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegistrationForm() if form.validate_on_submit(): candidate = Candidate() form.populate_obj(candidate) candidate.set_password(form.password.data) db.session.add(candidate) db.session.commit() send_email_confirmation(candidate) flash( "Congratulations, you've registered successfully. Now check your email to confirm your account" ) return redirect(url_for('auth.login')) return render_template('auth/register-as-candidate.html', title='Register as a candidate', form=form)
def register(): schema = { "type": "object", "properties": { "username": { "type": "string" }, "email": { "type": "string" }, "password": { "type": "string" }, "first_name": { "type": "string" }, "middle_name": { "type": "string" }, "last_name": { "type": "string" } }, "required": ["username", "email", "password", "first_name", "last_name"] } data = request.get_json() try: validate(data, schema=schema) except ValidationError as e: print(e) return jsonify({ 'status': 'Failure', 'msg': 'Missing/incorrect fields: {}'.format(e) }) if (not 'username' in data or not 'email' in data or not 'password' in data): return jsonify({ 'status': 'Failure', 'msg': 'Must provide username, email, and password' }), 400 #Check if email is already in use check_candidate = Candidate.query.filter_by(email=data['email']).first() if (check_candidate is not None): return jsonify({ 'status': 'Failure', 'msg': 'Email is already in use. Please use another' }) check_candidate = Candidate.query.filter_by( username=data['username']).first() if (check_candidate is not None): return jsonify({ 'status': 'Failure', 'msg': 'Username is already in use. Please use another' }) candidate = Candidate( candidate_id=uuid.uuid4().hex, username=data['username'], email=data['email'], first_name=data['first_name'], middle_name=data['middle_name'] if 'middle_name' in data else None, last_name=data['last_name']) candidate.set_password(data['password']) try: db.session.add(candidate) db.session.commit() return jsonify({'status': 'Success'}) except: print('Unexpected error:', sys.exc_info()[0]) return jsonify({ 'status': 'Failure', 'msg': 'Unknown error - please try again later' })