from flask import Flask, render_template, redirect
import psycopg2
import os
import urlparse

app = Flask(__name__)


# def connectDB(wrapped):
#     def inner(*args, **kwargs):
#         api_token = os.environ["API_TOKEN"]
#         urlparse.uses_netloc.append("postgres")
#         url = urlparse.urlparse(os.environ["DATABASE_URL"])
#         conn = psycopg2.connect(
#             database=url.path[1:],
#             user=url.username,
#             password=url.password,
#             host=url.hostname,
#             port=url.port
#         )
#         cur = conn.cursor()
#         ret = wrapped(*args, **kwargs)
#         return ret
#     return inner


@app.route('/')
def home():
    return render_template('home.html')


@app.route('/participants')
# @connectDB
def participants():
    return render_template('participants.html')


@app.route('/setup')
def setup():
    return render_template('setup.html')


@app.route('/register')
def register():
    return render_template('register.html')


@app.route('/complete', methods=['POST'])
# @connectDB
def complete():
    return redirect('/')

from flask import Flask, render_template, redirect, request
import psycopg2
from functools import wraps
import os
import urlparse


app = Flask(__name__)


def connectDB(wrapped):
    @wraps(wrapped)
    def inner(*args, **kwargs):
        urlparse.uses_netloc.append("postgres")
        url = urlparse.urlparse(os.environ["DATABASE_URL"])
        conn = psycopg2.connect(
            database=url.path[1:],
            user=url.username,
            password=url.password,
            host=url.hostname,
            port=url.port
        )
        cur = conn.cursor()
        return wrapped(cur, *args, **kwargs)
    return inner


@app.route('/')
def home():
    return render_template('home.html')


@app.route('/participants')
@connectDB
def participants(*args):
    return args[0]


@app.route('/setup')
def setup():
    return render_template('setup.html')


@app.route('/register')
def register():
    return render_template('register.html')


@app.route('/complete', methods=['POST'])
@connectDB
def complete(*args):
    return render_template('/success.html')

Add decorator to connect to database
Add decorator to connect to database

- from flask import Flask, render_template, redirect
+ from flask import Flask, render_template, redirect, request
  import psycopg2
+ from functools import wraps
  import os
  import urlparse
+ 
  
  app = Flask(__name__)
  
  
- # def connectDB(wrapped):
+ def connectDB(wrapped):
+     @wraps(wrapped)
- #     def inner(*args, **kwargs):
+     def inner(*args, **kwargs):
- #         api_token = os.environ["API_TOKEN"]
- #         urlparse.uses_netloc.append("postgres")
+         urlparse.uses_netloc.append("postgres")
- #         url = urlparse.urlparse(os.environ["DATABASE_URL"])
+         url = urlparse.urlparse(os.environ["DATABASE_URL"])
- #         conn = psycopg2.connect(
+         conn = psycopg2.connect(
- #             database=url.path[1:],
+             database=url.path[1:],
- #             user=url.username,
+             user=url.username,
- #             password=url.password,
+             password=url.password,
- #             host=url.hostname,
+             host=url.hostname,
- #             port=url.port
+             port=url.port
- #         )
+         )
- #         cur = conn.cursor()
+         cur = conn.cursor()
- #         ret = wrapped(*args, **kwargs)
+         return wrapped(cur, *args, **kwargs)
- #         return ret
- #     return inner
+     return inner
  
  
  @app.route('/')
  def home():
      return render_template('home.html')
  
  
  @app.route('/participants')
- # @connectDB
+ @connectDB
- def participants():
+ def participants(*args):
-     return render_template('participants.html')
+     return args[0]
  
  
  @app.route('/setup')
  def setup():
      return render_template('setup.html')
  
  
  @app.route('/register')
  def register():
      return render_template('register.html')
  
  
  @app.route('/complete', methods=['POST'])
- # @connectDB
+ @connectDB
- def complete():
+ def complete(*args):
-     return redirect('/')
+     return render_template('/success.html')
  
Add decorator to connect to database
## Code Before:
from flask import Flask, render_template, redirect
import psycopg2
import os
import urlparse

app = Flask(__name__)


# def connectDB(wrapped):
#     def inner(*args, **kwargs):
#         api_token = os.environ["API_TOKEN"]
#         urlparse.uses_netloc.append("postgres")
#         url = urlparse.urlparse(os.environ["DATABASE_URL"])
#         conn = psycopg2.connect(
#             database=url.path[1:],
#             user=url.username,
#             password=url.password,
#             host=url.hostname,
#             port=url.port
#         )
#         cur = conn.cursor()
#         ret = wrapped(*args, **kwargs)
#         return ret
#     return inner


@app.route('/')
def home():
    return render_template('home.html')


@app.route('/participants')
# @connectDB
def participants():
    return render_template('participants.html')


@app.route('/setup')
def setup():
    return render_template('setup.html')


@app.route('/register')
def register():
    return render_template('register.html')


@app.route('/complete', methods=['POST'])
# @connectDB
def complete():
    return redirect('/')

## Instruction:
Add decorator to connect to database
## Code After:
from flask import Flask, render_template, redirect, request
import psycopg2
from functools import wraps
import os
import urlparse


app = Flask(__name__)


def connectDB(wrapped):
    @wraps(wrapped)
    def inner(*args, **kwargs):
        urlparse.uses_netloc.append("postgres")
        url = urlparse.urlparse(os.environ["DATABASE_URL"])
        conn = psycopg2.connect(
            database=url.path[1:],
            user=url.username,
            password=url.password,
            host=url.hostname,
            port=url.port
        )
        cur = conn.cursor()
        return wrapped(cur, *args, **kwargs)
    return inner


@app.route('/')
def home():
    return render_template('home.html')


@app.route('/participants')
@connectDB
def participants(*args):
    return args[0]


@app.route('/setup')
def setup():
    return render_template('setup.html')


@app.route('/register')
def register():
    return render_template('register.html')


@app.route('/complete', methods=['POST'])
@connectDB
def complete(*args):
    return render_template('/success.html')
