Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
class QuestionQuery(Query, SearchQueryMixin):
    pass


class UserGroup(Model):

    __tablename__ = "usergroup"

    id = integer_pk()
    name = C(Unicode)


user_to_usergroup = Table(
    "user_to_usergroup", db_metadata,
    integer_fk("user.id", name="user_id", primary_key=True),
    integer_fk(UserGroup.id, name="group_id", primary_key=True))


class User(Model):  #, UserMixin):

    __tablename__ = "user"

    id = integer_pk()
    login_name = C(Unicode, unique=True)
    display_name = C(Unicode, unique=True)

    groups = rel(UserGroup, secondary=user_to_usergroup, backref="users")

    def __repr__(self):
        return "User '{}'".format(self.login_name)