Error occuring while sending test mail using flask-mail - python

I am basically building a flask web app which takes codeforces user id and returns user info
i am trying to add flask-mail functionality into it but it displays the
error:
TypeError: cannot unpack non-iterable NoneType object
The error is in the mail.send(msg) line
from flask import Flask, render_template,redirect,request
from flask_mail import Mail,Message
import requests
import json
import os
app = Flask(__name__)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'testdev582#gmail.com'
app.config['MAIL_PASSWORD'] = 'hardik1234'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
#print(app.config.update(mail_settings))
#app.config.update(mail_settings)
mail = Mail(app)
#app.route('/',methods=['GET','POST'])
def index():
if request.method == 'POST':
username = request.form['username']
email_id = request.form.get('email-id')
msg = Message("test",sender=app.config.get("MAIL_USERNAME"),recipients=[email_id])
mail.send(msg)
print(email_id)
#print(username)
url = 'http://codeforces.com/api/user.info?handles=' + username
wvurl = 'http://codeforces.com/api/contest.list?gym=false'
resp = requests.get(url)
r = requests.get(wvurl)
if resp and r:
user_data = json.loads(resp.text)['result']
contest_data = json.loads(r.text)['result']
for contest in contest_data:
if contest['phase'] == 'BEFORE':
break
print(contest['name'])
return render_template('show.html',user_data = user_data,contest_data = contest)
else:
print("Error in getting response")
return render_template('index.html')
return render_template('index.html')
if __name__=='__main__':
app.run(debug=True)

You will want to use os.environ
example:
app.config['MAIL_USERNAME'] = os.environ.get('EMAIL_USER', 'my#email.com')
app.config['MAIL_PASSWORD'] = os.environ.get('EMAIL_PASS', 'mypassword')
or set environment variables
windows:
https://www.youtube.com/watch?v=IolxqkL7cD8
linux:
https://www.youtube.com/watch?v=5iWhQWVXosU

You can try to check email_id if it is wrong.
I was just wrong there.

Related

Import Spotify song request

I would like to connect to spotify API and connect with OAuth and then search for a song a user requests.
I currently have a bot pulling for youtube and saving the file to play. I was wondering if there a way to do the same with Spotipy.
from flask import Flask, request, url_for, session, redirect
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
app = Flask(__name__)
app.secret_key = 'xxx'
app.config['SESSION_COOKIE_NAME'] = 'Token Cookie'
TOKEN_INFO = "token_info"
#app.route('/')
def login():
sp_oauth = create_spotify_oauth()
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
#app.route('/redirect')
def redirectPage():
sp_oauth = create_spotify_oauth()
session.clear()
code = request.args.get('code')
token_info = sp_oauth.get_access_token(code)
session[TOKEN_INFO] = token_info
return redirect(url_for('getTracks', _external=True))
#app.route('/getTracks')
def getTracks():
try:
token_info = get_token()
except:
print("user not logged in)")
redirect("/")
sp = spotipy.Spotify(auth = token_info["access_token"])
return
def get_token():
token_info = session.get(TOKEN_INFO, None)
if not token_info:
raise "execption"
now = int(time.time())
is_expired = token_info["expires_at"] - now < 60
if (is_expired):
sp_oauth = create_spotify_oauth()
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
return token_info
def create_spotify_oauth():
return SpotifyOAuth(
client_id = 'CLIENT_ID',
client_secret = 'CLIENT_SECRET',
redirect_uri = url_for('redirectPage', _external = True),
scope= 'user-library-read')

How to send an email with attachment using Python Flask /flask_mail

