# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():
# 
# 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


from flask import *
from pyfeedreader.database import db_session
from flask.ext.login import *
from pyfeedreader.models.category import Category
from pyfeedreader.models.category_entry import CategoryEntry
    }
    :return:
    """
    try:
        data = json.loads(request.data)
    except ValueError:  # Validate JSON
        return jsonify(success=False, message="Bad JSON request.", link="")

    action = data.get("action")

    # Check if the category belongs to the user.
    category = db_session.query(Category).filter(Category.id == _id, Category.user_id == current_user.id).first()

    if not category:
        return jsonify(success=False, message="Unknown ID.", link="")

    # Rename the category action.
    if action == "rename":
        name = data.get("name")
        if not name:  # Need to check if name is set.
            return jsonify(success=False, message="Bad JSON request.", link="")

        category.name = name

    # Add a feed to the category.
    elif action == "add":
        feed_id = data.get("feed_id")
        if not feed_id:
            return jsonify(success=False, message="Bad JSON request.", link="")


# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():
# 
# 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/views/category.py
from flask import *
from pyfeedreader.database import db_session
from flask.ext.login import *
from pyfeedreader.models.category import Category
from pyfeedreader.models.category_entry import CategoryEntry

    }
    :return:
    """
    try:
        data = json.loads(request.data)
    except ValueError:  # Validate JSON
        return jsonify(success=False, message="Bad JSON request.", link="")

    action = data.get("action")

    # Check if the category belongs to the user.
    category = db_session.query(Category).filter(Category.id == _id, Category.user_id == current_user.id).first()

    if not category:
        return jsonify(success=False, message="Unknown ID.", link="")

    # Rename the category action.
    if action == "rename":
        name = data.get("name")
        if not name:  # Need to check if name is set.
            return jsonify(success=False, message="Bad JSON request.", link="")

        category.name = name

    # Add a feed to the category.
    elif action == "add":
        feed_id = data.get("feed_id")
        if not feed_id:
            return jsonify(success=False, message="Bad JSON request.", link="")


        db_session.add(CategoryEntry(feed_id, _id))