Add ticket subscription management

This commit is contained in:
Drew DeVault 2018-07-14 15:13:52 -04:00
parent 6492749dd4
commit 2c6033c3f1
2 changed files with 137 additions and 27 deletions

View File

@ -19,7 +19,7 @@ ticket = Blueprint("ticket", __name__)
smtp_user = cfg("mail", "smtp-user", default=None)
@ticket.route("/<owner>/<path:name>/<int:ticket_id>")
@ticket.route("/<owner>/<name>/<int:ticket_id>")
def ticket_GET(owner, name, ticket_id):
tracker, _ = get_tracker(owner, name)
if not tracker:
@ -27,6 +27,7 @@ def ticket_GET(owner, name, ticket_id):
ticket, access = get_ticket(tracker, ticket_id)
if not ticket:
abort(404)
is_subscribed = False
if current_user:
seen = (TicketSeen.query
.filter(TicketSeen.user_id == current_user.id,
@ -37,12 +38,78 @@ def ticket_GET(owner, name, ticket_id):
seen.update()
db.session.add(seen)
db.session.commit()
return render_template("ticket.html",
tracker=tracker,
ticket=ticket,
access=access)
@ticket.route("/<owner>/<path:name>/<int:ticket_id>/comment", methods=["POST"])
tracker_sub = (TicketSubscription.query
.filter(TicketSubscription.ticket_id == None)
.filter(TicketSubscription.tracker_id == tracker.id)
.filter(TicketSubscription.user_id == current_user.id)
).one_or_none()
sub = (TicketSubscription.query
.filter(TicketSubscription.tracker_id == None)
.filter(TicketSubscription.ticket_id == ticket.id)
.filter(TicketSubscription.user_id == current_user.id)
).one_or_none()
is_subscribed = bool(sub)
return render_template("ticket.html", tracker=tracker, ticket=ticket,
access=access, is_subscribed=is_subscribed, tracker_sub=tracker_sub)
@ticket.route("/<owner>/<name>/<int:ticket_id>/enable_notifications", methods=["POST"])
@loginrequired
def enable_notifications(owner, name, ticket_id):
tracker, access = get_tracker(owner, name)
if not tracker:
abort(404)
ticket, access = get_ticket(tracker, ticket_id)
if not ticket:
abort(404)
sub = (TicketSubscription.query
.filter(TicketSubscription.tracker_id == None)
.filter(TicketSubscription.ticket_id == ticket.id)
.filter(TicketSubscription.user_id == current_user.id)
).one_or_none()
if sub:
return redirect(url_for(".ticket_GET",
owner=owner, name=name, ticket_id=ticket.scoped_id))
sub = TicketSubscription()
sub.ticket_id = ticket.id
sub.user_id = current_user.id
db.session.add(sub)
db.session.commit()
return redirect(url_for(".ticket_GET",
owner=owner, name=name, ticket_id=ticket.scoped_id))
@ticket.route("/<owner>/<name>/<int:ticket_id>/disable_notifications", methods=["POST"])
@loginrequired
def disable_notifications(owner, name, ticket_id):
tracker, access = get_tracker(owner, name)
if not tracker:
abort(404)
ticket, access = get_ticket(tracker, ticket_id)
if not ticket:
abort(404)
sub = (TicketSubscription.query
.filter(TicketSubscription.tracker_id == None)
.filter(TicketSubscription.ticket_id == ticket.id)
.filter(TicketSubscription.user_id == current_user.id)
).one_or_none()
if not sub:
return redirect(url_for(".ticket_GET",
owner=owner, name=name, ticket_id=ticket.scoped_id))
db.session.delete(sub)
db.session.commit()
return redirect(url_for(".ticket_GET",
owner=owner, name=name, ticket_id=ticket.scoped_id))
@ticket.route("/<owner>/<name>/<int:ticket_id>/comment", methods=["POST"])
@loginrequired
def ticket_comment_POST(owner, name, ticket_id):
tracker, access = get_tracker(owner, name)
@ -188,7 +255,7 @@ def ticket_comment_POST(owner, name, ticket_id):
return redirect(ticket_url)
@ticket.route("/<owner>/<path:name>/<int:ticket_id>/edit")
@ticket.route("/<owner>/<name>/<int:ticket_id>/edit")
@loginrequired
def ticket_edit_GET(owner, name, ticket_id):
tracker, _ = get_tracker(owner, name)
@ -202,7 +269,7 @@ def ticket_edit_GET(owner, name, ticket_id):
return render_template("edit_ticket.html",
tracker=tracker, ticket=ticket)
@ticket.route("/<owner>/<path:name>/<int:ticket_id>/edit", methods=["POST"])
@ticket.route("/<owner>/<name>/<int:ticket_id>/edit", methods=["POST"])
@loginrequired
def ticket_edit_POST(owner, name, ticket_id):
tracker, _ = get_tracker(owner, name)

View File

@ -10,7 +10,7 @@
{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-md-12 header-tabbed vertical">
<div class="col-md-12">
<h2>
<span class="d-block d-md-inline">
<a href="{{ url_for("html.user_GET",
@ -27,24 +27,67 @@
{{ticket.title}}
</span>
</h2>
<ul class="nav nav-tabs">
<li class="nav-item">
<a href="{{ url_for(".ticket_GET",
owner=tracker.owner.canonical_name(),
name=tracker.name,
ticket_id=ticket.scoped_id) }}"
class="nav-link active">view</a>
</li>
{% if TicketAccess.edit in access %}
<li class="nav-item">
<a href="{{ url_for(".ticket_edit_GET",
owner=tracker.owner.canonical_name(),
name=tracker.name,
ticket_id=ticket.scoped_id) }}"
class="nav-link">edit</a>
</li>
{% endif %}
</ul>
</div>
</div>
</div>
<div class="header-tabbed">
<div class="container">
<div class="row">
<div class="col-md-12 header-tabbed" style="border: none; margin: 0;">
<ul class="nav nav-tabs">
<li class="nav-item">
<a href="{{ url_for(".ticket_GET",
owner=tracker.owner.canonical_name(),
name=tracker.name,
ticket_id=ticket.scoped_id) }}"
class="nav-link active">view</a>
</li>
{% if TicketAccess.edit in access %}
<li class="nav-item">
<a href="{{ url_for(".ticket_edit_GET",
owner=tracker.owner.canonical_name(),
name=tracker.name,
ticket_id=ticket.scoped_id) }}"
class="nav-link">edit</a>
</li>
{% endif %}
{% if current_user %}
<li class="flex-grow-1 d-none d-sm-block"></li>
<li class="nav-item d-none d-sm-block">
{% if not tracker_sub %}
<form method="POST" action="{{url_for("ticket." +
("disable_notifications" if is_subscribed else "enable_notifications"),
owner=tracker.owner.canonical_name(),
name=tracker.name,
ticket_id=ticket.scoped_id)}}">
{% else %}
<div>
{% endif %}
<button
class="nav-link active"
type="submit"
{% if tracker_sub %}
title="You are subscribed to all activity on this tracker"
disabled
{% endif %}
>
<i class="fa fa-envelope-o"></i>
{% if is_subscribed or tracker_sub %}
Disable notifications
{% else %}
Enable notifications
{% endif %}
<i class="fa fa-caret-right"></i>
</button>
{% if not tracker_sub %}
</form>
{% else %}
</div>
{% endif %}
</li>
{% endif %}
</ul>
</div>
</div>
</div>
</div>