Ejemplo n.º 1
0
class Status(Base):
    name          = db.Column(db.String(100), nullable=False)
    status_type   = db.Column(db.Enum("TODO", "IN_PROGRESS", "DONE", name='status_types'),
                              nullable=False)
    next_statuses = db.relationship('Status', 
                                    secondary=status_relationships,
                                    primaryjoin="Status.id == status_relationships.c.status_id",
                                    secondaryjoin="Status.id == status_relationships.c.next_status_id", 
                                    backref='previous_statuses',
                                    lazy='dynamic')

    tickets = db.relationship('Ticket', backref='status', lazy='dynamic')

    def __init__(self, *, name, status_type=0):
        self.name        = name
        self.status_type = status_type

    def to_json(self):
        return super().to_json(ignoreFields=["created_at", "updated_at"])

    def get_next(self):
        return self.next_statuses.all()
    
    def get_previous(self):
        return self.previous_statuses.all()
Ejemplo n.º 2
0
class Project(Base):
    """Project is a container for tickets."""
    pkey = db.Column(db.String(6), nullable=False, unique=True)
    name = db.Column(db.String(250), nullable=False)
    repo = db.Column(db.String(250))
    homepage = db.Column(db.String(250))

    project_lead_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    team_id = db.Column(db.Integer, db.ForeignKey('team.id'))

    tickets = db.relationship('Ticket', backref='project', lazy='dynamic')
    members = db.relationship('Membership', backref='project', lazy='dynamic')

    def __init__(self, *, pkey, name, repo='', homepage=''):
        self.pkey = pkey.upper()
        self.name = name
        self.repo = repo
        self.homepage = homepage

    def get_by_key(team_slug, pkey, preload=False):
        p = Project.query.\
                join(Project.team).\
                filter(Team.url_slug == team_slug).\
                filter(Project.pkey == pkey).\
                first()
        if p == None:
            raise AppError(status_code=404,
                           message="That project does not exist.")
        return p

    def __repr__(self):
        return "<Project %r>" % (self.pkey)
Ejemplo n.º 3
0
class Ticket(Base):
    """A ticket is a unit of work for a project, be it a bug or support ticket."""
    ticket_key  = db.Column(db.String(100), nullable=False, unique=True) # I mean jesus christ how many digits
    summary     = db.Column(db.String(250), nullable=False)
    description = db.Column(db.Text())

    assignee_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    reporter_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    project_id  = db.Column(db.Integer, db.ForeignKey('project.id'))
    status_id   = db.Column(db.Integer, db.ForeignKey('status.id'))

    fields      = db.relationship('FieldValue', backref='ticket')
    comments    = db.relationship('Comment', backref='ticket', lazy='dynamic')

    def __init__(self, *, ticket_key, summary, description, assignee_id=None, reporter_id=None):
        self.ticket_key  = ticket_key
        self.summary     = summary
        self.description = description
        self.assignee_id = assignee_id
        self.reporter_id = reporter_id

    def get_by_id(i, preload=''):
        tk = Ticket.query.\
                options(joinedload(Ticket.status)).\
                options(joinedload(Ticket.reporter)).\
                options(joinedload(Ticket.assignee)).\
                filter(Ticket.id == i)
        return tk.first()

    def get_by_key(team_slug, pkey, ticket_key, preload=''):
        tk = Ticket.query.\
                options(joinedload(Ticket.status)).\
                options(joinedload(Ticket.reporter)).\
                options(joinedload(Ticket.assignee)).\
                join(Ticket.project).\
                join(Project.team).\
                filter(Team.url_slug == team_slug).\
                filter(Project.pkey == pkey).\
                filter(Ticket.ticket_key == ticket_key)
        if 'project' in preload.lower():
            tk = tk.options(joinedload(Ticket.project))
        return tk.first()

    def from_json(pkey, json):
        validate(json, ticket_schema)
        prjct = Project.get_by_key(pkey)
        r = User.get_by_username_or_id(json.get("reporter", {}).get("username", ""))
        a = User.get_by_username_or_id(json.get("assignee", {}).get("username", ""))
        tk = Ticket(summary=json['summary'],
                   description=json['description'],
                   ticket_key=prjct.pkey + "-" + str(len(prjct.tickets) + 1),
                   reporter_id=r.id,
                   project_id=prjct.id)
        if a != None:
            tk.assignee_id = a.id
        return tk

    def __repr__(self):
        return '<Ticket %r>' % (self.ticket_key)
