# Path: raggregate/queries/users.py
# def get_user_by_id(id):
# def get_user_by_name(name):
# def get_user_by_email(email):
# def get_user_by_token(token):
# def is_user_allowed_admin_action(user_id, target_id, request = None, target_class = 'user_post',):
# def get_followed_users(id):
# def get_user_votes(user_id, vote_type, submission_id=None):
# def create_temp_user(initial_pw = None):
# def fuzzify_date(d):
# def create_user(**kwargs):
# def login_user(request, u, password, bypass_password = False):
# def add_user_picture(orig_filename, new_prefix, up_dir, image_file):
# def send_email_to_user(request, to_email, title, body):
# def send_lost_password_verify_email(request, user):
# def generate_new_password(request, user):
# 
# Path: raggregate/models/comment.py
# class Comment(Base):
#     __tablename__ = 'comments'
# 
#     id = Column(GUID, primary_key=True)
#     submission_id = Column(GUID, ForeignKey('submissions.id'), nullable=False)
#     user_id = Column(GUID, ForeignKey('users.id'), nullable=False)
#     parent_id = Column(GUID, nullable=False)
#     in_reply_to = Column(GUID, ForeignKey('users.id'), nullable=True)
#     body = Column(UnicodeText, nullable=False)
#     points = Column(Integer, default=0)
#     # unread field for epistle/mailbox display.
#     unread = Column(Boolean, default=True)
#     deleted = Column(Boolean, default=False)
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
# 
#     submitter = relationship("User", backref="comments", primaryjoin="User.id == Comment.user_id")
#     recipient_u = relationship("User", primaryjoin="User.id == Comment.in_reply_to")
#     votes = relationship("Vote", cascade="all, delete, delete-orphan")
#     submission = relationship("Submission", backref="comments")
# 
#     def __init__(self, submission_id, user_id, parent_id, body, in_reply_to = None):
#         self.submission_id = submission_id
#         self.user_id = user_id
#         self.parent_id = parent_id
#         if body is None:
#             raise Exception("Please include a message.")
#         else:
#             self.body = body
#         self.in_reply_to = in_reply_to
# 
#     def tally_votes(self):
#         votes = DBSession.query(Vote).filter(Vote.comment_id == self.id).all()
#         tally = 0
#         for v in votes:
#             tally += v.points
#         if self.points != tally:
#             self.points = tally
#         return votes
# 
#     def load_parent(self):
#         # raggregate.queries depends on the models defined in this file
#         # so it shouldn't be imported until it's ready to be used in a function
#         # as it's being imported here. Otherwise, we die with dependency problems.
#         # it is probably not advisable to do things this way, but it is much nicer
#         from raggregate.queries import general
#         if self.parent_id == None:
#             print("No parent id on comment {0}, this is a problem...".format(self.id))
#             return None
#         p = general.find_by_id(self.parent_id)
#         return p
# 
#     def load_submission(self):
#         from raggregate.queries import submission
#         return submission.get_story_by_id(self.submission_id)
# 
# Path: raggregate/models/epistle.py
# class Epistle(Base):
#     __tablename__ = 'epistles'
# 
#     id = Column(GUID, primary_key=True)
#     recipient = Column(GUID, ForeignKey('users.id'))
#     sender = Column(GUID, ForeignKey('users.id'))
#     body = Column(UnicodeText)
#     subject = Column(UnicodeText)
#     unread = Column(Boolean, default = True)
#     #for threading
#     parent = Column(GUID)
#     # valid parent_types are 'epistle', 'story', 'comment'
#     parent_type = Column(UnicodeText, default=u'epistle')
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
# 
#     recipient_u = relationship("User", primaryjoin=("Epistle.recipient == User.id"))
#     sender_u = relationship("User", primaryjoin=("Epistle.sender == User.id"))
# 
#     def __init__(self, recipient, sender, body, parent_type = None, parent = None, subject = None):
#         self.recipient = recipient
#         self.sender = sender
#         self.body = body
# 
#         if parent_type is not None:
#             self.parent_type = parent_type
#         if parent is not None:
#             self.parent = parent
#         if subject is not None:
#             self.subject = subject
# 
#     def display_subject(self):
#         if self.subject is not None:
#             return self.subject
#         else:
#             return u'<no subject>'


import sqlalchemy
import sqlahelper
from raggregate.queries import users
from raggregate.models.comment import Comment
from raggregate.models.epistle import Epistle

dbsession = sqlahelper.get_session()

def get_epistle_by_sender_id(id):
    return dbsession.query(Epistle).filter(Epistle.sender == id).all()

def get_epistle_by_sender_name(name):
    user = users.get_user_by_name(name)
    return get_epistle_by_sender_id(user.id)

def get_epistle_by_recipient_id(id):
    return dbsession.query(Epistle).filter(Epistle.recipient == id).all()

def get_epistle_by_recipient_name(name):
    user = users.get_user_by_name(name)
    return get_epistle_by_recipient_id(user.id)

def get_new_message_num(id):
    user = users.get_user_by_id(id)
    epistle_num = dbsession.query(Epistle).filter(user.id == Epistle.recipient).filter(Epistle.unread == True).count()

