I am creating a short business directory web app with Flask & Firebase; I have managed to store the data collected but I also wanted the user to be able to upload their business photo; with what I have, when I store the photo with firebase.storage()
I can create a folder and image name as intended, but the file is empty. I keep trying different things - like where down the line I store the picture file but they all produce the same outcome - either an empty picture file or an error message.
My goal is when a user registers their details (along with uploading their photo), they can then login where they will have their details and their business picture on display.
python file
import pyrebase
from flask import *
app = Flask(__name__)
config = {
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"projectId": "",
"storageBucket": "",
"messagingSenderId": "",
"appId": "",
"measurementId": "",
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
db = firebase.database()
storage = firebase.storage()
@app.route('/', methods=['GET', 'POST'])
def login():
unsuccessful = 'Please check your credentials'
successful = 'Login successful'
if request.method == 'POST':
email = request.form.get('name')
password = request.form.get('pass')
try:
user = auth.sign_in_with_email_and_password(email, password)
registerList = db.child("Bame_Register").child("business").get(user['idToken']).val()
pic = storage.child("images/new.jpg").get_url(None)
return render_template('profile.html', registerList=registerList, pic=pic)
except:
return render_template('new.html', us=unsuccessful)
return render_template('new.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
unsuccessful = 'Please check your credentials'
successful = 'Registraion successful'
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('pass')
userName = request.form.get('inputName')
businessName = request.form.get('businessName')
startYear = request.form.get('startYear')
businessDescription = request.form.get('businessDescription')
web = request.form.get('web')
businessNumber = request.form.get('businessNumber')
businessEmail = request.form.get('businessEmail')
businessImage = request.files.get('businessImage')
try:
user = auth.create_user_with_email_and_password(email, password)
auth.send_email_verification(user['idToken'])
bameRegister = dict(
userName = userName,
businessName = businessName,
businessStartYear = startYear,
businessDescription = businessDescription,
businessURL = web,
businessNumber = businessNumber,
businessEmail = businessEmail,
businessImage = businessImage,
)
storage.child("images/new.jpg").put(businessImage, user['idToken'])
results = db.child("Bame_Register").child("business").update(bameRegister, user['idToken'])
return render_template('new.html', x=successful)
except:
return render_template('new.html', y=unsuccessful)
return render_template('new.html')
if __name__ == '__main__':
app.run(debug=True)
new.html
<!DOCTYPE html>
<html>
<head>
<title>Flask Firebase Auth</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
{% if s %}
<div class="alert alert-success">
<h2>{{s}}</h2>
</div>
{% endif %}
{% if us %}
<div class="alert alert-danger">
<h2>{{us}}</h2>
</div>
{% endif %}
<form action="/" method="post">
<h2>Please sign in</h2>
<label for="inputEmail">Email address</label>
<input type="email" id="inputEmail" name="name" placeholder="Email address" required autofocus>
<label for="inputPassword">Password</label>
<input type="password" id="inputPassword" name="pass" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<br>
<div class="container">
{% if x %}
<div class="alert alert-success">
<h2>{{x}}</h2>
</div>
{% endif %}
{% if y %}
<div class="alert alert-danger">
<h2>{{y}}</h2>
</div>
{% endif %}
<form action="{{ url_for('register') }}" method="post">
<h2>Please create an account to register your business</h2>
<label for="inputEmail">Email address</label>
<input type="email" id="inputEmail" name="email" placeholder="Email address" required autofocus>
<label for="inputPassword">Password</label>
<input type="password" id="inputPassword" name="pass" placeholder="Password" required><br>
<label for="inputName">Enter your name: </label>
<input type="text" id="inputName" name="inputName" placeholder="Name" required>
<label for="businessName">Business Name</label>
<input type="text" id="businessName" name="businessName" placeholder="Business Name" required><br>
<label for="startYear">Year buisness started:</label>
<input type="date" id="startYear" name="startYear" placeholder="2019"><br>
<textarea name="businessDescription" rows="8" cols="80" placeholder="Describe your business in a few sentences..." required></textarea><br>
<label for="web">Enter your business website (Optional):</label>
<input type="text" name="web" id="web" placeholder="Please start with https:// "><br>
<label for="businessNumber">Buisness number (Optional):</label>
<input type="number" name="businessNumber" id="businessNumber" placeholder="Enter landline or mobile number.."><br>
<label for="businessEmail">Buisness email (Optional):</label>
<input type="email" name="businessEmail" id="businessEmail" placeholder="Enter your business email"><br>
<label for="business_img">Select image:</label>
<input type="file" name="businessImage" accept="image/*">
<button class="btn btn-lg btn-primary btn-block" type="submit">Register business</button>
</form>
</div>
</body>
</html>
profile.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>{{ registerList['businessName'] }}</title>
</head>
<body>
<h1>{{ registerList['businessName'] }}</h1>
<h2>{{ registerList['userName'] }}</h2>
<div class="">
<img src="{{ pic }}">
</div>
<hr>
<p>Year business started: {{ registerList['businessStartYear'] }}</p>
<p>{{ registerList['businessDescription']}}</p><br>
<p>{{ registerList['businessURL'] }}</p>
<p>{{ registerList['businessEmail'] }}</p>
<p>{{ registerList['businessNumber'] }}</p>
</body>
</html>
If anyone can help me come up with a solution or point out to me where I'm going wrong that would be very much appreciated & grateful!
Thanks in advance :)