Blame progit/model.py

Pierre-Yves Chibon ae4136
#-*- coding: utf-8 -*-
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
"""
Pierre-Yves Chibon ae4136
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
 Authors:
Pierre-Yves Chibon ae4136
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
"""
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ee9b42
__requires__ = ['SQLAlchemy >= 0.8', 'jinja2 >= 2.4']
Pierre-Yves Chibon ae4136
import pkg_resources
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
import datetime
Pierre-Yves Chibon ae4136
import logging
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
import sqlalchemy as sa
Pierre-Yves Chibon ae4136
from sqlalchemy import create_engine
Pierre-Yves Chibon caf4e8
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon ae4136
from sqlalchemy.ext.declarative import declarative_base
Pierre-Yves Chibon ae4136
from sqlalchemy.orm import sessionmaker
Pierre-Yves Chibon ae4136
from sqlalchemy.orm import scoped_session
Pierre-Yves Chibon ae4136
from sqlalchemy.orm import relation
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
BASE = declarative_base()
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
ERROR_LOG = logging.getLogger('progit.model')
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
def create_tables(db_url, alembic_ini=None, debug=False):
Pierre-Yves Chibon ae4136
    """ Create the tables in the database using the information from the
Pierre-Yves Chibon ae4136
    url obtained.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    :arg db_url, URL used to connect to the database. The URL contains
Pierre-Yves Chibon ae4136
        information with regards to the database engine, the host to
Pierre-Yves Chibon ae4136
        connect to, the user and password and the database name.
Pierre-Yves Chibon ae4136
          ie: <engine>://<user>:<password>@<host>/<dbname></dbname></host></password></user></engine>
Pierre-Yves Chibon ae4136
    :kwarg alembic_ini, path to the alembic ini file. This is necessary
Pierre-Yves Chibon ae4136
        to be able to use alembic correctly, but not for the unit-tests.
Pierre-Yves Chibon ae4136
    :kwarg debug, a boolean specifying wether we should have the verbose
Pierre-Yves Chibon ae4136
        output of sqlalchemy or not.
Pierre-Yves Chibon ae4136
    :return a session that can be used to query the database.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    """
Pierre-Yves Chibon ae4136
    engine = create_engine(db_url, echo=debug)
Pierre-Yves Chibon 05d0d7
    from progit.ui.plugins import get_plugin_tables
Pierre-Yves Chibon c46cda
    get_plugin_tables()
Pierre-Yves Chibon ae4136
    BASE.metadata.create_all(engine)
Pierre-Yves Chibon ae4136
    #engine.execute(collection_package_create_view(driver=engine.driver))
Pierre-Yves Chibon ae4136
    if db_url.startswith('sqlite:'):
Pierre-Yves Chibon ae4136
        ## Ignore the warning about con_record
Pierre-Yves Chibon ae4136
        # pylint: disable=W0613
Pierre-Yves Chibon ae4136
        def _fk_pragma_on_connect(dbapi_con, con_record):
Pierre-Yves Chibon ae4136
            ''' Tries to enforce referential constraints on sqlite. '''
Pierre-Yves Chibon ae4136
            dbapi_con.execute('pragma foreign_keys=ON')
Pierre-Yves Chibon ae4136
        sa.event.listen(engine, 'connect', _fk_pragma_on_connect)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    if alembic_ini is not None:  # pragma: no cover
Pierre-Yves Chibon ae4136
        # then, load the Alembic configuration and generate the
Pierre-Yves Chibon ae4136
        # version table, "stamping" it with the most recent rev:
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
        ## Ignore the warning missing alembic
Pierre-Yves Chibon ae4136
        # pylint: disable=F0401
Pierre-Yves Chibon ae4136
        from alembic.config import Config
Pierre-Yves Chibon ae4136
        from alembic import command
Pierre-Yves Chibon ae4136
        alembic_cfg = Config(alembic_ini)
Pierre-Yves Chibon ae4136
        command.stamp(alembic_cfg, "head")
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    scopedsession = scoped_session(sessionmaker(bind=engine))
Pierre-Yves Chibon caf4e8
    # Insert the default data into the db
Pierre-Yves Chibon c46cda
    try:
Pierre-Yves Chibon c46cda
        create_default_status(scopedsession)
Pierre-Yves Chibon c46cda
    except SQLAlchemyError:
Pierre-Yves Chibon c46cda
        pass
Pierre-Yves Chibon ae4136
    return scopedsession
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon caf4e8
def create_default_status(session):
Pierre-Yves Chibon caf4e8
    """ Insert the defaults status in the status tables.
Pierre-Yves Chibon caf4e8
    """
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon caf4e8
    for status in ['Open', 'Invalid', 'Insufficient data', 'Fixed']:
Pierre-Yves Chibon caf4e8
        ticket_stat = StatusIssue(status=status)
Pierre-Yves Chibon caf4e8
        session.add(ticket_stat)
Pierre-Yves Chibon caf4e8
        try:
Pierre-Yves Chibon caf4e8
            session.flush()
Pierre-Yves Chibon caf4e8
        except SQLAlchemyError, err:
Pierre-Yves Chibon caf4e8
            ERROR_LOG.debug('Status %s could not be added', ticket_stat)
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon caf4e8
    session.commit()
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon cc324e
class StatusIssue(BASE):
Pierre-Yves Chibon cc324e
    """ Stores the status a ticket can have.
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon cc324e
    Table -- status_issue
Pierre-Yves Chibon cc324e
    """
Pierre-Yves Chibon cc324e
    __tablename__ = 'status_issue'
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon cc324e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon cc324e
    status = sa.Column(sa.Text, nullable=False, unique=True)
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon acc99e
class User(BASE):
Pierre-Yves Chibon acc99e
    """ Stores information about users.
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    Table -- users
Pierre-Yves Chibon acc99e
    """
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    __tablename__ = 'users'
Pierre-Yves Chibon acc99e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon acc99e
    user = sa.Column(sa.String(32), nullable=False, unique=True, index=True)
Pierre-Yves Chibon acc99e
    fullname = sa.Column(sa.Text, nullable=False, index=True)
Pierre-Yves Chibon baeff8
    public_ssh_key = sa.Column(sa.Text, nullable=True)
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon 13afd6
    password = sa.Column(sa.Text, nullable=True)
Pierre-Yves Chibon 13afd6
    token = sa.Column(sa.String(50), nullable=True)
Pierre-Yves Chibon 13afd6
    created = sa.Column(
Pierre-Yves Chibon 13afd6
        sa.DateTime,
Pierre-Yves Chibon 13afd6
        nullable=False,
Pierre-Yves Chibon 13afd6
        default=sa.func.now())
Pierre-Yves Chibon 13afd6
    updated_on = sa.Column(
Pierre-Yves Chibon 13afd6
        sa.DateTime,
Pierre-Yves Chibon 13afd6
        nullable=False,
Pierre-Yves Chibon 13afd6
        default=sa.func.now(),
Pierre-Yves Chibon 13afd6
        onupdate=sa.func.now())
Pierre-Yves Chibon 13afd6
Pierre-Yves Chibon 13e142
    # Relations
Pierre-Yves Chibon 13e142
    group_objs = relation(
Pierre-Yves Chibon 13e142
        "ProgitGroup",
Pierre-Yves Chibon 13e142
        secondary="progit_user_group",
Pierre-Yves Chibon 13e142
        primaryjoin="users.c.id==progit_user_group.c.user_id",
Pierre-Yves Chibon 13e142
        secondaryjoin="progit_group.c.id==progit_user_group.c.group_id",
Pierre-Yves Chibon 13e142
        backref="users",
Pierre-Yves Chibon 13e142
    )
Pierre-Yves Chibon 13e142
    session = relation("ProgitUserVisit", backref="user")
Pierre-Yves Chibon 13e142
Pierre-Yves Chibon 13afd6
    @property
Pierre-Yves Chibon 13afd6
    def username(self):
Pierre-Yves Chibon 13afd6
        ''' Return the username. '''
Pierre-Yves Chibon 13afd6
        return self.user
Pierre-Yves Chibon 13afd6
Pierre-Yves Chibon 13afd6
    @property
Pierre-Yves Chibon 13afd6
    def groups(self):
Pierre-Yves Chibon 13afd6
        ''' Return the list of Group.group_name in which the user is. '''
Pierre-Yves Chibon 13afd6
        return [group.group_name for group in self.group_objs]
Pierre-Yves Chibon 13afd6
Pierre-Yves Chibon 13afd6
    def __repr__(self):
