class QuestionVote(Model, VoteMixin): __tablename__ = "question_vote" question_id = integer_fk("question.id", primary_key=True) user_id = integer_fk(User.id, primary_key=True) value = C(Integer, default=1) user = rel(User, backref=bref("question_votes", lazy="dynamic")) question = rel("Question", backref=bref("votes", lazy="dynamic")) def __repr__(self): return "QuestionVote '{}' for '{}'".format(self.value, self.question_id)
class ArgumentVote(Model, VoteMixin): __tablename__ = "argument_vote" argument_id = integer_fk("argument.id", primary_key=True) user_id = integer_fk(User.id, primary_key=True) value = C(Integer) user = rel(User, backref=bref("argument_votes", lazy="dynamic")) argument = rel("Argument", backref=bref("votes", lazy="dynamic")) def __repr__(self): return "ArgumentVote '{}' for '{}'".format(self.value, self.argument_id)
class QuestionAssociation(Model): __tablename__ = "question_to_question" left_id = integer_fk(Question.id, primary_key=True) right_id = integer_fk(Question.id, primary_key=True) association_type = C(Unicode) left = rel(Question, primaryjoin=Question.id == left_id, backref=bref("right_assocs", lazy="dynamic")) right = rel(Question, primaryjoin=Question.id == right_id, backref=bref("left_assocs", lazy="dynamic")) def __repr__(self): return "Question {} -> {} ({})".format(self.left_id, self.right_id, self.association_type)
class EkklesiaUserInfo(Model): __tablename__ = "oauth_info" user_id = C(FK(User.id), primary_key=True) auid = C(Unicode, unique=True) user_type = C(Unicode) verified = C(Boolean) all_nested_group_ids = C(ARRAY(Integer)) nested_group_ids = C(ARRAY(Integer)) user = rel(User, backref=bref("ekklesia_info", uselist=False))
class Argument(Model, TimeStamp): __tablename__ = 'argument' id = integer_pk() title = C(Unicode) abstract = C(Unicode) details = C(Text) url = C(Unicode) author_id = integer_fk(User.id) question_id = integer_fk("question.id") argument_type = C(Unicode) parent_id = integer_fk(id) author = rel(User) _counter_arguments = rel("Argument", lazy="dynamic", backref=bref("parent", remote_side=[id])) question = rel("Question", backref=bref("arguments", lazy="dynamic")) @property def counter_arguments(self): return self._counter_arguments.order_by(desc(Argument.score)) @hybrid_property def score(self): return self.votes.with_entities( func.coalesce(func.sum(ArgumentVote.value), 0)).scalar() @score.expression def score_expr(cls): return (select([func.coalesce(func.sum(ArgumentVote.value), 0) ]).where(ArgumentVote.argument_id == cls.id)) def user_vote(self, user): return self.votes.filter_by(user_id=user.id).scalar() def __repr__(self): return "Argument '{}' for '{}'".format(self.url, self.question_id)