I've created a form with an optional WTForms File field to upload a file. The form works... but after submission, my view.py code is always trying to process the uploaded file, regardless of whether it was submitted or not.
How do I determine if a file has been uploaded or not? I'd like my code to only process the upload if something was uploaded.
Right now, I haven't found the correct way to validate, so my code is handling the uploaded file, even when no file has been uploaded.
I'm currently trying this in my views.py to distinguish, but it doesn't work (see below):
attachFile = False
if attachment:
attachFile = True
I've also tried the following to try to make something happen (these are commented out in the full code in views.py):
First attempt: if form.attachment.data is not str:
Second attempt: if not attachment.filename == "":
Third attempt: if (isinstance(attachment,str) == False):
(Fourth (and current) attempt is above)
I've also tried the following, but get the following error when a file is not uploaded:
if attachment.data:
attachFile = True
## AttributeError: 'str' object has no attribute 'data'
forms.py:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField,
SubmitField, TextAreaField, FileField
from wtforms.fields.html5 import EmailField
from wtforms.validators import DataRequired, Email
class MailerForm(FlaskForm):
fromName = StringField('fromName', validators=[DataRequired()])
fromEmail = EmailField('fromEmail', validators=[DataRequired(), Email()])
subject = StringField('Subject', validators=[DataRequired()])
toAddress = TextAreaField('toAddress', validators=[DataRequired()])
message = TextAreaField('message', validators=[DataRequired()])
attachment = FileField('attachment')
submit = SubmitField('Send Email')
views.py
@app.route('/mailer/', methods=['GET','POST'])
def mailer():
# compiled regex to quick and dirty email validation
EMAIL_REGEX = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
form = MailerForm()
if form.validate_on_submit():
fromName = form.fromName.data
fromEmail = form.fromEmail.data
subject = form.subject.data
toAddress = form.toAddress.data
messageBody = form.message.data
attachment = form.attachment.data
newFileName = ""
attachFile = False
if attachment:
attachFile = True
basedir = os.path.abspath(os.path.dirname(__file__))
## lists to track successful and unsuccessful email addresses submitted
success = []
failure = []
##
## split email address
##
addresses = toAddress.split("\n")
##
## iterate through email addresses, validate, and send
##
for address in addresses:
address = address.strip()
if EMAIL_REGEX.match(address):
##if (isinstance(attachment,str) == False):
##if not attachment.filename == "":
if attachFile == True:
filename = os.path.join(basedir + "/static/" + app.config['UPLOAD_FOLDER'], attachment.filename)
attachment.save(filename)
msg = Message(subject)
msg.sender = (fromName,fromEmail)
msg.recipients = [address]
msg.body = messageBody
#if form.attachment.data is not str:
#if not attachment.filename == "":
#if (isinstance(attachment,str) == False):
if attachFile == True:
newFileName = attachment.filename
with app.open_resource(filename) as fp:
msg.attach(
newFileName,
"application/octet-stream",
fp.read())
mail.send(msg)
success.append(address)
else:
failure.append(address)
print("Failed:" + address)
else:
"""Renders index page."""
return render_template(
'mailer/mailer.html',
form = form
)
##
## Successfully emailed, time to nuke the temp attachment
##
os.system('rm ' + basedir + "/static/" + app.config['UPLOAD_FOLDER'] + "/'" + newFileName + "'")
##
##
##
return render_template(
'mailer/mailerCompleted.html',
form = form,
success = success,
failure = failure
)