Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jelle van der Waa
Archweb
Commits
411e1c54
Commit
411e1c54
authored
May 19, 2018
by
Jelle van der Waa
🚧
Browse files
todolists: Add json endpoint for todo detail view
Add a /todo/$name/json endpoint which returns the json for a todo list item.
parent
bf3e96dc
Changes
3
Hide whitespace changes
Inline
Side-by-side
todolists/tests/test_views.py
View file @
411e1c54
...
...
@@ -27,3 +27,9 @@ def test_todolist_detail(self):
response
=
self
.
client
.
get
(
self
.
todolist
.
get_absolute_url
())
self
.
assertEqual
(
response
.
status_code
,
200
)
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
)
todolists/urls.py
View file @
411e1c54
from
django.conf.urls
import
url
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
)
urlpatterns
=
[
...
...
@@ -13,6 +13,7 @@
url
(
r
'^add/$'
,
permission_required
(
'todolists.add_todolist'
)(
add
)),
url
(
r
'^(?P<slug>[-\w]+)/$'
,
view
),
url
(
r
'^(?P<slug>[-\w]+)/json$'
,
view_json
),
url
(
r
'^(?P<slug>[-\w]+)/edit/$'
,
permission_required
(
'todolists.change_todolist'
)(
edit
)),
url
(
r
'^(?P<slug>[-\w]+)/delete/$'
,
...
...
todolists/views.py
View file @
411e1c54
...
...
@@ -14,7 +14,7 @@
from
main.models
import
Package
,
Repo
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
.utils
import
get_annotated_todolists
,
attach_staging
...
...
@@ -236,4 +236,23 @@ def send_todolist_emails(todo_list, new_packages):
[
maint
],
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:
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment