class Tag(Model): __tablename__ = 'tag' id = integer_pk() tag = C(Unicode) def __repr__(self): return "Tag '{}'".format(self.tag)
class VotingModule(Model): __tablename__ = "voting_module" id = integer_pk() name = C(Unicode, unique=True) base_url = C(Unicode) module_type = C(Unicode) __mapper_args__ = {"polymorphic_on": module_type}
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)
class Question(Model, TimeStamp): query_class = QuestionQuery __tablename__ = 'question' id = integer_pk() details = C(Text) title = C(Unicode) url = C(Unicode) search_vector = C(TSVectorType('details', 'title', 'url')) tags = rel(Tag, secondary=tag_to_question, backref="questions") @hybrid_property def score(self): return self.votes.count() @score.expression def score_expr(cls): return (select([ func.count("*") ]).where(QuestionVote.question_id == cls.id).label("votes_count")) @hybrid_property def pro_arguments(self): return self.arguments.filter_by(argument_type=u"pro", parent=None).order_by( desc(Argument.score)) @hybrid_property def contra_arguments(self): return self.arguments.filter_by(argument_type=u"con", parent=None).order_by( desc(Argument.score)) def user_vote(self, user): return self.votes.filter_by(user_id=user.id).scalar() def __repr__(self): return "Question '{}'".format(self.url)
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)
class UserGroup(Model): __tablename__ = "usergroup" id = integer_pk() name = C(Unicode)