rockviewer/three_d_viewer/views.py

87 lines
3 KiB
Python
Raw Normal View History

2013-08-25 20:16:20 +10:00
"""
Define the views for the Django MVC
"""
2013-07-27 22:23:05 +10:00
from django import template
from django.views import generic
2014-02-21 23:38:18 +10:00
from itertools import chain
from operator import attrgetter
2013-07-27 22:23:05 +10:00
2014-01-21 21:43:23 +10:00
from three_d_viewer.models import Sample, Category, Mineral
2013-07-27 22:23:05 +10:00
register = template.Library()
class HomeView(generic.ListView):
2013-08-25 20:16:20 +10:00
"""
Show the home page
2013-08-25 20:16:20 +10:00
"""
2013-07-27 22:23:05 +10:00
template_name = 'three_d_viewer/home.html'
model = Sample
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
2014-01-21 21:43:23 +10:00
context['active_samples'] = Sample.objects.select_subclasses(Mineral).filter(active=True).order_by('name')
context['parent_categories'] = Category.objects.filter(parent=None). \
2014-01-21 15:59:23 +10:00
filter(active=True).order_by('name')
return context
2013-07-27 22:23:05 +10:00
2014-02-21 23:38:18 +10:00
class MineralPracticeView(generic.ListView):
model = Sample
template_name = 'three_d_viewer/minerals_practice.html'
def get_context_data(self, **kwargs):
context = super(MineralPracticeView, self).get_context_data(**kwargs)
cat = Category.objects.get(name='Minerals')
result = cat.active_samples
2013-07-27 22:23:05 +10:00
2014-02-21 23:38:18 +10:00
for child in cat.active_children:
result = chain(result, child.active_samples)
context['active_samples'] = sorted(result, key=attrgetter('name'))
return context
2014-02-20 15:21:50 +10:00
2013-07-27 22:23:05 +10:00
class DetailView(generic.DetailView):
2013-08-25 20:16:20 +10:00
"""
Define the view to view the 3D model of a sample
"""
2013-07-27 22:23:05 +10:00
model = Sample
template_name = 'three_d_viewer/detail.html'
parent_categories = Category.objects.filter(parent=None). \
2014-01-21 15:59:23 +10:00
filter(active=True).order_by("name")
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
2014-01-21 21:43:23 +10:00
context['active_samples'] = Sample.objects.select_subclasses(Mineral).filter(active=True).order_by('name')
context['parent_categories'] = Category.objects.filter(parent=None). \
2014-01-21 15:59:23 +10:00
filter(active=True).order_by('name')
return context
2014-01-21 21:43:23 +10:00
2014-02-22 13:38:18 +10:00
class MineralDetailView(generic.DetailView):
2014-01-21 21:43:23 +10:00
"""
Add extra functionality for mineral details
"""
model = Mineral
template_name = 'three_d_viewer/mineral_detail.html'
parent_categories = Category.objects.filter(parent=None). \
filter(active=True).order_by("name")
2014-02-22 13:38:18 +10:00
#def get_context_data(self, **kwargs):
# context = super(MineralDetailView, self).get_context_data(**kwargs)
# context['active_samples'] = Sample.objects.select_subclasses(Mineral).filter(active=True).order_by('name')
# context['parent_categories'] = Category.objects.filter(parent=None). \
# filter(active=True).order_by('name')
# return context
def get_context_data(self, **kwargs):
2014-02-22 13:38:18 +10:00
context = super(MineralDetailView, self).get_context_data(**kwargs)
cat = Category.objects.get(name='Minerals')
result = cat.active_samples
for child in cat.active_children:
result = chain(result, child.active_samples)
context['active_samples'] = sorted(result, key=attrgetter('name'))
return context