I recently started my adventure with python and only a couple weeks ago I discovered flask. I'm trying to send an email with output.csv file attached using flask. Output.csv has been saved as a result of submitting a html form from my form.html page.
myform.py
from flask import Flask, render_template, request
from flask_mail import Mail, Message
import csv
app = Flask(__name__)
app.config.update(dict(
DEBUG = True,
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 587,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = 'myemail#gmail.com',
MAIL_PASSWORD = '*****',
))
mail = Mail(app)
#app.route('/')
def myForm():
return render_template('form.html')
#app.route('/steptwo',methods = ['POST', 'GET'])
def steptwo():
if request.method == 'POST':
id = request.form['id']
name = request.form['name']
email = request.form['email']
website = request.form['website']
requestPhoneNr = request.form['requestPhoneNr']
requestTopic = request.form['requestTopic']
requestDescription = request.form['requestDescription']
requestOriginator = request.form['requestOriginator']
fieldnames = ['requestPhoneNr', 'requestTopic', 'requestDescription', 'requestOriginator']
with open('path/to/my/output.csv','a') as inFile:
writer = csv.DictWriter(inFile, fieldnames=fieldnames)
writer.writerow({'requestPhoneNr': requestPhoneNr, 'requestTopic': requestTopic, 'requestDescription': requestDescription, 'requestOriginator': requestOriginator})
msg = Message('Sample title', sender = 'myemai#gmail.com', recipients = ['myotheremail#gmail.com'])
msg.body = request.form['id'] + request.form['name'] + request.form['email'] + request.form['website']
with app.open_resource("path/to/my/output.csv") as fp:
msg.attach("path/to/my/output.csv", "text/csv", fp.read())
mail.send(msg)
return 'Thanks for your input!'
if __name__ == '__main__':
app.run(debug = True)
Solved!

Using Flask API, how to upload & download file from server using jwt_required decorator from browser/ client?

I suspect it has something got to do with refresh token. Could not understand how to use it by the docs. Can I know the exact code how to use it?
The access token is created during login:
#app.route('/login', methods=['POST','GET'])
def login():
username = request.form["email"]
password = request.form["password"]
my_token_expiry_time = datetime.timedelta(seconds=60)
segments = 0
access_token = None
if request.method == 'POST':
result_set = authenticate_user(username, password)
if result_set:
ss1 = select([nsettings]).\
where(nsettings.c.mattribute == 'my_jwt_expiry_time_min')
rss1 = g.conn.execute(ss1)
if rss1.rowcount > 0:
for r in rss1:
my_token_expiry_time = datetime.timedelta(seconds=
(int(r[nsettings.c.mvalue])* 60))
else:
my_token_expiry_time = datetime.timedelta(
seconds=(2 * 60 *60)) # 2 hours
#print result_set, 'result_set result_set'
session['email'] = result_set['email']
access_token = create_access_token(
identity=username, expires_delta=my_token_expiry_time)
user_dict = result_set
if user_dict:
session['email'] = user_dict['email']
session['id'] = result_set['id']
# users and related views
session['access_token'] = access_token
print access_token, 'aaaaaaaaaaa'
return jsonify({
'email': session['email'],
'user_id': result_set['id'],
'access_token': access_token,
'id': session['id'],
}), 200
else:
return jsonify({'message': "Invalid credentials, retry"}), 401
return "True"
The flask api call to upload:
#app.route('/rt/api/v1.0/issues/<int:issue_id>/documents', methods=['POST'])
#jwt_required
def rt_doc_upload(issue_id):
'''
Upload documents for a rt ticket.
'''
# Iterate through the list of files, we don't care about the
# attribute name. We consider only the first file and ignore the
# rest.
if 'id' in session:
uploader = "3"
minternal_only = True
bool_internal_update = False
msg_str = None
for attr, document in request.files.iteritems():
trans = g.conn.begin()
try:
orig_filename = document.filename
filename, upload_folder = check_or_insert_document(
orig_filename, uploader)
new_doc = add_doc(orig_filename, filename)
print orig_filename, 'origooooo'
ins = archival_docs.insert().values(new_doc)
rs = g.conn.execute(ins)
doc_id = rs.inserted_primary_key[0]
filename = (str(doc_id) + '_' + orig_filename)
stmt = archival_docs.update().values(stored_name=filename).\
where(archival_docs.c.id == doc_id)
g.conn.execute(stmt)
document.save(os.path.join(upload_folder, filename))
mattach_doc_id = genUrl(doc_id)
trans.commit()
return jsonify(
{'issue_doc_id': rs.inserted_primary_key[0]}), 201
except Exception, e:
print e
trans.rollback()
return jsonify({'message': "Did not find any file"}), 400
return jsonify({'message': "UNAUTHORIZED"}), 401
When used with runserver and on commenting the jwt_required decorator I am able to upload and download files
Using sqlalchemy core, python and flask. The api call to upload worked for more than a month, but suddenly stopped working now

OperationalError: (sqlite3.OperationalError) no such table: user

