add json feed for bisac heading tree

pull/1/head
eric 2015-09-11 00:09:37 -04:00
parent 85284f7fca
commit 1df3cfee79
4 changed files with 38 additions and 0 deletions

View File

@ -10,6 +10,9 @@ class BisacHeading(MPTTModel):
class MPTTMeta:
order_insertion_by = ['notation']
def __unicode__(self):
return self.full_label
def populate_bisac_headings():
for key in bisac.keys():

5
bisac/urls.py Normal file
View File

@ -0,0 +1,5 @@
from django.conf.urls.defaults import *
urlpatterns = patterns("",
url(r"^tree$", "regluit.bisac.views.tree"),
)

29
bisac/views.py Normal file
View File

@ -0,0 +1,29 @@
import json
from django.template import Context, Template
from django.http import HttpResponse
from mptt.templatetags.mptt_tags import cache_tree_children
from regluit.bisac.models import BisacHeading
nodes = BisacHeading.objects.all()
def recursive_node_to_dict(node):
result = {
'title': node.full_label,
'key': node.pk,
}
children = [recursive_node_to_dict(c) for c in node.get_children()]
if children:
result['children'] = children
result['folder'] = True
return result
root_nodes = cache_tree_children(nodes)
dicts = []
for n in root_nodes:
dicts.append(recursive_node_to_dict(n))
def tree(request):
return HttpResponse(json.dumps(dicts, indent=4), content_type="application/json")

View File

@ -18,6 +18,7 @@ urlpatterns = patterns('',
url(r'', include('regluit.payment.urls')),
url(r'', include('regluit.libraryauth.urls')),
url(r'', include('regluit.marc.urls')),
url(r'^bisac/', include('regluit.bisac.urls')),
url(r'^selectable/', include('selectable.urls')),
url(r'^admin/', include(admin_site.urls)),
url(r'^comments/', include('django.contrib.comments.urls')),