I have Visual Studio Flask Python web project setup and I'm having trouble,using my own code to the default code.This is how my web should perform,user types there email enters how many entries they would like generates raffles,stores them into a db,and auto sends the raffle numbers to the email entered.the default code has 4 sections to it inside the template folder.i need help changing parts of the default code to my own code,the code i have done was done separately in python language,i need everything added to the web project expect the winner selection part.and for the record i have tried to do this myself but I'm getting errors after errors the program works as it should on my pc.heres the default code
"""
Routes and views for the flask application.
"""
from datetime import datetime
from flask import render_template
from FlaskWebProject10 import app
@app.route('/')
@app.route('/home')
def home()
return render_template(
'index.html',
title='Home Page',
year=datetime.now().year,
)
@app.route('/contact')
def contact()
return render_template(
'contact.html',
title='Contact',
year=datetime.now().year,
message='Your contact page.'
)
@app.route('/about')
def about()
return render_template(
'about.html',
title='About',
year=datetime.now().year,
message='Your application description page.'
)
heres my code that i have myself
from sqlalchemy import *
import random
engine = create_engine('sqlite:///raffle.db')
metadata = MetaData(bind=engine)
raffles_table = Table('raffles', metadata,
Column('id', Integer, primary_key=True),
Column('email', String(40)),
Column('raffle_color', String(40)),
Column('raffle_ticket', String(9)),
)
metadata.create_all(checkfirst=True)
conn = engine.connect()
def add_raffles():
email = raw_input("Please enter your email address:")
num_tickets = int(input("How many tickets do you want:"))
ticket_numbers = []
email_content=""
for i in range(num_tickets):
ins_raffle = raffles_table.insert()
COLORS = "Blue Pink Plum Aqua Navy Grey Rose Ruby Teal Gold Jade Lime".split()
NUM_COLORS = len(COLORS)
BASE = 10 ** 8
ticket = random.randrange(NUM_COLORS * BASE)
ticket_numbers.append(ticket)
color_index, rem = divmod(ticket, BASE)
color = COLORS[color_index]
num_a, num_b = divmod(rem, 10 ** 4)
ticket_ins = "{:04d} {:04d}".format(num_a, num_b)
new_raffle = ins_raffle.values(email = email, raffle_color = color, raffle_ticket = ticket_ins)
conn.execute(new_raffle)
formatted_string = "Your ticket is: {} {:04d} {:04d}".format(color, num_a, num_b);
print( formatted_string );
email_content += formatted_string + "\n"
print send_simple_message(email, 'Your Raffle Tickets', email_content)
def select_winner():
winner_query = raffles_table.select().order_by(func.random()).limit(1)
winner = conn.execute(winner_query)
for row in winner:
print("The winner is:" + row['email'])
raffle_color = row['raffle_color']
raffle_ticket = row['raffle_ticket']
print("The winning raffle is: {} {}".format(raffle_color, raffle_ticket))
print send_simple_message(
row['email'],
"The winner is:" + row['email'],
"The winning raffle is: {} {}".format(raffle_color, raffle_ticket))
import requests
def send_simple_message(to_address, subject, content):
api_key = "key-53915d904bf0999e1984129609f9927a"
sandbox_address = "https://api.mailgun.net/v3/sandbox9d3e17d42a2f40f18b697d70b52a7250.mailgun.org/messages"
return requests.post(
sandbox_address,
auth=("api", api_key),
data={"from": "osman.narnia@live.co.uk",
"to": [to_address],
"subject": subject,
"text": content})
if __name__ == '__main__':
add_raffles()
select_winner()
What I have tried:
replacing one of the four parts with my own code which didnt work now im here becuase really stuck.