I'm completely new to flask and web development in general. And what I need is to login to a website using steam id. I'm doing it as it said here, but get the following error:
OperationalError: (sqlite3.OperationalError) no such table: user
It seems to open up steam website correctly but it breaks when I press Log In. So, what's my mistake ? Any help is appreciated.
The code:
from flask import Flask, render_template, redirect, session, json, g
from flask_bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.openid import OpenID
import urllib
import re
app = Flask(__name__)
app.secret_key = '123'
Bootstrap(app)
app.config.from_pyfile('settings.cfg')
db = SQLAlchemy(app)
oid = OpenID(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
steam_id = db.Column(db.String(40))
nickname = db.String(80)
#staticmethod
def get_or_create(steam_id):
rv = User.query.filter_by(steam_id=steam_id).first()
if rv is None:
rv = User()
rv.steam_id = steam_id
db.session.add(rv)
return rv
def get_steam_userinfo(steam_id):
options = {
'key': app.config['STEAM_API_KEY'],
'steamids': steam_id
}
url = 'http://api.steampowered.com/ISteamUser/' \
'GetPlayerSummaries/v0001/?%s' % urllib.urlencode(options)
rv = json.load(urllib.urlopen(url))
return rv['response']['players']['player'][0] or {}
_steam_id_re = re.compile('steamcommunity.com/openid/id/(.*?)$')
#app.route('/login')
#oid.loginhandler
def login():
if g.user is not None:
return redirect(oid.get_next_url())
return oid.try_login('http://steamcommunity.com/openid')
#oid.after_login
def create_or_login(resp):
match = _steam_id_re.search(resp.identity_url)
g.user = User.get_or_create(match.group(1))
steamdata = get_steam_userinfo(g.user.steam_id)
g.user.nickname = steamdata['personaname']
db.session.commit()
session['user_id'] = g.user.id
flash('You are logged in as %s' % g.user.nickname)
return redirect(oid.get_next_url())
#app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = User.query.get(session['user_id'])
#app.route('/')
def homepage():
return render_template('mainpage.html')
#app.route('/logout')
def logout():
session.pop('user_id', None)
return redirect(oid.get_next_url())
if __name__ == '__main__':
app.run(debug=True)
You need to run a db.create_all() before running your app.
This will create all the tables described by your model in the database.
If you are new to flask you can follow the quickstart quide here

Flask-Testing: issues with session management with unittests

Here is my route:
#blueprint.before_request
def load_session_from_cookie():
if request.endpoint != 'client.me':
try:
cookie = request.cookies.get(settings.COOKIE_NAME, None)
# if cookie does not exist, redirect to login url
if not cookie:
session.pop('accountId', None)
return redirect(settings.LOGIN_URL)
account = check_sso_cookie(cookie)
if 'accountId' in session:
return
elif 'accountId' in account:
session['accountId'] = account.get('accountId')
return
else:
session.pop('accountId', None)
return redirect(settings.LOGIN_URL)
except BadSignature:
session.pop('accountId', None)
return redirect(settings.LOGIN_URL)
#blueprint.route('/')
def home():
session.permanent = True
return render_template('index.html')
Here is my test:
from flask import Flask
from flask.ext.testing import TestCase
from api.client import blueprint
class TestInitViews(TestCase):
render_templates = False
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'sekrit!'
app.register_blueprint(blueprint)
return app
def setUp(self):
self.app = self.create_app()
self.cookie = '{ "account_id": 100 }'
def test_root_route(self):
resp = self.client.get("/")
self.assert_template_used('index.html')
def test_root_route_404(self):
res = self.client.get('/foo')
self.assertEqual(res.status_code, 404)
The problem is that the test test_root_route fails because a redirect happens because the session doesn't exist. I can't find any good resource online that shows how to incorporate session management with Flask-Tests... anyone have a good way of doing this?
You can make login request before:
def create_app(self):
...
app.config['TESTING'] = True # should off csrf
...
def test_root_route(self):
self.client.post(settings.LOGIN_URL, data={'login': 'l', 'password': 'p'})
resp = self.client.get('/')
self.assert_template_used('index.html')
For some difficult cases login route can be mocked.
Or manually set cookie:
def test_root_route(self):
resp = self.client.get('/', headers={'Cookie': 'accountId=test'})
self.assert_template_used('index.html')
Or set session with session transaction (http://flask.pocoo.org/docs/testing/#accessing-and-modifying-sessions):
def test_root_route(self):
with self.client.session_transaction() as session:
session['accountId'] = 'test'
resp = self.client.get('/')
self.assert_template_used('index.html')

Categories

Resources