예제 #1
0
class VotePaper(db.Model):

    """
        voter_id: `Users` instance id (primary key)
        law_voted_id: `Law` instance id (primary key)
        vote: TODO: document
        law_voted: the relationship with `Law` table above. Backref `vote_papers`
        user_voted: the relationship with `Users` table above. Backref `laws_voted_in`
    """
    __tablename__ = 'vote_paper'
    __bind_key__ = 'law_votes_cast'
    voter_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    law_voted_id = db.Column(db.Integer, db.ForeignKey('law.id'), primary_key=True)
    vote = db.Column(db.Integer)

    law_voted = db.relationship("Law", back_populates="vote_papers")
    user_voted = db.relationship("Users", back_populates="laws_voted_in")

    def __init__(self, vote):
        self.vote = vote

    @staticmethod
    def remove(vote_paper=None, voter_id=None, law_voted_id=None):
        if voter_id and law_voted_id:
            vote_paper = VotePaper.query.filter(VotePaper.voter_id == voter_id)\
                .filter(VotePaper.law_voted_id == law_voted_id).first()
        if not vote_paper:
            return

        db.session.delete(vote_paper)
예제 #2
0
class Comment(db.Model):

    """
        Table columns:
            - id: primary key
            - content: The comment displayed
            - date posted: Note that this is automated once `Comment` instance is created

            - likes: ---.

            - author_id: one(user)-to-Many(comments) relationship with `Users`.
            - proposal_id: one(proposal)-to-Many(comments) relationship with `Proposal`.
            - comments: All comments associated with this law. backref `posted_at`.

            - parent id: One(parent comment)-to-Many(replies). defaults to `0` if comment is a parent and not a reply.
            - parent: Relationship with `Comment`. backref `children`.
        """

    __tablename__ = 'comment'
    __bind_key__ = 'comments'

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    likes = db.Column(db.Integer, nullable=False, default=0)

    author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    proposal_id = db.Column(db.Integer, db.ForeignKey('proposal.id'))

    parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=False, default=0)
    parent = db.relationship('Comment', remote_side=[id], backref='children')

    def __init__(self, content, proposal, author, parent_comment=None):
        self.content = content
        self.posted_at = proposal
        self.author = author
        self.parent = parent_comment

    def __repr__(self):
        return 'comment ' + str(self.id)

    @staticmethod
    def remove(comment=None, comment_id=None):
        if comment_id:
            comment = Comment.query.get(comment_id)
        if not comment:
            return

        if comment.parent_id != 0:  # child (reply)
            db.session.remove(comment)
        else:
            for reply in comment.children:
                Comment.remove(comment=reply)
            db.session.remove(comment)

        return
예제 #3
0
class Notification(db.Model):
    __bind_key__ = 'notification_db'
    __tablename__ = 'notification'

    id = db.Column(db.Integer, primary_key=True)
    message = db.Column(db.Text, nullable=False)
    sender_id = db.Column(db.Integer, db.ForeignKey('users.id'), default=0)
    recipient_id = db.Column(db.Integer, db.ForeignKey('users.id'), default=0)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    def __init__(self, message, sender_id=0, recipient_id=0):
        self.recipient_id = recipient_id
        self.message = message
        self.sender_id = sender_id
예제 #4
0
class Volunteer(db.Model):
    """
    - type: - sign_up_collector
    """
    __bind_key__ = 'volunteer_db'
    __tablename__ = 'volunteer'

    id = db.Column(db.Integer, db.ForeignKey('users.id'), unique=True, primary_key=True)
    type = db.Column(db.Text, nullable=False)
    date_volunteered = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    def __init__(self, volunteer_type, volunteer_user):
        self.user = volunteer_user
        self.type = volunteer_type