Pierre-Yves Chibon 13afd6
        ''' Return a string representation of this object. '''
Pierre-Yves Chibon 13afd6
Pierre-Yves Chibon 13afd6
        return 'User: %s - name %s' % (self.id, self.user)
Pierre-Yves Chibon 13afd6
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
class UserEmail(BASE):
Pierre-Yves Chibon acc99e
    """ Stores email information about the users.
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    Table -- user_emails
Pierre-Yves Chibon acc99e
    """
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    __tablename__ = 'user_emails'
Pierre-Yves Chibon acc99e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon acc99e
    email = sa.Column(sa.Text, nullable=False, unique=True)
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='emails')
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon ae4136
class Project(BASE):
Pierre-Yves Chibon ae4136
    """ Stores the projects.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    Table -- projects
Pierre-Yves Chibon ae4136
    """
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    __tablename__ = 'projects'
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon ae4136
    name = sa.Column(sa.String(32), nullable=False, index=True)
Pierre-Yves Chibon d688cf
    description = sa.Column(sa.Text, nullable=True)
Pierre-Yves Chibon ae4136
    parent_id = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Integer,
Pierre-Yves Chibon ae4136
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon ae4136
        nullable=True)
Pierre-Yves Chibon 20b945
    issue_tracker = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon 4d5b45
    project_docs = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon ae4136
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon f3f2fe
    parent = relation('Project', remote_side=[id], backref='forks')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='projects')
Pierre-Yves Chibon 334ada
Mathieu Bridon b0b97e
    @property
Mathieu Bridon b0b97e
    def path(self):
Pierre-Yves Chibon 0c862c
        ''' Return the name of the git repo on the filesystem. '''
Pierre-Yves Chibon ebf29e
        if self.parent_id:
Pierre-Yves Chibon 51f6a6
            path = '%s/%s.git' % (self.user.user, self.name)
Pierre-Yves Chibon ebf29e
        else:
Pierre-Yves Chibon ebf29e
            path = '%s.git' % (self.name)
Pierre-Yves Chibon ebf29e
        return path
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon b393e8
    @property
Pierre-Yves Chibon e2c16f
    def is_fork(self):
Pierre-Yves Chibon 49feff
        ''' Return a boolean specifying if the project is a fork or not '''
Pierre-Yves Chibon 3a95ee
        return self.parent_id is not None
Pierre-Yves Chibon 49feff
Pierre-Yves Chibon 49feff
    @property
Pierre-Yves Chibon b393e8
    def fullname(self):
Pierre-Yves Chibon b393e8
        ''' Return the name of the git repo as user/project if it is a
Pierre-Yves Chibon b393e8
        project forked, otherwise it returns the project name.
Pierre-Yves Chibon b393e8
        '''
Pierre-Yves Chibon b393e8
        str_name = self.name
Pierre-Yves Chibon 3a95ee
        if self.parent_id:
Pierre-Yves Chibon 51f6a6
            str_name = "%s/%s" % (self.user.user, str_name)
Pierre-Yves Chibon b393e8
        return str_name
Pierre-Yves Chibon b393e8
Pierre-Yves Chibon 2f8732
Pierre-Yves Chibon 96fa1e
class ProjectUser(BASE):
Pierre-Yves Chibon 96fa1e
    """ Stores the user of a projects.
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    Table -- user_projects
Pierre-Yves Chibon 96fa1e
    """
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    __tablename__ = 'user_projects'
Pierre-Yves Chibon 96fa1e
    __table_args__ = (
Pierre-Yves Chibon acc99e
        sa.UniqueConstraint('project_id', 'user_id'),
Pierre-Yves Chibon 96fa1e
    )
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 96fa1e
    project_id = sa.Column(
Pierre-Yves Chibon 96fa1e
        sa.Integer,
Pierre-Yves Chibon 96fa1e
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon 96fa1e
        nullable=False)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    project = relation(
Pierre-Yves Chibon 96fa1e
        'Project', foreign_keys=[project_id], remote_side=[Project.id],
Pierre-Yves Chibon 96fa1e
        backref='users')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='co_projects')
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon ae4136
class Issue(BASE):
Pierre-Yves Chibon ae4136
    """ Stores the issues reported on a project.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    Table -- issues
Pierre-Yves Chibon ae4136
    """
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    __tablename__ = 'issues'
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon de6d71
    uid = sa.Column(sa.Text(32), unique=True)
