Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Python
"""
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():
    """Renders the home page."""
    return render_template(
        'index.html',
        title='Home Page',
        year=datetime.now().year,
    )

@app.route('/contact')
def contact():
    """Renders the contact page."""
    return render_template(
        'contact.html',
        title='Contact',
        year=datetime.now().year,
        message='Your contact page.'
    )

@app.route('/about')
def about():
    """Renders the about page."""
    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)),
        )

# create tables in database
metadata.create_all(checkfirst=True)

# create a database connection
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)
        # add raffle to database by executing SQL
        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))

# Try running this locally.
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.
Posted
Updated 26-Feb-16 6:11am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900