Created
July 12, 2014 07:19
-
-
Save ragmha/fd7cbdd9c110bb876439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import Flask class from flask module | |
from flask import Flask, render_template, redirect, \ | |
url_for, request, session, flash | |
from functools import wraps | |
from flask.ext.sqlalchemy import SQLAlchemy | |
#creating the application object | |
app = Flask(__name__) | |
#config | |
import os | |
app.config.from_object(os.environ['APP_SETTINGS']) | |
#create the sqlalchemy | |
db = SQLAlchemy(app) | |
#import db schema | |
from models import * | |
# login required decorator | |
def login_required(f): | |
@wraps(f) | |
def wrap(*args, **kwargs): | |
if 'logged_in' in session: | |
return f(*args, **kwargs) | |
else: | |
flash('You need to login first.') | |
return redirect(url_for('log')) | |
return wrap | |
#use decorators to link the function to a url | |
@app.route('/') | |
@login_required | |
def home(): | |
posts = db.session.query(BlogPost).all() | |
return render_template('home.html',posts=posts) | |
#render template,assigning var posts => post and acess to that variable within template | |
@app.route('/about') | |
def about(): | |
return render_template('about.html')#render template | |
#route for handling the login page logic | |
@app.route('/log', methods=['GET', 'POST']) | |
def log(): | |
error = None | |
if request.method == 'POST': | |
if request.form['username'] != 'admin' or request.form['password'] != 'admin': | |
error = 'The Username/Password you entered is incorrect.PLease try again' | |
else: | |
session['logged_in'] = True | |
flash('You are logged in!') | |
return redirect(url_for('home')) | |
return render_template('log.html', error=error) | |
@app.route('/logout') | |
@login_required | |
def logout(): | |
session.pop('logged_in', None) | |
flash('You logged out!') | |
return redirect(url_for('home')) | |
# start the server with the 'run()' method | |
if __name__ == '__main__': | |
app.run(debug=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment