# Path: models.py
# class User(Base):
#     validation_errors = None
#     __tablename__ = 'users'
#     __table_args__ = {'schema': 'mineturer'}
#     id = Column('userid', Integer, primary_key=True)
#     username = Column('username', String(20), unique=True, index=True)
#     bcrypt_pwd = Column('bcrypt_pwd', String)
#     fullname = Column('fullname', String(50))
#     enabled = Column('enabled', Boolean)
#     email = Column('email', String(50), unique=True, index=True)
#     trips = relationship(
#         'Trip',
#         backref='mineturer.user',
#         order_by='desc(Trip.start)'
#     )
# 
#     def __init__(self, username=None, password=None, password2=None,
#                  email=None, fullname=None):
#         self.validation_errors = {}
#         self.username = username
#         self.fullname = fullname
#         self.set_password(password, password2)
#         self.email = email
# 
#     def set_password(self, password, password2):
#         if not self.validation_errors:
#             self.validation_errors = {}
# 
#         if not password and not self.id:
#             self.validation_errors['password'] = 'Fyll inn passord'
#         if not password2 and not self.id:
#             self.validation_errors['password2'] = 'Sett passord igjen'
#         if password != password2:
#             self.validation_errors['password2'] = 'Passordene er ulike'
#         if self.validation_errors:
#             return
# 
#         if password:
#             sha1_hashed = hashlib.sha1(password).hexdigest()
#             self.bcrypt_pwd = bcrypt.hashpw(sha1_hashed, bcrypt.gensalt())
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return self.enabled
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return unicode(self.id)
# 
#     def password_ok(self, password):
# 
#         sha1_hashed = hashlib.sha1(password).hexdigest()
# 
#         bcrypt_hashed = bcrypt.hashpw(
#             sha1_hashed,
#             self.bcrypt_pwd.encode('utf-8')
#         )
#         return bcrypt_hashed == self.bcrypt_pwd
# 
#     def validate(self):
# 
#         if not self.validation_errors:
#             self.validation_errors = {}
# 
#         if not self.id and not self.username:
#             self.validation_errors['username'] = 'Fyll inn brukernavn'
# 
#         if not self.id and not self.fullname:
#             self.validation_errors['fullname'] = 'Fyll inn navn'
# 
#         if not self.id and not self.email:
#             self.validation_errors['email'] = 'Fyll inn epost'
# 
#         if not self.id and get_user_by_username(self.username):
#             self.validation_errors['username'] = 'Brukernavnet er opptatt'
# 
#         if self.validation_errors:
#             return False
#         return True
# 
#     def __repr__(self):
#         return '<User %r>' % (self.username)
# 
# def get_user_by_username(username):
#     return User.query.filter_by(
#         username=username,
#     ).first()


from flask import (redirect, url_for, request, render_template, current_app,
                   flash)
from flask.ext.login import (LoginManager, logout_user, login_user,
                             current_user)
from models import User, get_user_by_username
                'register.html',
                errors=user.validation_errors,
                user=user
            )

    @app.route('/login', methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        username = request.form['username']
        password = request.form['password']

        remember_me = False
        if 'remember_me' in request.form:
            remember_me = True

        user = get_user_by_username(username)

        if user is None or not user.password_ok(password):
            return redirect(url_for('login'))
        login_user(user, remember=remember_me)
        return redirect(request.args.get('next') or url_for('trips'))

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

    @login_manager.user_loader
    def load_user(id):

# Path: models.py
# class User(Base):
#     validation_errors = None
#     __tablename__ = 'users'
#     __table_args__ = {'schema': 'mineturer'}
#     id = Column('userid', Integer, primary_key=True)
#     username = Column('username', String(20), unique=True, index=True)
#     bcrypt_pwd = Column('bcrypt_pwd', String)
#     fullname = Column('fullname', String(50))
#     enabled = Column('enabled', Boolean)
#     email = Column('email', String(50), unique=True, index=True)
#     trips = relationship(
#         'Trip',
#         backref='mineturer.user',
#         order_by='desc(Trip.start)'
#     )
# 
#     def __init__(self, username=None, password=None, password2=None,
#                  email=None, fullname=None):
#         self.validation_errors = {}
#         self.username = username
#         self.fullname = fullname
#         self.set_password(password, password2)
#         self.email = email
# 
#     def set_password(self, password, password2):
#         if not self.validation_errors:
#             self.validation_errors = {}
# 
#         if not password and not self.id:
#             self.validation_errors['password'] = 'Fyll inn passord'
#         if not password2 and not self.id:
#             self.validation_errors['password2'] = 'Sett passord igjen'
#         if password != password2:
#             self.validation_errors['password2'] = 'Passordene er ulike'
#         if self.validation_errors:
#             return
# 
#         if password:
#             sha1_hashed = hashlib.sha1(password).hexdigest()
#             self.bcrypt_pwd = bcrypt.hashpw(sha1_hashed, bcrypt.gensalt())
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return self.enabled
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return unicode(self.id)
# 
#     def password_ok(self, password):
# 
#         sha1_hashed = hashlib.sha1(password).hexdigest()
# 
#         bcrypt_hashed = bcrypt.hashpw(
#             sha1_hashed,
#             self.bcrypt_pwd.encode('utf-8')
#         )
#         return bcrypt_hashed == self.bcrypt_pwd
# 
#     def validate(self):
# 
#         if not self.validation_errors:
#             self.validation_errors = {}
# 
#         if not self.id and not self.username:
#             self.validation_errors['username'] = 'Fyll inn brukernavn'
# 
#         if not self.id and not self.fullname:
#             self.validation_errors['fullname'] = 'Fyll inn navn'
# 
#         if not self.id and not self.email:
#             self.validation_errors['email'] = 'Fyll inn epost'
# 
#         if not self.id and get_user_by_username(self.username):
#             self.validation_errors['username'] = 'Brukernavnet er opptatt'
# 
#         if self.validation_errors:
#             return False
#         return True
# 
#     def __repr__(self):
#         return '<User %r>' % (self.username)
# 
# def get_user_by_username(username):
#     return User.query.filter_by(
#         username=username,
#     ).first()


# Path: login_views.py
from flask import (redirect, url_for, request, render_template, current_app,
                   flash)
from flask.ext.login import (LoginManager, logout_user, login_user,
                             current_user)
from models import User, get_user_by_username

                'register.html',
                errors=user.validation_errors,
                user=user
            )

    @app.route('/login', methods=['GET', 'POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        username = request.form['username']
        password = request.form['password']

        remember_me = False
        if 'remember_me' in request.form:
            remember_me = True

        user = get_user_by_username(username)

        if user is None or not user.password_ok(password):
            return redirect(url_for('login'))
        login_user(user, remember=remember_me)
        return redirect(request.args.get('next') or url_for('trips'))

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

    @login_manager.user_loader
    def load_user(id):

        return User.query.get(int(id))