Hi im not sure how to use the msg.body so it sends a generated raffle instead of he msg provided as shown below,the code i have does generate raffles in this format teal c2g4 h374. heres the part of the code which i think is responsible for this
part of the utils file
def assign_raffles_to_user(raffle_count, user):
from app import db
raffles = get_unused_raffles(raffle_count)
for raffle in random.sample(raffles, raffle_count):
print "Assigning {0} to {1}".format(raffle, user)
msg = Message('Raffle assigned', sender = 'osman.soloking009@outlook.com', recipients = [user.email])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
raffle.user = user
as im not sure how to make this right can someone give me clue to begin with. heres the app.py file
from flask import Flask, request, render_template, redirect, url_for
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from forms import RaffleForm
from models import db, get_db_uri, User, Raffle
from utils import assign_raffles_to_user, seed_raffles_into_db
from flask import Flask
from flask_mail import Mail
from mail import mail
from flask_mail import Message
app = Flask(__name__)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'osman33454@gmail.com'
app.config['MAIL_PASSWORD'] = 'erhvlcziftselwuy'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = get_db_uri()
app.config['SECRET_KEY'] = 'some-random-secret-key'
mail.init_app(app)
db.app = app
db.init_app(app)
db.create_all()
seed_raffles_into_db()
admin = Admin(app, name='raffles', template_mode='bootstrap3')
admin.add_view(ModelView(User, db.session))
admin.add_view(ModelView(Raffle, db.session))
@app.route('/', methods=['GET', 'POST'])
def home():
form = RaffleForm(request.form)
if request.method == 'POST' and form.validate():
email = form.email.data
# check if user exists
user = User.query.filter_by(email=email).all()
if not user:
user = User(email=email)
db.session.add(user)
else:
user = user[0]
# assign required raffles to user
assign_raffles_to_user(form.raffle_count.data, user)
return redirect(url_for('success'))
return render_template('home.html', form=form)
@app.route('/success', methods=['GET'])
def success():
return render_template('success.html')
if __name__ == '__main__':
app.run(debug=True)
heres the full utils.py
# utility functions go here
import random
import os
from uuid import uuid4
import constants
from models import Raffle, User
from flask_mail import Message
from flask_mail import Mail
from mail import mail
def generate_raffles(count):
for i in xrange(count):
colour = constants.COLORS[i % constants.COLORS_LEN]
uniq = uuid4().hex
uniq_p1, uniq_p2 = uniq[:4], uniq[4:8]
yield (colour, uniq_p1, uniq_p2)
def seed_raffles_into_db(max_raffles=constants.MAX_RAFFLES):
if is_inited():
print 'Raffles have already been seeded...'
return False
from app import db
print 'Seeding raffles...'
for raffle_colour, raffle_up1, raffle_up2 in generate_raffles(max_raffles):
raffle = Raffle(
colour=raffle_colour,
up1=raffle_up1,
up2=raffle_up2,
)
print "Adding", raffle
db.session.add(raffle)
db.session.commit()
mark_as_inited()
return True
def get_unused_raffles(raffle_count):
return (
Raffle.query.filter_by(
user=None
).limit(
constants.RAFFLE_PADDING + raffle_count
)
).all()
def mark_as_inited():
open(constants.INIT_FILE_PATH, 'w').close()
def is_inited():
return os.path.exists(constants.INIT_FILE_PATH)
def assign_raffles_to_user(raffle_count, user):
from app import db
raffles = get_unused_raffles(raffle_count)
for raffle in random.sample(raffles, raffle_count):
print "Assigning {0} to {1}".format(raffle, user)
msg = Message('Raffle assigned', sender = 'osman.soloking009@outlook.com', recipients = [user.email])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
raffle.user = user
db.session.commit()
return True
Aucun commentaire:
Enregistrer un commentaire