# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():
# 
# Path: pyfeedreader/models/user.py
# class User(Model):
#     __tablename__ = "user"
#     id = Column('id', Integer, primary_key=True)
#     email = Column(String(256))
#     password = Column(Text)
#     current_login = Column(Integer)
#     last_login = Column(Integer)
#     username = Column(String(100))
#     categories = relationship("Category")
#     feeds = relationship("UserFeeds")
#     category_entries = relationship("UserFeeds")
# 
#     def __init__(self, email=None, password=None, current_login=None, last_login=None, username=None,
#                  categories=[], feeds=[], read_ents=[]):
#         self.email = email
#         self.password = password
#         self.current_login = current_login
#         self.last_login = last_login
#         self.username = username
#         self.categories = categories
#         self.feeds = feeds
#         self.read_ents = read_ents
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return unicode(self.id)
# 
#     def q_categories(self, session):
#         """
#         Dont remember what this function is for
#         TODO fix this.
#         :param session:
#         :return:
#         """
#         for category in self.categories:
#             category.get_feeds(session)
# 
#     def get_feeds(self, session):
#         temp_list = []
#         for uf in self.feeds:
#             f = session.query(Feed).filter(Feed.id == uf.feed_id).first()
#             if f:
#                 temp_list.append(f)
# 
#         self.rfeed = temp_list
# 
#     def feed_entities(self, session):
#         ids = []
#         for feed in self.feeds:
#             ids.append(feed.feed_id)
# 
#         for category in self.categories:
#             for category_entry in category.category_entries:
#                 ids.append(category_entry.feed_id)
# 
#         self.entries = session.query(Entry).filter(Entry.feed_id.in_(ids)).order_by(desc(Entry.updated)).all()
# 
#         for entry in self.entries:
#             entry.unread = True
#             for read_ent in self.read_ents:
#                 if entry.id == read_ent.entry_id:
#                     entry.unread = False
# 
#     def get_entries(self, session, offset, limit):
#         self.get_feeds(session)
# 
#         ids = []
#         for feed in self.feeds:
#             ids.append(feed.feed_id)
# 
#         query = session.query(Entry).filter(Entry.feed_id.in_(ids)).order_by(desc(Entry.updated)).limit(limit).offset(
#             offset)
# 
#         return query.all()
# 
#     def __repr__(self):
#         return '<user %r>' % self.username
# 
# Path: pyfeedreader/forms/registrationform.py
# class RegistrationForm(Form):
#     username = TextField('Username', [validators.Length(min=4, max=25)])
#     email = TextField('E-Mail Address', [
#         validators.Length(min=6, max=100),
#         validators.Required(),
#         validators.EqualTo("emailc", message="E-Mails must match.")
#     ])
# 
#     emailc = TextField("Repeate E-Mail", [
#         validators.Length(min=6, max=100),
#         validators.Required(),
#         validators.EqualTo("email", message="")
#     ])
# 
#     password = PasswordField('New Password', [
#         validators.Length(min=6, max=1024),
#         validators.Required(),
#         validators.EqualTo('passwc', message='Passwords must match.')
#     ])
# 
#     passwc = PasswordField('Repeat Password', [
#         validators.Length(min=6, max=1024),
#         validators.Required(),
#         validators.EqualTo('password', message="")
#     ])


from flask import *
from pyfeedreader.database import db_session
from pyfeedreader.models.user import User
from flask.ext.login import *
from pyfeedreader.forms.registrationform import RegistrationForm
import bcrypt
import time

    result = User.query.filter(User.username == form.username.data).first()
    if result:
        error = True
        form.username.errors = ["Username already in use."]

    result = User.query.filter(User.email == form.email.data).first()
    if result:
        error = True
        form.email.errors = ["E-Mail already in use."]

    if error:
        return render_template("createaccount.html", form=form)

    hash = bcrypt.hashpw(form.password.data, bcrypt.gensalt())

    user = User(email=form.email.data, password=hash, current_login=time.time(),
                last_login=time.time(), username=form.username.data)

    db_session.add(user)
    db_session.commit()

    #Login the user.
    login_user(user)

    return redirect("/")


@mod.route("/register", methods=["GET"])
def display_ca_page():

# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():
# 
# Path: pyfeedreader/models/user.py
# class User(Model):
#     __tablename__ = "user"
#     id = Column('id', Integer, primary_key=True)
#     email = Column(String(256))
#     password = Column(Text)
#     current_login = Column(Integer)
#     last_login = Column(Integer)
#     username = Column(String(100))
#     categories = relationship("Category")
#     feeds = relationship("UserFeeds")
#     category_entries = relationship("UserFeeds")
# 
#     def __init__(self, email=None, password=None, current_login=None, last_login=None, username=None,
#                  categories=[], feeds=[], read_ents=[]):
#         self.email = email
#         self.password = password
#         self.current_login = current_login
#         self.last_login = last_login
#         self.username = username
#         self.categories = categories
#         self.feeds = feeds
#         self.read_ents = read_ents
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return unicode(self.id)
# 
#     def q_categories(self, session):
#         """
#         Dont remember what this function is for
#         TODO fix this.
#         :param session:
#         :return:
#         """
#         for category in self.categories:
#             category.get_feeds(session)
# 
#     def get_feeds(self, session):
#         temp_list = []
#         for uf in self.feeds:
#             f = session.query(Feed).filter(Feed.id == uf.feed_id).first()
#             if f:
#                 temp_list.append(f)
# 
#         self.rfeed = temp_list
# 
#     def feed_entities(self, session):
#         ids = []
#         for feed in self.feeds:
#             ids.append(feed.feed_id)
# 
#         for category in self.categories:
#             for category_entry in category.category_entries:
#                 ids.append(category_entry.feed_id)
# 
#         self.entries = session.query(Entry).filter(Entry.feed_id.in_(ids)).order_by(desc(Entry.updated)).all()
# 
#         for entry in self.entries:
#             entry.unread = True
#             for read_ent in self.read_ents:
#                 if entry.id == read_ent.entry_id:
#                     entry.unread = False
# 
#     def get_entries(self, session, offset, limit):
#         self.get_feeds(session)
# 
#         ids = []
#         for feed in self.feeds:
#             ids.append(feed.feed_id)
# 
#         query = session.query(Entry).filter(Entry.feed_id.in_(ids)).order_by(desc(Entry.updated)).limit(limit).offset(
#             offset)
# 
#         return query.all()
# 
#     def __repr__(self):
#         return '<user %r>' % self.username
# 
# Path: pyfeedreader/forms/registrationform.py
# class RegistrationForm(Form):
#     username = TextField('Username', [validators.Length(min=4, max=25)])
#     email = TextField('E-Mail Address', [
#         validators.Length(min=6, max=100),
#         validators.Required(),
#         validators.EqualTo("emailc", message="E-Mails must match.")
#     ])
# 
#     emailc = TextField("Repeate E-Mail", [
#         validators.Length(min=6, max=100),
#         validators.Required(),
#         validators.EqualTo("email", message="")
#     ])
# 
#     password = PasswordField('New Password', [
#         validators.Length(min=6, max=1024),
#         validators.Required(),
#         validators.EqualTo('passwc', message='Passwords must match.')
#     ])
# 
#     passwc = PasswordField('Repeat Password', [
#         validators.Length(min=6, max=1024),
#         validators.Required(),
#         validators.EqualTo('password', message="")
#     ])


# Path: pyfeedreader/views/createaccount.py
from flask import *
from pyfeedreader.database import db_session
from pyfeedreader.models.user import User
from flask.ext.login import *
from pyfeedreader.forms.registrationform import RegistrationForm
import bcrypt
import time


    result = User.query.filter(User.username == form.username.data).first()
    if result:
        error = True
        form.username.errors = ["Username already in use."]

    result = User.query.filter(User.email == form.email.data).first()
    if result:
        error = True
        form.email.errors = ["E-Mail already in use."]

    if error:
        return render_template("createaccount.html", form=form)

    hash = bcrypt.hashpw(form.password.data, bcrypt.gensalt())

    user = User(email=form.email.data, password=hash, current_login=time.time(),
                last_login=time.time(), username=form.username.data)

    db_session.add(user)
    db_session.commit()

    #Login the user.
    login_user(user)

    return redirect("/")


@mod.route("/register", methods=["GET"])
def display_ca_page():

    form = RegistrationForm(request.form)