polls: created backbone of models (#72)

This commit is contained in:
Darks 2020-11-12 00:11:33 +01:00
parent 63baae9683
commit 4aa1802afb
Signed by: Darks
GPG Key ID: 7515644268BE1433
2 changed files with 66 additions and 0 deletions

View File

@ -19,6 +19,9 @@ class Comment(Post):
backref=backref('comments', lazy='dynamic'),
foreign_keys=thread_id)
# Other fields populated automatically through relations:
# <poll> A poll attached to the comment (of class Poll)
def __init__(self, author, text, thread):
"""
Create a new Comment in a thread.

63
app/models/poll.py Normal file
View File

@ -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:
# <answers> 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)