# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():
# 
# Path: pyfeedreader/models/ReadEntries.py
# class ReadEntry(Model):
#     __tablename__ = "read_entry"
#     id = Column('id', Integer, primary_key=True)
#     entry_id = Column(Integer)
#     user_id = Column(Integer, ForeignKey('user.id'))
# 
#     def __init__(self, entry_id, user_id):
#         self.entry_id = entry_id
#         self.user_id = user_id
# 
#     def __repr__(self):
#         return '<ReadEntry %r>' % self.username
# 
# Path: pyfeedreader/models/entry.py
# class Entry(Model):
#     __tablename__ = "entry"
#     id = Column('id', Integer, primary_key=True)
#     feed_id = Column(Integer)
#     published = Column(Integer)
#     updated = Column(Integer)
#     title = Column(String(1024))
#     content = Column(Text)
#     description = Column(String(256))
#     link = Column(String(1024))
#     remote_id = Column(String(1024))
# 
#     def __init__(self, feed_id=None, published=None, updated=None, title=None, content=None, description=None,
#                  link=None, remote_id=None, unread=True):
#         self.feed_id = feed_id
#         self.published = published
#         self.updated = updated
#         self.title = title
#         self.content = content
#         self.description = description
#         self.link = link
#         self.remote_id = remote_id
#         self.unread = unread
# 
#     @staticmethod
#     def json_entry(entry, db_session, user_id):
#         """
#         Turns the entry object into a JSON ready dict object.
# 
#         :param entry: The entry you want to make JSON ready.
# 
#         :return: The JSON ready dict.
#         """
# 
#         read_entry = db_session.query(ReadEntry).filter(ReadEntry.entry_id == entry.id,
#                                                         ReadEntry.user_id == user_id).first()
#         unread = read_entry is None
# 
#         return {
#             "entry_id": entry.id,
#             "feed_id": entry.feed_id,
#             "published": entry.published,
#             "updated": entry.updated,
#             "title": entry.title,
#             "content": entry.content,
#             "description": entry.description,
#             "link": entry.link,
#             "remote_id": entry.remote_id,
#             "unread": unread,
#         }
# 
#     @staticmethod
#     def json_list(entries, db_session, user_id):
#         """
#         Turns a list of entries into JSON ready entries.
# 
#         :param entries: The list of entries you want to turn into JSON ready dicts.
# 
#         :return: List of JSON ready dicts.
#         """
# 
#         json_ready = []
#         for entry in entries:
#             json_ready.append(
#                 Entry.json_entry(entry, db_session, user_id)
#             )
# 
#         return json_ready


from flask import *
from pyfeedreader.database import db_session
from flask.ext.login import *
from sqlalchemy import desc
from pyfeedreader.models.ReadEntries import ReadEntry
from pyfeedreader.models.entry import Entry
import json

def delete_entries_feed():
    abort(400)


def get_entries_feed(feed_id):
    """
    A GET request should return all entries for that feed, this request will have a limit and offset.

    :param feed_id: The ID of the feed.
    :return:
    """
    try:
        limit = int(request.args.get("limit"))
        offset = int(request.args.get("offset"))
    except (ValueError, TypeError):
        return jsonify(success=False, message="Invalid limit and or offset.")

    if limit is None or offset is None:
        return jsonify(success=False, message="Invalid limit and or offset.")

    if limit < 0 or offset < 0:
        return jsonify(success=False, message="Invalid limit and or offset.")

    if limit > 200:
        return jsonify(success=False, message="Too big range.")

    query = db_session.query(Entry).filter(Entry.feed_id == feed_id).order_by(desc(Entry.updated)).limit(limit).offset(
        offset)


# Path: pyfeedreader/database.py
# def init_db():
# def drop_all():
# 
# Path: pyfeedreader/models/ReadEntries.py
# class ReadEntry(Model):
#     __tablename__ = "read_entry"
#     id = Column('id', Integer, primary_key=True)
#     entry_id = Column(Integer)
#     user_id = Column(Integer, ForeignKey('user.id'))
# 
#     def __init__(self, entry_id, user_id):
#         self.entry_id = entry_id
#         self.user_id = user_id
# 
#     def __repr__(self):
#         return '<ReadEntry %r>' % self.username
# 
# Path: pyfeedreader/models/entry.py
# class Entry(Model):
#     __tablename__ = "entry"
#     id = Column('id', Integer, primary_key=True)
#     feed_id = Column(Integer)
#     published = Column(Integer)
#     updated = Column(Integer)
#     title = Column(String(1024))
#     content = Column(Text)
#     description = Column(String(256))
#     link = Column(String(1024))
#     remote_id = Column(String(1024))
# 
#     def __init__(self, feed_id=None, published=None, updated=None, title=None, content=None, description=None,
#                  link=None, remote_id=None, unread=True):
#         self.feed_id = feed_id
#         self.published = published
#         self.updated = updated
#         self.title = title
#         self.content = content
#         self.description = description
#         self.link = link
#         self.remote_id = remote_id
#         self.unread = unread
# 
#     @staticmethod
#     def json_entry(entry, db_session, user_id):
#         """
#         Turns the entry object into a JSON ready dict object.
# 
#         :param entry: The entry you want to make JSON ready.
# 
#         :return: The JSON ready dict.
#         """
# 
#         read_entry = db_session.query(ReadEntry).filter(ReadEntry.entry_id == entry.id,
#                                                         ReadEntry.user_id == user_id).first()
#         unread = read_entry is None
# 
#         return {
#             "entry_id": entry.id,
#             "feed_id": entry.feed_id,
#             "published": entry.published,
#             "updated": entry.updated,
#             "title": entry.title,
#             "content": entry.content,
#             "description": entry.description,
#             "link": entry.link,
#             "remote_id": entry.remote_id,
#             "unread": unread,
#         }
# 
#     @staticmethod
#     def json_list(entries, db_session, user_id):
#         """
#         Turns a list of entries into JSON ready entries.
# 
#         :param entries: The list of entries you want to turn into JSON ready dicts.
# 
#         :return: List of JSON ready dicts.
#         """
# 
#         json_ready = []
#         for entry in entries:
#             json_ready.append(
#                 Entry.json_entry(entry, db_session, user_id)
#             )
# 
#         return json_ready


# Path: pyfeedreader/views/entries.py
from flask import *
from pyfeedreader.database import db_session
from flask.ext.login import *
from sqlalchemy import desc
from pyfeedreader.models.ReadEntries import ReadEntry
from pyfeedreader.models.entry import Entry
import json


def delete_entries_feed():
    abort(400)


def get_entries_feed(feed_id):
    """
    A GET request should return all entries for that feed, this request will have a limit and offset.

    :param feed_id: The ID of the feed.
    :return:
    """
    try:
        limit = int(request.args.get("limit"))
        offset = int(request.args.get("offset"))
    except (ValueError, TypeError):
        return jsonify(success=False, message="Invalid limit and or offset.")

    if limit is None or offset is None:
        return jsonify(success=False, message="Invalid limit and or offset.")

    if limit < 0 or offset < 0:
        return jsonify(success=False, message="Invalid limit and or offset.")

    if limit > 200:
        return jsonify(success=False, message="Too big range.")

    query = db_session.query(Entry).filter(Entry.feed_id == feed_id).order_by(desc(Entry.updated)).limit(limit).offset(
        offset)


    return jsonify(success=True, results=Entry.json_list(query.all(), db_session, current_user.id))