예제 #5
0
class Proposal(db.Model):

    """
    Table columns:
        - id: primary key
        - title: ---.
        - info: proposal's main information that is displayed under title.
        - explanation: ---.
        - date posted: Note that this is automated once `Proposal` instance is created

        - author_id: one(user)-to-Many(proposals) relationship with `Users`.
        - law_id: one(law)-to-Many(proposals) relationship with `Law`.
        - comments: All comments associated with this law. backref `posted_at`.

        - up_votes: ---.

        - title_arabic: Translation
        - info_arabic: Translation
        - explanation_arabic: Translation
    """
    __tablename__ = 'proposal'
    __bind_key__ = 'proposals'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False, default='title')
    info = db.Column(db.Text, nullable=False)
    explanation = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    law_id = db.Column(db.Integer, db.ForeignKey('law.id'))
    comments = db.relationship('Comment', backref='posted_at')

    up_votes = db.Column(db.Integer, nullable=False, default=0)

    title_arabic = db.Column(db.Text, nullable=True)
    info_arabic = db.Column(db.Text, nullable=True)
    explanation_arabic = db.Column(db.Text, nullable=True)

    def __init__(self, title, info, explanation, law_, author):
        self.title = title
        self.info = info
        self.explanation = explanation
        self.posted_at = law_
        self.author = author

    def __repr__(self):
        return 'proposal ' + str(self.id)

    def __meta_data__(self, endpoint):
        data = [
            {
                'name': 'title',
                'content': 'proposal - ' + self.title
            },
            {
                'name': 'description',
                'content': self.info[:155] + '...'
            },
            {
                'name': 'og:description',
                'content': self.info[:155] + '...'
            },
            {
                'name': 'og:title',
                'content': self.title
            }
        ]
        return data

    @staticmethod
    def remove(proposal=None, proposal_id=None):
        if proposal_id:
            proposal = Proposal.query.get(proposal)
        if not proposal:
            return

        for comment in proposal.comments:
            Comment.remove(comment=comment)

        for user in proposal.voters:
            proposal.voters.remove(user)

        db.session.delete(proposal)
예제 #6
0
class Law(db.Model):

    """
    Table columns:
        - id: primary key
        - title: ---.
        - info: law's main information that is displayed under title.
        - explanation: ---.
        - date posted: Note that this is automated once `Law` instance is created

        - author_id: one(Users)-to-Many(Laws) relationship with `Users`.
        - edit_proposals: All edit_proposals associated with this law. backref `posted_at`.

        - vote_papers: no backref (Association Object). Many-to-Many relationship using ASSOCIATION OBJECT
                       `VotePaper`.
        - up_votes: ---.
        - down_votes: ---.

        - title_arabic: Translation
        - info_arabic: Translation
        - explanation_arabic: Translation
    """

    __tablename__ = 'law'
    __bind_key__ = 'laws'
    __searchable__ = ['id', 'title', 'info']

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.Text, nullable=False, default='title')
    info = db.Column(db.Text, nullable=False)
    explanation = db.Column(db.Text, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    edit_proposals = db.relationship('Proposal', backref='posted_at')

    vote_papers = db.relationship('VotePaper')
    up_votes = db.Column(db.Integer, nullable=False, default=0)
    down_votes = db.Column(db.Integer, nullable=False, default=0)

    title_arabic = db.Column(db.Text, nullable=True)
    info_arabic = db.Column(db.Text, nullable=True)
    explanation_arabic = db.Column(db.Text, nullable=True)

    def __init__(self, title, info, explanation, author):
        self.title = title
        self.info = info
        self.explanation = explanation
        self.author = author

    def __repr__(self):
        return 'law ' + str(self.id)

    def __meta_data__(self, endpoint):
        if endpoint == 'laws.law':
            data = [
                {
                    'name': 'title',
                    'content': 'law - ' + self.title
                },
                {
                    'name': 'description',
                    'content': self.info[:155] + '...'
                },
                {
                    'name': 'og:description',
                    'content': self.info[:155] + '...'
                },
                {
                    'name': 'og:title',
                    'content': self.title
                }
            ]
            return data
        return None

    @staticmethod
    def remove(law=None, law_id=None):
        if law_id:
            law = Law.query.get(law_id)
        if not law:
            return

        for proposal in law.edit_proposals:
            Proposal.remove(proposal=proposal)

        for vote_paper in law.vote_papers:
            VotePaper.remove(vote_paper=vote_paper)

        db.session.delete(law)
예제 #7
0
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from white_party import db, app
from flask import url_for
from datetime import datetime, timedelta


comment_likes_table = db.Table('comment_likes_table',
                               db.Column('comment_id', db.Integer, db.ForeignKey('comment.id')),
                               db.Column('user_id', db.Integer, db.ForeignKey('users.id'))
                               )


proposals_up_voting_table = db.Table('proposals_up_voting_table',
                                     db.Column('proposal_id', db.Integer, db.ForeignKey('proposal.id')),
                                     db.Column('user_id', db.Integer, db.ForeignKey('users.id'))
                                     )


follow_ship_table = db.Table('follow_ship_table',
                             db.Column('follower_id', db.Integer, db.ForeignKey('users.id')),
                             db.Column('followed_id', db.Integer, db.ForeignKey('users.id'))
                             )


class Users(db.Model):
    """
    Table columns:
        - id: primary key
        - membership id: unique id given for every member
        - description: About me section in user profile
        - password: *****