Implemented tree view for selection of samples

This commit is contained in:
2013-09-11 23:44:40 +10:00
parent 7f099bfca4
commit 6d40700cf3
11 changed files with 99 additions and 45 deletions

Binary file not shown.

View File

@@ -42,7 +42,8 @@ class Sample(CommonInfo):
description = models.CharField(max_length=2000, default='', blank=True,
null=True)
parent = models.ForeignKey(Category, blank=True, null=True,
on_delete=models.SET_NULL)
on_delete=models.SET_NULL,
related_name="samples")
class Question(models.Model):

View File

@@ -1,7 +0,0 @@
<ul>
{% for child in children %}
<li> <a href="{{ child.get_absolute_url }}">{{ child }}</a></li>
{% child.children.count > 0 %}
{% children_list child %}
{% endfor %}
</ul>

View File

@@ -1 +0,0 @@
{% children_tag parent %}

View File

@@ -366,8 +366,7 @@ function my_callback(data){
}
alert(message);
}
</script>
</script
</head>
@@ -380,16 +379,23 @@ function my_callback(data){
<div class="row">
<div class="col-md-4">
{% if sample.questions %}
{{ sample.questions.all.0 }}<br>
{% for answer in sample.questions.all.0.answers.all %}
<input type="radio" name="answer" value={{answer.id}}>{{ answer}}</input><br>
{% endfor %}
<input type="button" onclick="Dajaxice.three_d_viewer.check_answer(my_callback, {'answerid':$('input:radio[name=answer]:checked').val(), 'questionid':{{ sample.questions.all.0.id }}})" value="Get message from server!" />
{% endif %}
<div class="row">
{% include "three_d_viewer/sampletree.html" %}
</div>
<div class="row">
<div class="well">
{% if sample.questions %}
{{ sample.questions.all.0 }}<br>
{% for answer in sample.questions.all.0.answers.all %}
<input type="radio" name="answer" value={{answer.id}}>{{ answer}}</input><br>
{% endfor %}
<input type="button" onclick="Dajaxice.three_d_viewer.check_answer(my_callback, {'answerid':$('input:radio[name=answer]:checked').val(), 'questionid':{{ sample.questions.all.0.id }}})" value="Get message from server!" />
{% endif %}
</div>
</div>
</div>
<div class="col-md-8">
<canvas id="canvas" <!--style="border: 1px dotted #bfbfbf;" width=933 height="700"--></canvas>
<canvas id="canvas"></canvas>
<div><br><span id="progress" style="color: red"></span></div>
</div>
</div>

View File

@@ -0,0 +1,31 @@
{% load static %}
{% load dajaxice_templatetags %}
<html>
<head>
<title>3D Sample Viewer</title>
<link href="{% static "three_d_viewer/css/bootstrap.min.css" %}" rel="stylesheet" media="screen">
<script type="text/javascript" src="{% static "three_d_viewer/jquery-2.0.3.js" %}"></script>
<script type="text/javascript" srv="{% static "three_d_viewer/js/bootstrap.min.js" %}"></script>
</head>
<body onload="load();">
<div class="container">
<div class="page-header">
<h1>Three D Viewer</h1>
<p class="lead">Look at some rocks</p>
</div>
<div class="row">
<div class="col-md-4">
<div class="row">
{% include "three_d_viewer/sampletree.html" %}
</div>
</div>
<div class="col-md-8">
<p>Put some intro text sort of stuff here</p?
</div>
</div>
</div
</body>
</html>

View File

@@ -1,9 +0,0 @@
{% if active_samples %}
<ul>
{% for sample in active_samples %}
<li><a href="{% url 'three_d_viewer:detail' sample.id %}">{{ sample.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No samples are available.</p>
{% endif %}

View File

@@ -0,0 +1,26 @@
<script type='text/javascript'>
$(window).load(function(){
$('.tree-toggle').click(function () {
$(this).parent().children('ul.tree').toggle(200);
});
});
</script>
<div class="well">
<ul class="nav nav-list">
<h2>Samples</h2>
<ul>
<ul class="nav nav-list tree">
{% for category in parent_categories %}
<li><label class="tree-toggle nav-header">{{ category.name }}</label>
<ul class="nav nav-list tree">
{% for sample in category.samples.all %}
<li><a href="{% url 'three_d_viewer:detail' sample.id %}">{{ sample.name }}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</ul>
</ul>
</div>

View File

@@ -7,6 +7,7 @@ Replace this with more appropriate tests for your application.
from django.test import TestCase
from three_d_viewer.models import Category, Question, Answer
from three_d_viewer.views import DetailView
import ajax

View File

@@ -4,7 +4,7 @@ from three_d_viewer import views
urlpatterns = patterns(
'',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^$', views.HomeView.as_view(), name='home'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),

View File

@@ -5,33 +5,39 @@ Define the views for the Django MVC
from django import template
from django.views import generic
from three_d_viewer.models import Sample
from three_d_viewer.models import Sample, Category
register = template.Library()
@register.inclusion_tag('children.html')
def children_tag(category):
children = category.children.all()
return {'children': children}
class IndexView(generic.ListView):
class HomeView(generic.ListView):
"""
Define the page to display the Sample objects that can be viewed
Show the home page
"""
template_name = 'three_d_viewer/index.html'
context_object_name = 'active_samples'
def get_queryset(self):
"""
Return the active samples
"""
return Sample.objects.filter(active=True)
template_name = 'three_d_viewer/home.html'
model = Sample
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context['active_samples'] = Sample.objects.filter(active=True)
context['parent_categories'] = Category.objects.filter(parent=None). \
filter(active=True)
return context
class DetailView(generic.DetailView):
"""
Define the view to view the 3D model of a sample
"""
model = Sample
template_name = 'three_d_viewer/detail.html'
parent_categories = Category.objects.filter(parent=None). \
filter(active=True)
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context['active_samples'] = Sample.objects.filter(active=True)
context['parent_categories'] = Category.objects.filter(parent=None). \
filter(active=True)
return context