# Path: raggregate/queries/users.py
# def get_user_by_id(id):
# def get_user_by_name(name):
# def get_user_by_email(email):
# def get_user_by_token(token):
# def is_user_allowed_admin_action(user_id, target_id, request = None, target_class = 'user_post',):
# def get_followed_users(id):
# def get_user_votes(user_id, vote_type, submission_id=None):
# def create_temp_user(initial_pw = None):
# def fuzzify_date(d):
# def create_user(**kwargs):
# def login_user(request, u, password, bypass_password = False):
# def add_user_picture(orig_filename, new_prefix, up_dir, image_file):
# def send_email_to_user(request, to_email, title, body):
# def send_lost_password_verify_email(request, user):
# def generate_new_password(request, user):
# 
# Path: raggregate/models/comment.py
# class Comment(Base):
#     __tablename__ = 'comments'
# 
#     id = Column(GUID, primary_key=True)
#     submission_id = Column(GUID, ForeignKey('submissions.id'), nullable=False)
#     user_id = Column(GUID, ForeignKey('users.id'), nullable=False)
#     parent_id = Column(GUID, nullable=False)
#     in_reply_to = Column(GUID, ForeignKey('users.id'), nullable=True)
#     body = Column(UnicodeText, nullable=False)
#     points = Column(Integer, default=0)
#     # unread field for epistle/mailbox display.
#     unread = Column(Boolean, default=True)
#     deleted = Column(Boolean, default=False)
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
# 
#     submitter = relationship("User", backref="comments", primaryjoin="User.id == Comment.user_id")
#     recipient_u = relationship("User", primaryjoin="User.id == Comment.in_reply_to")
#     votes = relationship("Vote", cascade="all, delete, delete-orphan")
#     submission = relationship("Submission", backref="comments")
# 
#     def __init__(self, submission_id, user_id, parent_id, body, in_reply_to = None):
#         self.submission_id = submission_id
#         self.user_id = user_id
#         self.parent_id = parent_id
#         if body is None:
#             raise Exception("Please include a message.")
#         else:
#             self.body = body
#         self.in_reply_to = in_reply_to
# 
#     def tally_votes(self):
#         votes = DBSession.query(Vote).filter(Vote.comment_id == self.id).all()
#         tally = 0
#         for v in votes:
#             tally += v.points
#         if self.points != tally:
#             self.points = tally
#         return votes
# 
#     def load_parent(self):
#         # raggregate.queries depends on the models defined in this file
#         # so it shouldn't be imported until it's ready to be used in a function
#         # as it's being imported here. Otherwise, we die with dependency problems.
#         # it is probably not advisable to do things this way, but it is much nicer
#         from raggregate.queries import general
#         if self.parent_id == None:
#             print("No parent id on comment {0}, this is a problem...".format(self.id))
#             return None
#         p = general.find_by_id(self.parent_id)
#         return p
# 
#     def load_submission(self):
#         from raggregate.queries import submission
#         return submission.get_story_by_id(self.submission_id)
# 
# Path: raggregate/models/epistle.py
# class Epistle(Base):
#     __tablename__ = 'epistles'
# 
#     id = Column(GUID, primary_key=True)
#     recipient = Column(GUID, ForeignKey('users.id'))
#     sender = Column(GUID, ForeignKey('users.id'))
#     body = Column(UnicodeText)
#     subject = Column(UnicodeText)
#     unread = Column(Boolean, default = True)
#     #for threading
#     parent = Column(GUID)
#     # valid parent_types are 'epistle', 'story', 'comment'
#     parent_type = Column(UnicodeText, default=u'epistle')
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
# 
#     recipient_u = relationship("User", primaryjoin=("Epistle.recipient == User.id"))
#     sender_u = relationship("User", primaryjoin=("Epistle.sender == User.id"))
# 
#     def __init__(self, recipient, sender, body, parent_type = None, parent = None, subject = None):
#         self.recipient = recipient
#         self.sender = sender
#         self.body = body
# 
#         if parent_type is not None:
#             self.parent_type = parent_type
#         if parent is not None:
#             self.parent = parent
#         if subject is not None:
#             self.subject = subject
# 
#     def display_subject(self):
#         if self.subject is not None:
#             return self.subject
#         else:
#             return u'<no subject>'


# Path: raggregate/queries/epistle.py
import sqlalchemy
import sqlahelper
from raggregate.queries import users
from raggregate.models.comment import Comment
from raggregate.models.epistle import Epistle


dbsession = sqlahelper.get_session()

def get_epistle_by_sender_id(id):
    return dbsession.query(Epistle).filter(Epistle.sender == id).all()

def get_epistle_by_sender_name(name):
    user = users.get_user_by_name(name)
    return get_epistle_by_sender_id(user.id)

def get_epistle_by_recipient_id(id):
    return dbsession.query(Epistle).filter(Epistle.recipient == id).all()

def get_epistle_by_recipient_name(name):
    user = users.get_user_by_name(name)
    return get_epistle_by_recipient_id(user.id)

def get_new_message_num(id):
    user = users.get_user_by_id(id)
    epistle_num = dbsession.query(Epistle).filter(user.id == Epistle.recipient).filter(Epistle.unread == True).count()

    comment_num = dbsession.query(Comment).filter(sqlalchemy.and_(user.id == Comment.in_reply_to, user.id != Comment.user_id)).filter(Comment.unread == True).count()