forked from OpenTreeMap/otm-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
60 lines (51 loc) · 2.88 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django.template import RequestContext
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import Context, Template, RequestContext
from django.template.loader import render_to_string
from treekey.models import Node, Species
# Create a tree heirarchy to display node relationships
def nodes(request):
return render_to_response('treekey/base.html', RequestContext(request, {'display': index_maker()}))
# Warning! Recursion here! Tread carefully!
# Recursively generates a tree of nodes in their parent-child relationships
def index_maker(request):
rootnode = Node.objects.order_by('id')[0]
def _index(node):
for child in node.child_set.all():
if child.has_children():
yield render_to_string('treekey/list_branch.html',
{'node': child,
'leaves': _index(child)}, RequestContext(request))
continue
yield render_to_string('treekey/list_branch.html',
{'node': child,
'leaves': None}, RequestContext(request))
return _index(rootnode)
# Warning! Inner-loop reassignment here! Tread carefully!
# Generates a bread-crumb trail from a node up the parent tree to the start
def trail_maker(n):
node_trail = []
while n.has_parent():
node_trail.insert(0, n)
n = n.parent_node
return node_trail
# Returns the starting node for the key. Must be the first record in the database.
def first_node(request):
f = Node.objects.order_by('id')[0]
return render_to_response('treekey/node.html', RequestContext(request, {'request': request, 'node':f, 'display':index_maker(request), 'trail':trail_maker(f)}))
# Returns a node or leaf depending on the child objects
def node(request, node_id):
n = get_object_or_404(Node, pk=node_id)
if n.has_children():
return render_to_response('treekey/node.html', RequestContext(request, {'request': request, 'node':n, 'display':index_maker(request), 'trail':trail_maker(n)}))
return render_to_response('treekey/leaf.html', RequestContext(request, {request: request, 'leaf':n, 'display':index_maker(request), 'trail':trail_maker(n)}))
# Returns species information with parent node trail/index information
def species(request, species_id):
s = get_object_or_404(Species, pk=species_id)
n = s.node_set.all()[0];
return render_to_response('treekey/species.html', RequestContext(request, {'request': request, 'species':s, 'display':index_maker(request), 'trail':trail_maker(n)}))
def browse(request):
f = Node.objects.order_by('id')[0]
species = Species.objects.all().order_by('common_name')
return render_to_response('treekey/browse.html', RequestContext(request, {'request': request, 'species':species, 'display':index_maker(request), 'trail':trail_maker(f)}))