def authorized(): # retrieve basic user information print("Redirect from Spotify") try: resp = spotify.authorized_response() if resp is None: return 'Access denied: reason={0} error={1}'.format( request.args['error_reason'], request.args['error_description']) else: session['oauth_token'] = { "access_token": resp['access_token'], "refresh_token": resp['refresh_token'], "expires_in": resp['expires_in'], "expires_at": int(time.time()) + resp['expires_in'] } me = spotify.request('/v1/me/') if me.status != 200: return 'HTTP Status Error: {0}'.format(resp.data) else: print(me.data) if me.data["display_name"] is None: display_name = "" else: display_name = me.data["display_name"] user = User.query.filter_by(id=me.data["id"]).first() if user is None: userhash = str(uuid.uuid4()) user = User(id=me.data["id"], username=display_name, userhash=userhash, consent_to_share=False) db.session.add(user) db.session.commit() # Whether user is set or not, always update the consent_to_share user.consent_to_share = True if session[ "consent_to_share"] == "True" else False db.session.commit() session["userid"] = user.id scrape() print(session["redirecturl"]) return redirect("/personality_survey") except OAuthException: print 'Access denied'
def setup_webapp_db(): from webapp import db, bcrypt, User, Project # from webapp.models import User, Project print('Building webapp database...') hashed_password = bcrypt.generate_password_hash("genomics").decode('utf-8') db.create_all() db.session.add(User(username = "******", email = "*****@*****.**", password = hashed_password)) db.session.commit() db.session.close()
def create_fake_user(bcrypt): f = Faker() new_user = User(email=f.email() , username=f.user_name() , name=f.name() , password=bcrypt.generate_password_hash(f.password()).decode('utf-8')) db.session.add(new_user) db.session.commit() return new_user.name
def get(self, user_id): """ Get wishes for user id """ user = User.query.filter_by(vk_id=user_id).first() if not user: user = User(vk_id=user_id) db.session.add(user) db.session.commit() return { "success": True, "message": errors.OK_MESSAGE, "result": [i.serialize for i in user.wishes] }, 200
def get(self, user_id, wish_id): """ Get wish by wish_id """ user = User.query.filter_by(vk_id=user_id).first() if not user: user = User(vk_id=user_id) db.session.add(user) db.session.commit() else: abort(404) wish = Wish.query.filter_by(id=wish_id, user_id=user.id).first() if wish: return { "success": True, "message": errors.OK_MESSAGE, "result": wish.serialize }, 200 else: abort(404)
def delete(self, user_id, wish_id): """ Delete wish by wish_id """ user = User.query.filter_by(vk_id=user_id).first() if not user: user = User(vk_id=user_id) db.session.add(user) db.session.commit() else: abort(404) wish = Wish.query.filter_by(id=wish_id, user_id=user.id).first() if wish: db.session.delete(wish) db.session.commit() return { "success": True, "message": errors.OK_MESSAGE, }, 200 else: abort(404)
def signup(): form = SignupForm(request.form) if not current_user.is_authenticated: if request.method == 'POST' and form.validate(): user = User( username=form.username.data, password=form.password.data, role=None ) check_user = User.query.filter_by(username=user.username).first() if not check_user: db.session.add(user) db.session.commit() flash('Successful created a new user. You can login now.') return redirect(url_for('user.login')) else: flash('User already exists. Try another username') return render_template('signup.html', form=form) return render_template('signup.html', form=form) else: flash('You are already logged in. You need to logout first.') return redirect(url_for('home'))
def put(self, user_id, wish_id): """ Update wish by wish_id """ parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('description', type=str) parser.add_argument('link', type=str) args = parser.parse_args() user = User.query.filter_by(vk_id=user_id).first() if not user: user = User(vk_id=user_id) db.session.add(user) db.session.commit() else: abort(404) wish = Wish.query.filter_by(id=wish_id, user_id=user.id).first() if wish: if args['name']: wish.name = args['name'] if args['description']: wish.description = args['description'] if args['link']: wish.link = args['link'] db.session.commit() return { "success": True, "message": errors.OK_MESSAGE, "result": wish.serialize }, 200 else: abort(404)
def post(self, user_id): """ Add new wish """ parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('description', type=str) parser.add_argument('link', type=str) args = parser.parse_args() user = User.query.filter_by(vk_id=user_id).first() if not user: user = User(vk_id=args['user_id']) db.session.add(user) wish = Wish(text=args['name'], description=args['description'], link=args['link']) db.session.add(wish) user.wishes.append(wish) db.session.commit() return { "success": True, "message": errors.OK_MESSAGE, }, 200