def recv(): message = request.values['m'] data = message.split(' ', 1) user = load_user(data[0]) resp = Response() resp.message("Forwarded to %s" % user.email if user else "User not found") return str(resp)
def login_handler(): form = LoginForm() if form.validate_on_submit(): email = form.email.data password = form.password.data user_email, user_name, user_pwd_hash = models.load_user( email) # Get user data from database if user_email is None: # User not found flash('User not present please register') return redirect(url_for('signup_handler')) elif not bcrypt.checkpw(password.encode('utf8'), user_pwd_hash.encode('utf8')): flash('Incorrect Password!') return redirect(url_for('login_handler')) else: session[ 'email'] = user_email # put email and name in session object session['name'] = user_name return redirect(url_for('home')) return render_template('login.html', form=form)
def cat_detail(cat_name): client = CatClient() attributes_to_keep = [ 'affection_level', 'child_friendly', 'dog_friendly', 'energy_level', 'grooming', 'hypoalergenic' ] image_result, breed_result = client.retrieve_cat_by_id(cat_name) ratings = dict() for key in breed_result[0].keys(): value = str(breed_result[0][key]) if value.isdigit() and key in attributes_to_keep: new_key = key.replace('_', ' ').capitalize() ratings[new_key] = (range(int(value)), range(5 - int(value))) #if type(image_result) == dict: # return render_template('movie_detail.html', error_msg=result['Error']) if len(image_result) == 0 or len(breed_result) == 0: return render_template('cat_detail.html', error_msg="error") picform = ProposePicForm() if picform.validate_on_submit(): temp = User.objects(username=current_user.username).first() msg = Message('Upload Request', sender='*****@*****.**', recipients=[str(temp.email)]) msg.body = "Thanks for requesting to upload an image to breed:" + str( cat_name) + "!\nYour image is attached to this email" msg.attach(picform.new_pic.data.filename, 'images/png', picform.new_pic.data.read()) mail.send(msg) msg = Message('Upload Request', sender='*****@*****.**', recipients=['*****@*****.**']) msg.body = "Someone is requesting to upload image to breed: " + str( cat_name) msg.attach(picform.new_pic.data.filename, 'images/png', picform.new_pic.data.read()) mail.send(msg) img = picform.new_pic.data filename = secure_filename(img.filename) pim = CatImage( commenter=load_user(current_user.username), date=current_time(), im=None, cat_name=cat_name, ) pim.save() pim.im.put(img.stream, content_type='images/png') pim.save() return redirect(url_for('features.cat_detail', cat_name=cat_name)) form = CatReviewForm() if form.validate_on_submit(): review = Review( commenter=load_user(current_user.username), content=form.text.data, date=current_time(), cat_name=cat_name, ) review.save() return redirect(request.path) reviews_m = Review.objects(cat_name=cat_name) reviews = [] for r in reviews_m: reviews.append({ 'date': r.date, 'username': r.commenter.username, 'content': r.content, 'image': images(r.commenter.username) }) return render_template('cat_detail.html', form=form, image=image_result[0], cat=breed_result[0], ratings=ratings, reviews=reviews, picform=picform)
def oauth_authorized(resp): """ Handles authentication after the user has authorized via Google. Sets up session variables with user's information. Creates a local user entry if necessary. :param resp: :return: """ next_url = flask.url_for('index') if resp is None: flask.flash(u'Request to sign in was denied by the user.', 'error') return flask.redirect(next_url) # Verify signed JSON Web Token and retrieve deserialized JSON in the JWT try: jwt = verify_id_token(resp['id_token'], GOOGLE_CLIENT_ID) except Oauth2clientError as err: flask.flash(u'Invalid token.', 'error') return flask.redirect(next_url) fmsg = None # Verify that the access token is valid for this app. if jwt.get('aud') != GOOGLE_CLIENT_ID: fmsg = make_flash_params( u"Token's client ID does not match app's.", 'error') if jwt.get('iss') not in GOOGLE_ISS: fmsg = make_flash_params(u"Invalid token issuer.", 'error') # Check is user is already signed in. stored_token = flask.session.get('access_token') stored_gplus_id = flask.session.get('gplus_id') if stored_token is not None and jwt.get('sub') == stored_gplus_id: fmsg = make_flash_params( u'Current user is already connected.', 'error') if fmsg is None: # No errors up to this point, user can be authenticated flask.session['name'] = jwt.get('name') flask.session['picture'] = jwt.get('picture') flask.session['email'] = jwt.get('email') flask.session['token_expires'] = jwt.get('exp') flask.session['access_token'] = resp['access_token'] # Get or create user user = models.load_user(flask.session) if user: # Mng logged in users with Flask-Login login_user(user, remember=True) fmsg = make_flash_params( u'You were signed in as %s.' % jwt.get('name')) else: reset_user_session_vars(flask.session) fmsg = make_flash_params( u'Error registering user %s in the database.' % jwt.get( 'name'), 'error') flask.flash(**fmsg) return flask.redirect(next_url)