Pierre-Yves Chibon ae4136
    project_id = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Integer,
Pierre-Yves Chibon ae4136
        sa.ForeignKey(
Pierre-Yves Chibon ae4136
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon ae4136
        nullable=False)
Pierre-Yves Chibon ae4136
    title = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Text,
Pierre-Yves Chibon ae4136
        nullable=False)
Pierre-Yves Chibon ae4136
    content = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Text(),
Pierre-Yves Chibon ae4136
        nullable=False)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon caf4e8
    status = sa.Column(
Pierre-Yves Chibon caf4e8
        sa.Text,
Pierre-Yves Chibon caf4e8
        sa.ForeignKey(
Pierre-Yves Chibon caf4e8
            'status_issue.status', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon caf4e8
        default='Open',
Pierre-Yves Chibon caf4e8
        nullable=False)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon ae4136
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 0ef501
    project = relation(
Pierre-Yves Chibon 0ef501
        'Project', foreign_keys=[project_id], remote_side=[Project.id],
Pierre-Yves Chibon 0ef501
        backref='issues')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='issues')
Pierre-Yves Chibon 0ef501
Pierre-Yves Chibon 2a0134
    def __repr__(self):
Pierre-Yves Chibon 2a0134
        return 'Issue(%s, project:%s, user:%s, title:%s)' % (
Pierre-Yves Chibon 2a0134
            self.id, self.project.name, self.user.user, self.title
Pierre-Yves Chibon 2a0134
        )
Pierre-Yves Chibon 2a0134
Pierre-Yves Chibon 11c5e9
    @property
Pierre-Yves Chibon 11c5e9
    def mail_id(self):
Pierre-Yves Chibon 11c5e9
        ''' Return a unique reprensetation of the issue as string that
Pierre-Yves Chibon 11c5e9
        can be used when sending emails.
Pierre-Yves Chibon 11c5e9
        '''
Pierre-Yves Chibon 11c5e9
        return '%s-ticket-%s@progit' % (self.project.name, self.id)
Pierre-Yves Chibon 11c5e9
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon bb9f71
class IssueComment(BASE):
Pierre-Yves Chibon bb9f71
    """ Stores the comments made on a commit/file.
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    Table -- issue_comments
Pierre-Yves Chibon bb9f71
    """
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    __tablename__ = 'issue_comments'
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon bb9f71
    issue_id = sa.Column(
Pierre-Yves Chibon bb9f71
        sa.Integer,
Pierre-Yves Chibon bb9f71
        sa.ForeignKey(
Pierre-Yves Chibon bb9f71
            'issues.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon bb9f71
        index=True)
Pierre-Yves Chibon bb9f71
    comment = sa.Column(
Pierre-Yves Chibon bb9f71
        sa.Text(),
Pierre-Yves Chibon bb9f71
        nullable=False)
Pierre-Yves Chibon bb9f71
    parent_id = sa.Column(
Pierre-Yves Chibon bb9f71
        sa.Integer,
Pierre-Yves Chibon 36edcd
        sa.ForeignKey('issue_comments.id', onupdate='CASCADE'),
Pierre-Yves Chibon bb9f71
        nullable=True)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon bb9f71
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    issue = relation(
Pierre-Yves Chibon bb9f71
        'Issue', foreign_keys=[issue_id], remote_side=[Issue.id],
Pierre-Yves Chibon bb9f71
        backref='comments')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='comment_issues')
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon 7b8c4b
class PullRequest(BASE):
Pierre-Yves Chibon 7b8c4b
    """ Stores the pull requests created on a project.
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    Table -- pull_requests
Pierre-Yves Chibon 7b8c4b
    """
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    __tablename__ = 'pull_requests'
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 7b8c4b
    project_id = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.Integer,
Pierre-Yves Chibon 7b8c4b
        sa.ForeignKey(
Pierre-Yves Chibon 7b8c4b
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 7b8c4b
        nullable=False)
