Improve ORM relationships to and from Labels

* rename all backrefs to TicketLabel to 'ticket_labels'
* add Ticket.labels relationship through TicketLabel secondary
* add Label.tickets relationship through TicketLabel secondary

This avoids confusion as accessing 'ticket_labels' property will return
TicketLabel objects, and accessing 'labels' will return Label objects.

Also makes the code less verbose.
This commit is contained in:
Ivan Habunek 2018-10-24 15:49:24 +02:00 committed by Drew DeVault
parent 5f3b06df4b
commit d46dd6094f
5 changed files with 21 additions and 19 deletions

View File

@ -57,12 +57,8 @@ def ticket_GET(owner, name, ticket_id):
is_subscribed = bool(sub)
ticket_labels = TicketLabel.query.filter(TicketLabel.ticket_id == ticket.id)
labels = [tl.label for tl in ticket_labels]
return render_template("ticket.html", tracker=tracker, ticket=ticket,
access=access, is_subscribed=is_subscribed, tracker_sub=tracker_sub,
labels=labels)
access=access, is_subscribed=is_subscribed, tracker_sub=tracker_sub)
@ticket.route("/<owner>/<name>/<int:ticket_id>/enable_notifications", methods=["POST"])
@loginrequired

View File

@ -125,20 +125,20 @@
</dd>
<dt class="col-md-3">Labels</dt>
<dd class="col-md-9">
{% for tl in ticket.labels %}
{% for label in ticket.labels %}
<div class="label-edit">
<div
class="label"
style="color: {{ tl.label.text_color }};
background-color: {{ tl.label.color }}"
style="color: {{ label.text_color }};
background-color: {{ label.color }}"
>
<a href="{{
url_for("tracker.tracker_GET",
owner=tracker.owner.canonical_name(),
name=tracker.name)
}}?search=label:&quot;{{ tl.label.name|urlencode }}&quot;"
}}?search=label:&quot;{{ label.name|urlencode }}&quot;"
style="color: inherit"
>{{ tl.label.name }}
>{{ label.name }}
</a>
<form
method="POST"
@ -147,7 +147,7 @@
owner=tracker.owner.canonical_name(),
name=tracker.name,
ticket_id=ticket.scoped_id,
label_id=tl.label.id,
label_id=label.id,
)
}}">
<button type="submit" class="btn btn-link">
@ -172,7 +172,7 @@
}}">
<select id="label_id" name="label_id" class="form-control">
<option>-- Pick one --</option>
{% for label in tracker.labels if label not in labels %}
{% for label in tracker.labels if label not in ticket.labels %}
<option
value="{{ label.id }}"
style="color: {{ label.text_color }};

View File

@ -149,12 +149,12 @@
<div class="title">
{{ ticket.title }}
<span class="pull-right">
{% for tl in ticket.labels %}
<a href="?search=label:&quot;{{ tl.label.name|urlencode }}&quot;"
{% for label in ticket.labels %}
<a href="?search=label:&quot;{{ label.name|urlencode }}&quot;"
class="small label"
style="color: {{ tl.label.text_color }};
background-color: {{ tl.label.color }}"
>{{ tl.label.name }}
style="color: {{ label.text_color }};
background-color: {{ label.color }}"
>{{ label.name }}
</a>
{% endfor %}
</span>

View File

@ -16,6 +16,8 @@ class Label(Base):
color = sa.Column(sa.Text, nullable=False)
text_color = sa.Column(sa.Text, nullable=False)
tickets = sa.orm.relationship("Ticket", secondary="ticket_label")
__table_args__ = (
sa.UniqueConstraint("tracker_id", "name",
name="idx_tracker_name_unique"),
@ -28,11 +30,13 @@ class TicketLabel(Base):
__tablename__ = 'ticket_label'
ticket_id = sa.Column(sa.Integer,
sa.ForeignKey('ticket.id'), primary_key=True)
ticket = sa.orm.relationship("Ticket", backref=sa.orm.backref("labels"))
ticket = sa.orm.relationship("Ticket",
backref=sa.orm.backref("ticket_labels"))
label_id = sa.Column(sa.Integer,
sa.ForeignKey('label.id'), primary_key=True)
label = sa.orm.relationship("Label", backref=sa.orm.backref("tickets"))
label = sa.orm.relationship("Label",
backref=sa.orm.backref("ticket_labels"))
user_id = sa.Column(sa.Integer, sa.ForeignKey("user.id"), nullable=False)
user = sa.orm.relationship("User", backref=sa.orm.backref("ticket_labels"))

View File

@ -50,6 +50,8 @@ class Ticket(Base):
"""Permissions granted to anonymous (non-logged in) users"""
view_list = sa.orm.relationship("TicketSeen")
labels = sa.orm.relationship("Label", secondary="ticket_label")
def new_updates(self, user):
if not user:
return None