From 4aa1802afbbd004d34d659e2b0e651e73d01da0e Mon Sep 17 00:00:00 2001 From: Darks Date: Thu, 12 Nov 2020 00:11:33 +0100 Subject: [PATCH] polls: created backbone of models (#72) --- app/models/comment.py | 3 +++ app/models/poll.py | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/models/poll.py diff --git a/app/models/comment.py b/app/models/comment.py index 8c7be13..5c1d0e7 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -19,6 +19,9 @@ class Comment(Post): backref=backref('comments', lazy='dynamic'), foreign_keys=thread_id) + # Other fields populated automatically through relations: + # A poll attached to the comment (of class Poll) + def __init__(self, author, text, thread): """ Create a new Comment in a thread. diff --git a/app/models/poll.py b/app/models/poll.py new file mode 100644 index 0000000..3b11930 --- /dev/null +++ b/app/models/poll.py @@ -0,0 +1,63 @@ +from app import db +from enum import Enum +from sqlalchemy.orm import backref + + +class PollType(Enum): + """Polls types: single/multiple answers. Easier than inheritance""" + SINGLE = 1 + MULTIPLE = 2 + + +class Poll(db.Model): + """Some poll, with different options""" + + __tablename__ = 'poll' + + # Unique ID + id = db.Column(db.Integer, primary_key=True) + + # Owner comment + comment_id = db.Column(db.Integer, db.ForeignKey('comment.id')) + comment = db.relationship('Comment', uselist=False, back_populates="poll", + foreign_keys=thread_id) + + # Type + type = db.Column(db.Enum(PollType)) + + # Title/question + title = db.Column(db.UnicodeText) + + # End datetime + end = db.Column(db.DateTime) + + # Choices + # We want a size-variable list of strings, or a dictionnary with + # key/values, depending on the poll type. + # As the data is likely to be adapted to the poll type, the PickleType + # seems to be appropriate. Same applies for PollAnswer. + choices = db.Column(db.PickleType) + + # Other fields populated automatically through relations: + # The list of answers (of type PollAnswer) + + +class PollAnswer(db.Model): + """An answer to a poll""" + + __tablename__ = 'pollanswer' + + # Unique ID + id = db.Column(db.Integer, primary_key=True) + + # Poll + poll_id = db.Column(db.Integer, db.ForeignKey('poll.id')) + poll = db.relationship('Poll', back_populates="answers", + foreign_keys=poll_id) + + # Author. Must be Member + author_id = db.Column(db.Integer, db.ForeignKey('member.id')) + author = db.relationship('Member', foreign_keys=author_id) + + # Choice(s) + choices = db.Column(db.PickleType)