Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Added basic API endpoint to get ticket info #1656

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions tracdb/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,63 @@ def test_from_querystring_model_and_custom_field_together(self):
Ticket.objects.from_querystring("severity=high&stage=unreviewed"),
["test1"],
)


class TicketAPITestCase(TracDBCreateDatabaseMixin, TestCase):
databases = {"default", "trac"}

def test_empty_results(self):
response = self.client.get("/trac/api/?ids=1")
self.assertJSONEqual(response.content, {"tickets": []})

def test_single_result(self):
t = Ticket.objects.create(reporter="test", description="test")

response = self.client.get(f"/trac/api/?ids={t.id}")

self.assertJSONEqual(
response.content,
{
"tickets": [
{
"id": t.id,
"reporter": "test",
"description": "test",
"resolution": "",
"status": "",
"changes": [],
}
]
},
)

def test_single_result_with_comment(self):
t = Ticket.objects.create()
t.changes.create(author="test", field="test", _time=1)

response = self.client.get(f"/trac/api/?ids={t.id}")

self.assertJSONEqual(
response.content,
{
"tickets": [
{
"id": t.id,
"reporter": "",
"description": "",
"resolution": "",
"status": "",
"changes": [
{
"author": "test",
"field": "test",
"time": "1970-01-01T00:00:00.000001+00:00",
}
],
}
]
},
)

def test_querycount(self):
pass # TODO (make sure there's no N+1 issue)
1 change: 1 addition & 0 deletions tracdb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

urlpatterns = [
path("bouncing/", views.bouncing_tickets, name="bouncing_tickets"),
path("api/", views.miniapi, name="miniapi"),
]
41 changes: 40 additions & 1 deletion tracdb/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime

from django import db
from django.http import JsonResponse
from django.shortcuts import render

from .models import _epoc
from .models import Ticket, _epoc


def bouncing_tickets(request):
Expand Down Expand Up @@ -35,3 +36,41 @@ def ts2dt(ts):
def dictfetchall(cursor):
desc = cursor.description
return [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]


def miniapi(request):
"""
Return information about the requestest tickets (JSON)
"""
# TODO: max size
# TODO: paginate?
# TODO: error handling
pks = request.GET.get("ids")
tickets = (
Ticket.objects.filter(pk__in=pks)
.only("status", "reporter", "resolution", "description")
.prefetch_related("changes")
)

return JsonResponse(
{
"tickets": [
{
"id": ticket.pk,
"status": ticket.status,
"reporter": ticket.reporter, # TODO: sanitize (emails, ...)
"resolution": ticket.resolution,
"description": ticket.description,
"changes": [
{
"time": change.time.isoformat(),
"author": change.author, # TODO: sanitize (emails, ...)
"field": change.field,
}
for change in ticket.changes.all()
],
}
for ticket in tickets
]
}
)