Unverified Commit f8958f8f authored by Jelle van der Waa's avatar Jelle van der Waa 🚧 Committed by GitHub
Browse files

Merge pull request #126 from jelly/todolist_json

todolists: Add json endpoint for todo detail view
parents bf3e96dc 411e1c54
...@@ -27,3 +27,9 @@ def test_todolist_detail(self): ...@@ -27,3 +27,9 @@ def test_todolist_detail(self):
response = self.client.get(self.todolist.get_absolute_url()) response = self.client.get(self.todolist.get_absolute_url())
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertIn(self.todolist.name, response.content) self.assertIn(self.todolist.name, response.content)
def test_todolist_json(self):
response = self.client.get(self.todolist.get_absolute_url() + 'json')
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data['name'], self.todolist.name)
from django.conf.urls import url from django.conf.urls import url
from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required
from .views import (view_redirect, view, add, edit, flag, from .views import (view_redirect, view, view_json, add, edit, flag,
list_pkgbases, DeleteTodolist, TodolistListView) list_pkgbases, DeleteTodolist, TodolistListView)
urlpatterns = [ urlpatterns = [
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
url(r'^add/$', url(r'^add/$',
permission_required('todolists.add_todolist')(add)), permission_required('todolists.add_todolist')(add)),
url(r'^(?P<slug>[-\w]+)/$', view), url(r'^(?P<slug>[-\w]+)/$', view),
url(r'^(?P<slug>[-\w]+)/json$', view_json),
url(r'^(?P<slug>[-\w]+)/edit/$', url(r'^(?P<slug>[-\w]+)/edit/$',
permission_required('todolists.change_todolist')(edit)), permission_required('todolists.change_todolist')(edit)),
url(r'^(?P<slug>[-\w]+)/delete/$', url(r'^(?P<slug>[-\w]+)/delete/$',
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
from main.models import Package, Repo from main.models import Package, Repo
from main.utils import find_unique_slug from main.utils import find_unique_slug
from packages.utils import attach_maintainers from packages.utils import attach_maintainers, PackageJSONEncoder
from .models import Todolist, TodolistPackage from .models import Todolist, TodolistPackage
from .utils import get_annotated_todolists, attach_staging from .utils import get_annotated_todolists, attach_staging
...@@ -236,4 +236,23 @@ def send_todolist_emails(todo_list, new_packages): ...@@ -236,4 +236,23 @@ def send_todolist_emails(todo_list, new_packages):
[maint], [maint],
fail_silently=True) fail_silently=True)
class TodoListJSONEncoder(PackageJSONEncoder):
def default(self, obj):
if isinstance(obj, Todolist):
return {
'id': obj.pk,
'name': obj.name,
'description': obj.description,
'packages': [pkg.pkg for pkg in obj.packages()],
}
return super(TodoListJSONEncoder, self).default(obj)
def view_json(request, slug):
todolist = get_object_or_404(Todolist, slug=slug)
to_json = json.dumps(todolist, ensure_ascii=False, cls=TodoListJSONEncoder)
return HttpResponse(to_json, content_type='application/json')
# vim: set ts=4 sw=4 et: # vim: set ts=4 sw=4 et:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment