# Path: pyfeedreader/forms/directoryforms.py
# class NewDirForm(Form):
#     name = TextField("name", [
#         validators.Required("You must enter a name."),
#     ])
# 
# class AddFeedDirForm(Form):
#     feed_id = IntegerField("feed_id", [
#         validators.Required("You must select a feed."),
#         ])
# 
#     dir_id = IntegerField("dir_id", [
#             validators.Required("You must select a directory."),
#         ])
# 
# Path: pyfeedreader/models/category.py
# class Category(Model):
#     """
#     The Category class represent a category that is a user named collection of feeds.
#     """
#     __tablename__ = "category"
#     id = Column('id', Integer, primary_key=True)
#     name = Column(String(128))
#     user_id = Column(Integer, ForeignKey('user.id'))
#     category_entries = relationship("CategoryEntry")
# 
#     def __init__(self, name=None, user_id=None):
#         self.name = name
#         self.user_id = user_id
# 
#     def __repr__(self):
#         return '<Category %r>' % self.username
# 
#     def get_feeds(self, db_session):
#         """
#         Creates the class variable feeds which is a list of the feed objects for this category.
# 
#         :param session: The current database session.
#         :return:
#         """
#         self.feeds = []
#         for entry in self.category_entries:
#             f = db_session.query(Feed).filter(Feed.id == entry.feed_id).first()
#             if f:
#                 self.feeds.append(Feed.unread(f, db_session))
# 
#     @staticmethod
#     def json(category):
#         """
#         Turns the category into a JSON ready dict.
# 
#         :param db_session: The current database session.
#         :param curent_user: The User object of the current logged in user.
#         :return: Returns a JSON ready dict of the Category object.
#         """
#         sum = 0
# 
#         for feed in category.feeds:
#             sum += feed.unread
# 
#         return {
#             "name": category.name,
#             "id": category.id,
#             "feeds": Feed.json_list(category.feeds),
#             "unread": sum,
#         }
# 
#     @staticmethod
#     def json_list(categories):
#         """
#         Turns a list on Category objects into a list of JSON ready Category objects.
# 
#         :param categories: The list of Category objects you want to make JSON ready.
#         :param db_session: The current database session.
#         :param current_user: The User object of the currently logged in user.
#         :return: Returns a list of JSON ready Category dicts.
#         """
#         json_ready = []
#         for category in categories:
#             json_ready.append(
#                 Category.json(category)
#             )
# 
#         return json_ready
# 
# Path: pyfeedreader/models/category_entry.py
# class CategoryEntry(Model):
#     __tablename__ = "category_entry"
#     id = Column('id', Integer, primary_key=True)
#     feed_id = Column(Integer)
#     category_id = Column(Integer, ForeignKey('category.id'))
# 
#     def __init__(self, feed_id=None, category_id=None):
#         self.feed_id = feed_id
#         self.category_id = category_id
# 
#     def __repr__(self):
#         return '<CategoryEntry %r>' % self.id
# 
# Path: pyfeedreader/models/userfeeds.py
# class UserFeeds(Model):
#     __tablename__ = "user_feeds"
#     id = Column('id', Integer, primary_key=True)
#     feed_id = Column(Integer)
#     user_id = Column(Integer, ForeignKey('user.id'))
# 
#     def __init__(self, feed_id, user_id):
#         self.feed_id = feed_id
#         self.user_id = user_id
# 
#     def __repr__(self):
#         return '<UserFeeds %r>' % self.username
# 
# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():


from pyfeedreader.forms.directoryforms import NewDirForm, AddFeedDirForm
from pyfeedreader.models.category import Category
from pyfeedreader.models.category_entry import CategoryEntry
from pyfeedreader.models.userfeeds import UserFeeds
from flask import *
from pyfeedreader.database import db_session
from flask.ext.login import *
    _dir = db_session.query(Directory).filter(Directory.user_id == current_user.id).\
        filter(Directory.name == form.name.data).first()
    if _dir:
        return jsonify(success=False, message="Directory already exists.")

    _dir = Directory(name=form.name.data, user_id=current_user.id)
    db_session.add(_dir)
    db_session.commit()

    return jsonify(success=True, message="Directory created.")

@mod.route("/directory/add/feed", methods=["POST"])
@login_required
def add_feed_dir():
    form = AddFeedDirForm(request.form)
    if not form.validate():
        return jsonify(success=False, message="Form not valid.")

    #Check if this user actually owns that directory.
    _dir = db_session.query(Directory).filter(Directory.id == form.dir_id.data).\
        filter(Directory.user_id == current_user.id).first()

    if not _dir:
        return jsonify(success=False, message="Cannot find this directory.")

    de = db_session.query(DirEntry).filter(DirEntry.feed_id == form.feed_id.data).\
        filter(DirEntry.dir_id == form.dir_id.data).first()
    if de:
        return jsonify(success=False, message="This feed is already in this directory.")


# Path: pyfeedreader/forms/directoryforms.py
# class NewDirForm(Form):
#     name = TextField("name", [
#         validators.Required("You must enter a name."),
#     ])
# 
# class AddFeedDirForm(Form):
#     feed_id = IntegerField("feed_id", [
#         validators.Required("You must select a feed."),
#         ])
# 
#     dir_id = IntegerField("dir_id", [
#             validators.Required("You must select a directory."),
#         ])
# 
# Path: pyfeedreader/models/category.py
# class Category(Model):
#     """
#     The Category class represent a category that is a user named collection of feeds.
#     """
#     __tablename__ = "category"
#     id = Column('id', Integer, primary_key=True)
#     name = Column(String(128))
#     user_id = Column(Integer, ForeignKey('user.id'))
#     category_entries = relationship("CategoryEntry")
# 
#     def __init__(self, name=None, user_id=None):
#         self.name = name
#         self.user_id = user_id
# 
#     def __repr__(self):
#         return '<Category %r>' % self.username
# 
#     def get_feeds(self, db_session):
#         """
#         Creates the class variable feeds which is a list of the feed objects for this category.
# 
#         :param session: The current database session.
#         :return:
#         """
#         self.feeds = []
#         for entry in self.category_entries:
#             f = db_session.query(Feed).filter(Feed.id == entry.feed_id).first()
#             if f:
#                 self.feeds.append(Feed.unread(f, db_session))
# 
#     @staticmethod
#     def json(category):
#         """
#         Turns the category into a JSON ready dict.
# 
#         :param db_session: The current database session.
#         :param curent_user: The User object of the current logged in user.
#         :return: Returns a JSON ready dict of the Category object.
#         """
#         sum = 0
# 
#         for feed in category.feeds:
#             sum += feed.unread
# 
#         return {
#             "name": category.name,
#             "id": category.id,
#             "feeds": Feed.json_list(category.feeds),
#             "unread": sum,
#         }
# 
#     @staticmethod
#     def json_list(categories):
#         """
#         Turns a list on Category objects into a list of JSON ready Category objects.
# 
#         :param categories: The list of Category objects you want to make JSON ready.
#         :param db_session: The current database session.
#         :param current_user: The User object of the currently logged in user.
#         :return: Returns a list of JSON ready Category dicts.
#         """
#         json_ready = []
#         for category in categories:
#             json_ready.append(
#                 Category.json(category)
#             )
# 
#         return json_ready
# 
# Path: pyfeedreader/models/category_entry.py
# class CategoryEntry(Model):
#     __tablename__ = "category_entry"
#     id = Column('id', Integer, primary_key=True)
#     feed_id = Column(Integer)
#     category_id = Column(Integer, ForeignKey('category.id'))
# 
#     def __init__(self, feed_id=None, category_id=None):
#         self.feed_id = feed_id
#         self.category_id = category_id
# 
#     def __repr__(self):
#         return '<CategoryEntry %r>' % self.id
# 
# Path: pyfeedreader/models/userfeeds.py
# class UserFeeds(Model):
#     __tablename__ = "user_feeds"
#     id = Column('id', Integer, primary_key=True)
#     feed_id = Column(Integer)
#     user_id = Column(Integer, ForeignKey('user.id'))
# 
#     def __init__(self, feed_id, user_id):
#         self.feed_id = feed_id
#         self.user_id = user_id
# 
#     def __repr__(self):
#         return '<UserFeeds %r>' % self.username
# 
# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():


# Path: pyfeedreader/views/directory.py
from pyfeedreader.forms.directoryforms import NewDirForm, AddFeedDirForm
from pyfeedreader.models.category import Category
from pyfeedreader.models.category_entry import CategoryEntry
from pyfeedreader.models.userfeeds import UserFeeds
from flask import *
from pyfeedreader.database import db_session
from flask.ext.login import *

    _dir = db_session.query(Directory).filter(Directory.user_id == current_user.id).\
        filter(Directory.name == form.name.data).first()
    if _dir:
        return jsonify(success=False, message="Directory already exists.")

    _dir = Directory(name=form.name.data, user_id=current_user.id)
    db_session.add(_dir)
    db_session.commit()

    return jsonify(success=True, message="Directory created.")

@mod.route("/directory/add/feed", methods=["POST"])
@login_required
def add_feed_dir():
    form = AddFeedDirForm(request.form)
    if not form.validate():
        return jsonify(success=False, message="Form not valid.")

    #Check if this user actually owns that directory.
    _dir = db_session.query(Directory).filter(Directory.id == form.dir_id.data).\
        filter(Directory.user_id == current_user.id).first()

    if not _dir:
        return jsonify(success=False, message="Cannot find this directory.")

    de = db_session.query(DirEntry).filter(DirEntry.feed_id == form.feed_id.data).\
        filter(DirEntry.dir_id == form.dir_id.data).first()
    if de:
        return jsonify(success=False, message="This feed is already in this directory.")


    result = db_session.query(UserFeeds).filter(UserFeeds.user_id == current_user.id).\