2017-07-22 06:28:47 +02:00
|
|
|
import sqlalchemy as sa
|
2020-01-09 18:19:44 +01:00
|
|
|
import sqlalchemy_utils as sau
|
2017-07-22 06:28:47 +02:00
|
|
|
from srht.database import Base
|
2017-09-20 02:15:18 +02:00
|
|
|
from srht.flagtype import FlagType
|
2019-01-09 12:30:26 +01:00
|
|
|
from todosrht.types import TicketAccess, TicketStatus, TicketResolution
|
2020-01-09 18:19:44 +01:00
|
|
|
from todosrht.types import TicketAuthenticity
|
2017-07-22 06:28:47 +02:00
|
|
|
|
|
|
|
class Ticket(Base):
|
|
|
|
__tablename__ = 'ticket'
|
2019-01-09 12:30:26 +01:00
|
|
|
__table_args__ = (
|
|
|
|
sa.UniqueConstraint('tracker_id', 'scoped_id',
|
|
|
|
name="uq_ticket_tracker_id_scoped_id"),
|
|
|
|
)
|
2017-07-22 06:28:47 +02:00
|
|
|
id = sa.Column(sa.Integer, primary_key=True)
|
|
|
|
created = sa.Column(sa.DateTime, nullable=False)
|
|
|
|
updated = sa.Column(sa.DateTime, nullable=False)
|
|
|
|
|
2018-12-13 04:48:43 +01:00
|
|
|
tracker_id = sa.Column(sa.Integer,
|
|
|
|
sa.ForeignKey("tracker.id", ondelete="CASCADE"),
|
|
|
|
nullable=False)
|
|
|
|
tracker = sa.orm.relationship("Tracker",
|
|
|
|
backref=sa.orm.backref("tickets", cascade="all, delete-orphan"))
|
2017-07-22 06:28:47 +02:00
|
|
|
|
2019-01-09 12:30:26 +01:00
|
|
|
scoped_id = sa.Column(sa.Integer, nullable=False, index=True)
|
2017-09-14 13:20:47 +02:00
|
|
|
|
2018-12-13 04:48:43 +01:00
|
|
|
dupe_of_id = sa.Column(sa.Integer,
|
|
|
|
sa.ForeignKey("ticket.id", ondelete="SET NULL"))
|
2017-09-13 14:17:52 +02:00
|
|
|
dupe_of = sa.orm.relationship("Ticket",
|
|
|
|
backref=sa.orm.backref("dupes"),
|
|
|
|
remote_side=[id])
|
|
|
|
|
2018-12-13 04:48:43 +01:00
|
|
|
submitter_id = sa.Column(sa.Integer,
|
2019-08-21 08:35:44 +02:00
|
|
|
sa.ForeignKey("participant.id"), nullable=False)
|
|
|
|
submitter = sa.orm.relationship("Participant",
|
2018-12-13 04:48:43 +01:00
|
|
|
backref=sa.orm.backref("submitted", cascade="all, delete-orphan"))
|
2017-07-22 06:28:47 +02:00
|
|
|
|
2017-09-13 13:39:40 +02:00
|
|
|
title = sa.Column(sa.Unicode(2048), nullable=False)
|
2017-09-14 04:09:32 +02:00
|
|
|
description = sa.Column(sa.Unicode(16384))
|
2021-09-13 20:19:55 +02:00
|
|
|
comment_count = sa.Column(sa.Integer, default=0, nullable=False, index=True)
|
2017-09-13 13:39:40 +02:00
|
|
|
|
2017-09-13 14:17:52 +02:00
|
|
|
status = sa.Column(FlagType(TicketStatus),
|
|
|
|
nullable=False,
|
|
|
|
default=TicketStatus.reported)
|
|
|
|
|
|
|
|
resolution = sa.Column(FlagType(TicketResolution),
|
|
|
|
nullable=False,
|
2018-10-28 11:52:13 +01:00
|
|
|
default=TicketResolution.unresolved)
|
2017-09-13 14:17:52 +02:00
|
|
|
|
2019-05-21 13:47:17 +02:00
|
|
|
|
|
|
|
labels = sa.orm.relationship("Label",
|
2021-07-26 11:21:18 +02:00
|
|
|
secondary="ticket_label", order_by="Label.name",
|
|
|
|
viewonly=True)
|
2018-11-29 16:22:55 +01:00
|
|
|
|
|
|
|
assigned_users = sa.orm.relationship("User",
|
|
|
|
secondary="ticket_assignee",
|
2021-07-26 11:21:18 +02:00
|
|
|
foreign_keys="[TicketAssignee.ticket_id,TicketAssignee.assignee_id]",
|
|
|
|
viewonly=True)
|
2019-02-24 19:34:59 +01:00
|
|
|
|
2020-01-09 18:19:44 +01:00
|
|
|
authenticity = sa.Column(
|
|
|
|
sau.ChoiceType(TicketAuthenticity, impl=sa.Integer()),
|
|
|
|
nullable=False, server_default="0")
|
|
|
|
"""
|
|
|
|
The authenticity of the ticket. Tickets submitted by logged-in users are
|
|
|
|
considered authentic. Tickets which have been exported and re-imported are
|
|
|
|
considered authentic if the signature validates, unauthenticated if no
|
|
|
|
signature is present, or tampered if the signature does not validate.
|
|
|
|
"""
|
|
|
|
|
2019-04-30 00:26:36 +02:00
|
|
|
def ref(self, short=False, email=False):
|
2019-02-26 02:30:32 +01:00
|
|
|
if short:
|
|
|
|
return "#" + str(self.scoped_id)
|
2019-04-30 00:26:36 +02:00
|
|
|
if email:
|
|
|
|
return "{}/{}/{}".format(
|
|
|
|
self.tracker.owner.canonical_name,
|
|
|
|
self.tracker.name,
|
|
|
|
self.scoped_id)
|
2019-02-24 19:34:59 +01:00
|
|
|
return "{}/{}#{}".format(
|
|
|
|
self.tracker.owner.canonical_name,
|
|
|
|
self.tracker.name,
|
|
|
|
self.scoped_id)
|
2019-03-07 08:02:57 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<Ticket {self.id}>"
|
2019-04-30 22:17:43 +02:00
|
|
|
|
|
|
|
def to_dict(self, short=False):
|
|
|
|
def permissions(w):
|
|
|
|
return [p.name for p in TicketAccess
|
|
|
|
if p in w and p not in [TicketAccess.none, TicketAccess.all]]
|
|
|
|
return {
|
|
|
|
"id": self.scoped_id,
|
|
|
|
"ref": self.ref(),
|
|
|
|
"tracker": self.tracker.to_dict(short=True),
|
2020-04-24 17:10:19 +02:00
|
|
|
"title": self.title,
|
2019-04-30 22:17:43 +02:00
|
|
|
**({
|
|
|
|
"created": self.created,
|
|
|
|
"updated": self.updated,
|
|
|
|
"submitter": self.submitter.to_dict(short=True),
|
|
|
|
"description": self.description,
|
|
|
|
"status": self.status.name,
|
|
|
|
"resolution": self.resolution.name,
|
|
|
|
"labels": [l.name for l in self.labels],
|
|
|
|
"assignees": [u.to_dict(short=True) for u in self.assigned_users],
|
|
|
|
} if not short else {}),
|
|
|
|
}
|