Ejemplo n.º 4
0
class Project(Base):
    """Project is a container for tickets."""
    pkey     = db.Column(db.String(6), nullable=False, unique=True)
    name     = db.Column(db.String(250), nullable=False)
    repo     = db.Column(db.String(250))
    homepage = db.Column(db.String(250))

    project_lead_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    team_id         = db.Column(db.Integer, db.ForeignKey('team.id'))

    tickets = db.relationship('Ticket', backref='project')
    members = db.relationship('Membership', backref='project', lazy='dynamic')

    def __init__(self, *, pkey, name, repo='', homepage=''):
        self.pkey     = pkey.upper()
        self.name     = name
        self.repo     = repo
        self.homepage = homepage

    def get_by_id(i, preload=''):
       p = Project.query.\
                options(joinedload(Project.project_lead)).\
                filter(Project.id == i).\
                first()
       return p

    def get_by_key(team_slug, pkey, preload=''):
        p = Project.query.\
                options(joinedload(Project.project_lead)).\
                join(Project.team).\
                filter(Team.url_slug == team_slug).\
                filter(Project.pkey == pkey).\
                first()
        return p

    def from_json(team_slug, jsn):
        validate(jsn, project_schema)
        t = Team.get_by_slug(team_slug)
        p = Project(pkey=jsn["pkey"],
                    name=jsn["name"],
                    homepage=jsn.get("homepage", ""),
                    repo=jsn.get("repo", ""))
        p.team = t
        return p

    def to_json(self):
        return super().to_json(ignoreFields=["updated_at"])

    def __repr__(self):
        return "<Project %r>" % (self.pkey)
Ejemplo n.º 5
0
class Team(Base):
    """Team is a container for projects.
    
    When changing the name for a team use set_name as this properly updates
    dependent fields for Team. YOU WILL HAVE A BAD TIME IF YOU DO team.name =
    SOME_NAME.
    """
    name = db.Column(db.String(120), nullable=False, unique=True)
    url_slug = db.Column(db.String(150), nullable=False, unique=True)
    icon = db.Column(db.String(150))

    team_lead_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    projects = db.relationship('Project', backref='team', lazy='dynamic')
    members = db.relationship('Membership', backref='team', lazy='dynamic')

    def __init__(self, *, name, icon=""):
        self.name = name
        self.url_slug = name.lower().replace(" ", "-")
        self.icon = icon

    def set_name(self, name):
        self.name = name
        self.url_slug = name.lower().replace(" ", "-")

    def from_json(json):
        validate(json, team_schema)
        t = Team(name=json["name"], icone=json.get("icon", ""))
        un = json.get("project_lead", {}).get("username", "")
        lead = User.query.filter_by(username=un).first()
        t.team_lead = lead
        return t

    def get_by_name_or_stub(name):
        t = Team.query.filter(or_(Team.name == name,
                                  Team.url_stub == name)).first()
        if t == None:
            raise AppError(status_code=404, message="That team does not exist")
        return t

    def from_json(json):
        validate(json, team_schema)
        t = Team(name=json["name"], icone=json.get("icon", ""))
        un = json.get("project_lead", {}).get("username", "")
        lead = User.query.filter_by(username=un).first()
        t.team_lead = lead
        return t

    def __repr__(self):
        return "<Team %r>" % (self.name)
Ejemplo n.º 6
0
class Team(Base):
    """Team is a container for projects.
    
    When changing the name for a team use set_name as this properly updates
    dependent fields for Team. YOU WILL HAVE A BAD TIME IF YOU DO team.name =
    SOME_NAME.
    """
    name = db.Column(db.String(120), nullable=False, unique=True)
    url_slug = db.Column(db.String(150), nullable=False, unique=True)
    icon = db.Column(db.String(150))

    team_lead_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    projects = db.relationship('Project', backref='team', lazy='dynamic')
    members = db.relationship('Membership', backref='team', lazy='dynamic')

    def __init__(self, *, name, icon=''):
        self.name = name
        self.url_slug = name.lower().replace(' ', '-')
        self.icon = icon

    def set_name(self, name):
        self.name = name
        self.url_slug = name.lower().replace(' ', '-')

    def from_json(json):
        validate(json, team_create_schema)
        t = Team(name=json['name'], icon=json.get('icon', ''))
        un = json.get('project_lead', {}).get('username', '')
        lead = User.query.filter_by(username=un).first()
        t.team_lead = lead
        return t

    def to_json(self):
        return super().to_json(ignoreFields=["updated_at"])

    def get_by_name_or_stub(name):
        t = Team.query.\
                options(joinedload(Team.team_lead)).\
                filter(or_(Team.name == name,
                           Team.url_slug == name)).first()
        print(t.team_lead)
        if t == None:
            raise AppError(status_code=404, message='That team does not exist')
        return t

    def __repr__(self):
        return '<Team %r>' % (self.name)