Pierre-Yves Chibon 85ebac
    project_id_from = sa.Column(
Pierre-Yves Chibon 85ebac
        sa.Integer,
Pierre-Yves Chibon 85ebac
        sa.ForeignKey(
Pierre-Yves Chibon 85ebac
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 85ebac
        nullable=False)
Pierre-Yves Chibon 7b8c4b
    title = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.Text,
Pierre-Yves Chibon 7b8c4b
        nullable=False)
Pierre-Yves Chibon 9e21cc
    branch = sa.Column(
Pierre-Yves Chibon 9e21cc
        sa.Text(),
Pierre-Yves Chibon 9e21cc
        nullable=False)
Pierre-Yves Chibon 7b8c4b
    start_id = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.String(40),
Pierre-Yves Chibon 04a4f4
        nullable=True)
Pierre-Yves Chibon 7b8c4b
    stop_id = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.String(40),
Pierre-Yves Chibon 7b8c4b
        nullable=False)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon 7f6e93
    status = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon 7b8c4b
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon aa3b6b
Pierre-Yves Chibon aa3b6b
    repo = relation(
Pierre-Yves Chibon 58e9f5
        'Project', foreign_keys=[project_id], remote_side=[Project.id],
Pierre-Yves Chibon 58e9f5
        backref='requests')
Pierre-Yves Chibon aa3b6b
    repo_from = relation(
Pierre-Yves Chibon aa3b6b
        'Project', foreign_keys=[project_id_from], remote_side=[Project.id])
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='pull_requests')
Pierre-Yves Chibon 2a35bc
Pierre-Yves Chibon 2a35bc
    def __repr__(self):
Pierre-Yves Chibon 2a35bc
        return 'PullRequest(%s, project:%s, user:%s, title:%s)' % (
Pierre-Yves Chibon 2a35bc
            self.id, self.repo.name, self.user.user, self.title
Pierre-Yves Chibon 2a35bc
        )
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 26f048
class PullRequestComment(BASE):
Pierre-Yves Chibon 26f048
    """ Stores the comments made on a pull-request.
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    Table -- pull_request_comments
Pierre-Yves Chibon 26f048
    """
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    __tablename__ = 'pull_request_comments'
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 26f048
    pull_request_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        sa.ForeignKey(
Pierre-Yves Chibon 26f048
            'pull_requests.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 26f048
        nullable=False)
Pierre-Yves Chibon 26f048
    commit_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.String(40),
Pierre-Yves Chibon 26f048
        nullable=False,
Pierre-Yves Chibon 26f048
        index=True)
Pierre-Yves Chibon 26f048
    user_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon 26f048
        nullable=False,
Pierre-Yves Chibon 26f048
        index=True)
Pierre-Yves Chibon 26f048
    line = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        nullable=True)
Pierre-Yves Chibon 26f048
    comment = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Text(),
Pierre-Yves Chibon 26f048
        nullable=False)
Pierre-Yves Chibon 26f048
    parent_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        sa.ForeignKey('pull_request_comments.id', onupdate='CASCADE'),
Pierre-Yves Chibon 26f048
        nullable=True)
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon 26f048
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon 26f048
                    remote_side=[User.id], backref='pull_request_comments')
