# Path: raggregate/models/vote.py
# class Vote(Base):
#     __tablename__ = 'votes'
#     id = Column(GUID, primary_key=True)
#     # we do not coalesce these into a generic item column
#     # because things are easier/safer with the FK support.
#     # the tradeoff here is size on various db structures.
#     submission_id = Column(GUID, ForeignKey('submissions.id'))
#     comment_id = Column(GUID, ForeignKey('comments.id'))
#     motd_id = Column(GUID, ForeignKey('motds.id'))
#     user_id = Column(GUID, ForeignKey('users.id'), nullable=False)
#     # points: -1 for down, 0 for neutral, 1 for up
#     # this also allows easier manipulation of vote tallying if needed
#     # considered using a boolean and deleting the row if a user unvotes
#     # i guess but have no evidence that's slower than putting this back
#     # to zero, and as above, makes it more difficult to "modify" the
#     # voting system if needed.
#     points = Column(Integer, nullable=False, default=0)
#     # 0 for none, 1 for up, -1 for down
#     direction = Column(Integer, nullable=False, default=0)
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
# 
#     voter = relationship("User", backref="votes")
# 
#     def __init__(self, item_id, user_id, points, target_type, comment_id = None):
#         if target_type == 'comment':
#             self.comment_id = comment_id
#             self.submission_id = item_id
#         elif target_type == 'submission':
#             self.submission_id = item_id
#         elif target_type == 'motd':
#             self.motd_id = item_id
#         else:
#             raise Exception("Need to know target type for this vote.")
#         self.user_id = user_id
#         self.points = points
# 
# Path: raggregate/guid_recipe.py
# class GUID(TypeDecorator):
#     """Platform-independent GUID type.
# 
#     Uses Postgresql's UUID type, otherwise uses
#     CHAR(32), storing as stringified hex values.
# 
#     """
#     impl = CHAR
# 
#     def load_dialect_impl(self, dialect):
#         if dialect.name == 'postgresql':
#             return dialect.type_descriptor(UUID())
#         else:
#             return dialect.type_descriptor(CHAR(32))
# 
#     def process_bind_param(self, value, dialect):
#         if value is None:
#             return value
#         elif dialect.name == 'postgresql':
#             return str(value)
#         else:
#             if not isinstance(value, uuid.UUID):
#                 return "{0}".format(uuid.UUID(value))
#             else:
#                 # hexstring
#                 return "{0}".format(value)
# 
#     def process_result_value(self, value, dialect):
#         if value is None:
#             return value
#         else:
#             return uuid.UUID(value)


from raggregate.models import *
from raggregate.models.vote import Vote
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import Unicode
from sqlalchemy import UnicodeText
from sqlalchemy import DateTime
from sqlalchemy import text
from sqlalchemy import Boolean
from sqlalchemy import ForeignKey
from raggregate.guid_recipe import GUID
        from raggregate.queries import general
        from raggregate.queries import submission
import sqlalchemy
    __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):

# Path: raggregate/models/vote.py
# class Vote(Base):
#     __tablename__ = 'votes'
#     id = Column(GUID, primary_key=True)
#     # we do not coalesce these into a generic item column
#     # because things are easier/safer with the FK support.
#     # the tradeoff here is size on various db structures.
#     submission_id = Column(GUID, ForeignKey('submissions.id'))
#     comment_id = Column(GUID, ForeignKey('comments.id'))
#     motd_id = Column(GUID, ForeignKey('motds.id'))
#     user_id = Column(GUID, ForeignKey('users.id'), nullable=False)
#     # points: -1 for down, 0 for neutral, 1 for up
#     # this also allows easier manipulation of vote tallying if needed
#     # considered using a boolean and deleting the row if a user unvotes
#     # i guess but have no evidence that's slower than putting this back
#     # to zero, and as above, makes it more difficult to "modify" the
#     # voting system if needed.
#     points = Column(Integer, nullable=False, default=0)
#     # 0 for none, 1 for up, -1 for down
#     direction = Column(Integer, nullable=False, default=0)
#     added_on = Column(DateTime(timezone=True), default=sqlalchemy.sql.func.now())
# 
#     voter = relationship("User", backref="votes")
# 
#     def __init__(self, item_id, user_id, points, target_type, comment_id = None):
#         if target_type == 'comment':
#             self.comment_id = comment_id
#             self.submission_id = item_id
#         elif target_type == 'submission':
#             self.submission_id = item_id
#         elif target_type == 'motd':
#             self.motd_id = item_id
#         else:
#             raise Exception("Need to know target type for this vote.")
#         self.user_id = user_id
#         self.points = points
# 
# Path: raggregate/guid_recipe.py
# class GUID(TypeDecorator):
#     """Platform-independent GUID type.
# 
#     Uses Postgresql's UUID type, otherwise uses
#     CHAR(32), storing as stringified hex values.
# 
#     """
#     impl = CHAR
# 
#     def load_dialect_impl(self, dialect):
#         if dialect.name == 'postgresql':
#             return dialect.type_descriptor(UUID())
#         else:
#             return dialect.type_descriptor(CHAR(32))
# 
#     def process_bind_param(self, value, dialect):
#         if value is None:
#             return value
#         elif dialect.name == 'postgresql':
#             return str(value)
#         else:
#             if not isinstance(value, uuid.UUID):
#                 return "{0}".format(uuid.UUID(value))
#             else:
#                 # hexstring
#                 return "{0}".format(value)
# 
#     def process_result_value(self, value, dialect):
#         if value is None:
#             return value
#         else:
#             return uuid.UUID(value)


# Path: raggregate/models/comment.py
from raggregate.models import *
from raggregate.models.vote import Vote
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import Unicode
from sqlalchemy import UnicodeText
from sqlalchemy import DateTime
from sqlalchemy import text
from sqlalchemy import Boolean
from sqlalchemy import ForeignKey
from raggregate.guid_recipe import GUID
        from raggregate.queries import general
        from raggregate.queries import submission
import sqlalchemy

    __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()