Ejemplo n.º 7
0
class User(Base):
    """User represents a user of our application."""
    __tablename__ = "users"

    full_name = db.Column(db.String(250), nullable=False)
    email = db.Column(db.String(128), nullable=False, unique=True)
    # TODO: Move this into memberships
    is_admin = db.Column(db.Boolean)
    username = db.Column(db.String(128), nullable=False, unique=True)
    password = db.Column(db.String(192), nullable=False)

    comments_author_of = db.relationship('Comment',
                                         backref='author',
                                         lazy='dynamic')
    projects_lead_of = db.relationship('Project',
                                       backref='project_lead',
                                       lazy='dynamic')
    teams_lead_of = db.relationship('Team',
                                    backref='team_lead',
                                    lazy='dynamic')
    memberships = db.relationship('Membership', backref='user', lazy='dynamic')
    assigned_tickets = db.relationship(
        'Ticket',
        backref='assignee',
        lazy='dynamic',
        primaryjoin='Ticket.assignee_id == User.id')
    reported_tickets = db.relationship(
        'Ticket',
        backref='reporter',
        lazy='dynamic',
        primaryjoin='Ticket.reporter_id == User.id')

    teams = association_proxy('membership', 'team')
    projects = association_proxy('membership', 'project')

    def __init__(self,
                 *,
                 username,
                 email,
                 password,
                 full_name,
                 is_admin=False):
        self.full_name = full_name
        self.username = username
        self.email = email
        self.is_admin = is_admin
        self.set_password(password)

    def get_by_username_or_id(param):
        u = User.query.\
                filter(User.username == param or User.id == param).\
                first()
        return u

    def from_json(json):
        validate(json, user_signup_schema)
        u = User(username=json['username'],
                 password=json['password'],
                 full_name=json['fullName'],
                 email=json['email'])
        return u

    def to_json(self):
        return super().to_json(
            ignoreFields=["password", "is_admin", "created_at", "updated_at"])

    def update(self, json):
        validate(self, user_schema)
        self.username = json.get('username', self.username)
        self.full_name = json.get('fullName', self.full_name)
        self.email = json.get('email', self.email)

        if json.get('password', None) != None:
            self.set_password(json['password'])

    def set_password(self, pw):
        self.password = generate_password_hash(pw)

    def check_password(self, pw):
        return check_password_hash(self.password, pw)

    def __repr__(self):
        return '<User %r>' % (self.username)
Ejemplo n.º 8
0
class User(Base):
    """User represents a user of our application."""
    full_name = db.Column(db.String(250), nullable=False)
    email = db.Column(db.String(128), nullable=False, unique=True)
    # TODO: Move this into memberships
    is_admin = db.Column(db.Boolean)
    username = db.Column(db.String(128), nullable=False, unique=True)
    password = db.Column(db.String(192), nullable=False)

    projects_lead_of = db.relationship('Project',
                                       backref='project_lead',
                                       lazy='dynamic')
    teams_lead_of = db.relationship('Team',
                                    backref='team_lead',
                                    lazy='dynamic')
    memberships = db.relationship('Membership', backref='user', lazy='dynamic')
    assigned_tickets = db.relationship(
        'Ticket',
        backref='assignee',
        lazy='dynamic',
        primaryjoin="Ticket.assignee_id == User.id")
    reported_tickets = db.relationship(
        'Ticket',
        backref='reporter',
        lazy='dynamic',
        primaryjoin="Ticket.reporter_id == User.id")

    teams = association_proxy('membership', 'team')
    projects = association_proxy('membership', 'project')

    def __init__(self,
                 *,
                 username,
                 email,
                 password,
                 full_name,
                 is_admin=False):
        self.full_name = full_name
        self.username = username
        self.email = email
        self.is_admin = is_admin
        self.set_password(password)

    def get_by_username_or_id(param):
        try:
            i = int(param)
            u = User.query.filter(User.id == i).first()
        except:
            u = User.query.filter_by(username=param).first()
        if u == None:
            raise AppError(status_code=404, message="User not found.")
        return u

    def from_json(json):
        validate(json, user_schema)
        u = User(username=json["username"],
                 password=json["password"],
                 full_name=json["fullName"],
                 email=json["email"])
        return u

    def to_json(self):
        """Extends base class to_json to drop password as well."""
        s = super().to_json()
        s.pop('password', None)
        return s

    def update(self, json):
        self.username = json.get("username", self.username)
        self.full_name = json.get("fullName", self.full_name)
        self.email = json.get("email", self.email)

        if json.get("password", None) != None:
            self.set_password(json["password"])

    def set_password(self, pw):
        self.password = generate_password_hash(pw)

    def check_password(self, pw):
        return check_password_hash(self.password, pw)

    def __repr__(self):
        return "<User %r>" % (self.username)