Pierre-Yves Chibon 26f048
    pull_request = relation(
Pierre-Yves Chibon 26f048
        'PullRequest', foreign_keys=[pull_request_id], remote_side=[PullRequest.id],
Pierre-Yves Chibon 26f048
        backref='comments')
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 4e1ed8
class GlobalId(BASE):
Pierre-Yves Chibon 4e1ed8
    """ Store the mapping of the project with their issue and pull-request
Pierre-Yves Chibon 4e1ed8
    and provides us with a way to get global identifier per project
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    Table -- global_id
Pierre-Yves Chibon 4e1ed8
    """
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    __tablename__ = 'global_id'
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 4e1ed8
    project_id = sa.Column(
Pierre-Yves Chibon 4e1ed8
        sa.Integer,
Pierre-Yves Chibon 4e1ed8
        sa.ForeignKey(
Pierre-Yves Chibon 4e1ed8
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 4e1ed8
        nullable=False)
Pierre-Yves Chibon 4e1ed8
    issue_id = sa.Column(
Pierre-Yves Chibon 4e1ed8
        sa.Integer,
Pierre-Yves Chibon 4e1ed8
        sa.ForeignKey(
Pierre-Yves Chibon 4e1ed8
            'issues.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 4e1ed8
        nullable=True)
Pierre-Yves Chibon 4e1ed8
    request_id = sa.Column(
Pierre-Yves Chibon 4e1ed8
        sa.Integer,
Pierre-Yves Chibon 4e1ed8
        sa.ForeignKey(
Pierre-Yves Chibon 4e1ed8
            'pull_requests.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 4e1ed8
        nullable=True)
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    __table_args__ = (
Pierre-Yves Chibon 4e1ed8
        # Both fields should not be NULL
Pierre-Yves Chibon 4e1ed8
        sa.CheckConstraint('NOT(request_id IS NULL AND issue_id IS NULL)'),
Pierre-Yves Chibon 4e1ed8
        # Both fields should not be not NULL (ie: only one of them should)
Pierre-Yves Chibon 4e1ed8
        sa.CheckConstraint('NOT(request_id IS NOT NULL AND issue_id IS NOT NULL)'),
Pierre-Yves Chibon 4e1ed8
    )
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
# ##########################################################
Pierre-Yves Chibon 2b1b47
# These classes are only used if you're using the `local`
Pierre-Yves Chibon 2b1b47
#                  authentication method
Pierre-Yves Chibon 2b1b47
# ##########################################################
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
class ProgitUserVisit(BASE):
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    __tablename__ = 'progit_user_visit'
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 2b1b47
    user_id = sa.Column(
Pierre-Yves Chibon 2b1b47
        sa.Integer, sa.ForeignKey('users.id'), nullable=False)
Pierre-Yves Chibon 2b1b47
    visit_key = sa.Column(
Pierre-Yves Chibon 2b1b47
        sa.String(40), nullable=False, unique=True, index=True)
Pierre-Yves Chibon 2b1b47
    user_ip = sa.Column(sa.String(50), nullable=False)
Pierre-Yves Chibon 2b1b47
    created = sa.Column(
Pierre-Yves Chibon 2b1b47
        sa.DateTime, nullable=False, default=datetime.datetime.utcnow)
Pierre-Yves Chibon 2b1b47
    expiry = sa.Column(sa.DateTime)
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
class ProgitGroup(BASE):
Pierre-Yves Chibon 2b1b47
    """
Pierre-Yves Chibon 2b1b47
    An ultra-simple group definition.
Pierre-Yves Chibon 2b1b47
    """
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    # names like "Group", "Order" and "User" are reserved words in SQL
Pierre-Yves Chibon 2b1b47
    # so we set the name to something safe for SQL
Pierre-Yves Chibon 2b1b47
    __tablename__ = 'progit_group'
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 2b1b47
    group_name = sa.Column(sa.String(16), nullable=False, unique=True)
Pierre-Yves Chibon 2b1b47
    display_name = sa.Column(sa.String(255), nullable=True)
Pierre-Yves Chibon 2b1b47
    created = sa.Column(
Pierre-Yves Chibon 2b1b47
        sa.DateTime, nullable=False, default=datetime.datetime.utcnow)
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    def __repr__(self):
Pierre-Yves Chibon 2b1b47
        ''' Return a string representation of this object. '''
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
        return 'Group: %s - name %s' % (self.id, self.group_name)
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
class ProgitUserGroup(BASE):
Pierre-Yves Chibon 2b1b47
    """
Pierre-Yves Chibon 2b1b47
    Association table linking the mm_user table to the mm_group table.
Pierre-Yves Chibon 2b1b47
    This allow linking users to groups.
Pierre-Yves Chibon 2b1b47
    """
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    __tablename__ = 'progit_user_group'
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    user_id = sa.Column(
Pierre-Yves Chibon 2b1b47
        sa.Integer, sa.ForeignKey('users.id'), primary_key=True)
Pierre-Yves Chibon 2b1b47
    group_id = sa.Column(
Pierre-Yves Chibon 2b1b47
        sa.Integer, sa.ForeignKey('progit_group.id'), primary_key=True)
Pierre-Yves Chibon 2b1b47
Pierre-Yves Chibon 2b1b47
    # Constraints
Pierre-Yves Chibon 2b1b47
    __table_args__ = (
Pierre-Yves Chibon 2b1b47
        sa.UniqueConstraint(
Pierre-Yves Chibon 2b1b47
            'user_id', 'group_id'),
Pierre-Yves Chibon 2b1b47
    )