Skip to content

Commit 872f19d

Browse files
authored
Added Trac api to receive information on specific ticket. (#1931)
Refs #1657
1 parent 9a43da9 commit 872f19d

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

tracdb/tests.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import time_machine
55
from django.test import SimpleTestCase, TestCase
6+
from django.urls import reverse
67

78
from .models import (
89
Attachment,
@@ -29,7 +30,7 @@ def test_router(self):
2930

3031

3132
class TicketTestCase(TracDBCreateDatabaseMixin, TestCase):
32-
databases = {"trac"}
33+
databases = {"default", "trac"}
3334

3435
def _create_ticket(self, custom=None, **kwargs):
3536
"""
@@ -224,6 +225,59 @@ def test_from_querystring_invalid_time(self):
224225
with self.assertRaises(ValueError):
225226
Ticket.objects.from_querystring("time=2024-10-24..")
226227

228+
def test_api_ticket_404(self):
229+
no_ticket_url = reverse("api_ticket", args=[30000])
230+
response = self.client.get(no_ticket_url)
231+
self.assertEqual(response.status_code, 404)
232+
233+
def test_api_ticket_405(self):
234+
ticket = self._create_ticket(summary="test")
235+
ticket_url = reverse("api_ticket", args=[ticket.id])
236+
post_response = self.client.post(ticket_url, {})
237+
delete_response = self.client.delete(ticket_url)
238+
self.assertEqual(post_response.status_code, 405)
239+
self.assertEqual(delete_response.status_code, 405)
240+
241+
def test_api_ticket_200(self):
242+
ticket = self._create_ticket(
243+
reporter="[email protected]",
244+
type="Bug",
245+
summary="test summary",
246+
description="test description",
247+
severity="Normal",
248+
resolution="fixed",
249+
status="assigned",
250+
custom={
251+
"stage": "Accepted",
252+
"has_patch": "1",
253+
"needs_better_patch": "0",
254+
"needs_tests": "0",
255+
},
256+
)
257+
258+
with self.assertNumQueries(1, using="trac"):
259+
response = self.client.get(reverse("api_ticket", args=[ticket.id]))
260+
261+
self.assertEqual(response.status_code, 200)
262+
self.assertJSONEqual(
263+
response.content,
264+
{
265+
"id": ticket.id,
266+
"type": "Bug",
267+
"summary": "test summary",
268+
"description": "test description",
269+
"severity": "Normal",
270+
"status": "assigned",
271+
"resolution": "fixed",
272+
"custom": {
273+
"stage": "Accepted",
274+
"has_patch": "1",
275+
"needs_better_patch": "0",
276+
"needs_tests": "0",
277+
},
278+
},
279+
)
280+
227281

228282
class TracTimeTestCase(SimpleTestCase):
229283
def test_datetime_to_timestamp(self):

tracdb/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
urlpatterns = [
66
path("bouncing/", views.bouncing_tickets, name="bouncing_tickets"),
7+
path("api/tickets/<int:ticket_id>", views.api_ticket, name="api_ticket"),
78
]

tracdb/views.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from django import db
2-
from django.shortcuts import render
2+
from django.http import JsonResponse
3+
from django.shortcuts import get_object_or_404, render
4+
from django.views.decorators.http import require_http_methods
35

6+
from .models import Ticket
47
from .tractime import timestamp_to_datetime
58

69

@@ -29,3 +32,19 @@ def bouncing_tickets(request):
2932
def dictfetchall(cursor):
3033
desc = cursor.description
3134
return [dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()]
35+
36+
37+
@require_http_methods(["GET"])
38+
def api_ticket(request, ticket_id):
39+
ticket_qs = Ticket.objects.with_custom().values(
40+
"id",
41+
"type",
42+
"summary",
43+
"description",
44+
"severity",
45+
"status",
46+
"resolution",
47+
"custom",
48+
)
49+
ticket = get_object_or_404(ticket_qs, id=ticket_id)
50+
return JsonResponse(ticket)

0 commit comments

Comments
 (0)