from flask import render_template, flash, redirect, session, url_for, request, g
from flask.ext.login import login_user, logout_user, current_user, login_required
from app import app, db, lm
from forms import LoginForm, RegisterForm
from models import User

@app.before_request
def before_request():
    g.user = current_user

@lm.user_loader
def load_user(id):
    return User.query.get(int(id))

@app.route('/')
@app.route('/index')
@login_required
def index():
    user = g.user

    return render_template('index.html',
                           title='Strona glowna',
                           user=user)

@app.route('/login', methods=['GET', 'POST'])
def login():
    if g.user is not None and g.user.is_authenticated():
        return redirect(url_for('index'))

    form = LoginForm()
    if form.validate_on_submit():
        session['remember_me'] = form.remember_me.data

        user = User.query.filter_by(email=form.email.data).first()
        if user is None:
          flash(gettext('User %(email)s not found.', email=email))
          return redirect(url_for('index'))

        remember_me = False
        if 'remember_me' in session:
            remember_me = session['remember_me']
            session.pop('remember_me', None)
        login_user(user, remember=remember_me)

        return redirect('/index')

    return render_template('login.html',
                           title='Logowanie',
                           form=form)

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/register', methods=['GET', 'POST'])
def register():
    if g.user is not None and g.user.is_authenticated():
        return redirect(url_for('index'))

    form = RegisterForm()
    if form.validate_on_submit():
        user = User(email=form.email.data)
        user.hash_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        return redirect('/index')

    return render_template('register.html',
                           title='Rejestracja',
                           form=form)
@@ -293,18 +293,16 @@
 uest():%0A
-  
   g.user
@@ -353,18 +353,16 @@
 er(id):%0A
-  
   return
@@ -453,18 +453,16 @@
 ndex():%0A
-  
   user =
@@ -470,18 +470,16 @@
 g.user%0A%0A
-  
   return
@@ -530,26 +530,24 @@
             
- 
- 
 title='Stron
@@ -553,26 +553,24 @@
 na glowna',%0A
-  
             
@@ -649,26 +649,24 @@
 ef login():%0A
-  
   if g.user 
@@ -700,36 +700,32 @@
 uthenticated():%0A
-    
     return redir
@@ -745,26 +745,24 @@
 index'))%0A%0A  
- 
- 
 form = Login
@@ -764,26 +764,24 @@
 LoginForm()%0A
-  
   if form.va
@@ -800,28 +800,24 @@
 bmit():%0A    
- 
-   
 session%5B'rem
@@ -852,20 +852,16 @@
 e.data%0A%0A
-    
     user
@@ -919,20 +919,16 @@
 t()%0A    
-  
-  
 if user 
@@ -946,41 +946,32 @@
     
-    
 flash(
-gettext('User %25(
+'User with %7B
 email
-)s
+%7D
  not
@@ -982,21 +982,37 @@
 nd.'
-, email=email
+.format(email=form.email.data
 ))%0A 
@@ -1012,28 +1012,24 @@
 ata))%0A      
- 
-   
 return redir
@@ -1051,20 +1051,16 @@
 dex'))%0A%0A
-    
     reme
@@ -1079,20 +1079,16 @@
 lse%0A    
-   
- 
 if 'reme
@@ -1108,20 +1108,16 @@
 ession:%0A
-    
         
@@ -1153,20 +1153,16 @@
 er_me'%5D%0A
-    
         
@@ -1194,20 +1194,16 @@
 , None)%0A
-    
     logi
@@ -1238,20 +1238,16 @@
 er_me)%0A%0A
-    
     retu
@@ -1263,34 +1263,32 @@
 ct('/index')%0A%0A  
- 
- 
 return render_te
@@ -1329,26 +1329,24 @@
             
-  
 title='Logow
@@ -1352,18 +1352,16 @@
 wanie',%0A
-  
         
@@ -1425,18 +1425,16 @@
 gout():%0A
-  
   logout
@@ -1441,18 +1441,16 @@
 _user()%0A
-  
   return
@@ -1543,18 +1543,16 @@
 ster():%0A
-  
   if g.u
@@ -1598,28 +1598,24 @@
 ated():%0A    
-   
- 
 return redir
@@ -1637,18 +1637,16 @@
 dex'))%0A%0A
-  
   form =
@@ -1663,18 +1663,16 @@
 orm()%0A  
- 
- 
 if form.
@@ -1693,20 +1693,16 @@
 bmit():%0A
-    
     user
@@ -1728,28 +1728,24 @@
 email.data)%0A
-    
     user.has
@@ -1775,20 +1775,16 @@
 d.data)%0A
-    
     db.s
@@ -1804,20 +1804,16 @@
 er)%0A    
- 
-   
 db.sessi
@@ -1828,20 +1828,16 @@
 t()%0A    
- 
-   
 return r
@@ -1855,18 +1855,16 @@
 ndex')%0A%0A
-  
   return
@@ -1918,26 +1918,24 @@
             
-  
 title='Rejes
@@ -1939,26 +1939,24 @@
 jestracja',%0A
-  
             

handle not available user. better indentation of code