64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
|
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)
|