from importlib import import_module

from django.conf import settings
from django.views.generic.base import TemplateView

from regulations.generator import api_reader


class SideBarView(TemplateView):
    """ View for handling the right-side sidebar """
    def get_template_names(self):
        return ['regulations/custom-sidebar.html', 'regulations/sidebar.html']

    def get_context_data(self, **kwargs):
        context = super(SideBarView, self).get_context_data(**kwargs)

        client = api_reader.ApiReader()

        context['sidebars'] = []
        for class_path in settings.SIDEBARS:
            module, class_name = class_path.rsplit('.', 1)
            klass = getattr(import_module(module), class_name)
            sidebar = klass(context['label_id'], context['version'])
            context['sidebars'].append(
                sidebar.full_context(client, self.request))

        return context

@@ -252,24 +252,16 @@
 %22%22%22%0A    
-def get_
 template
@@ -269,33 +269,11 @@
 name
-s(self):%0A        return %5B
+ = 
 'reg
@@ -285,15 +285,8 @@
 ons/
-custom-
 side
@@ -298,37 +298,110 @@
 tml'
-, 'regulations/sidebar.html'%5D
+%0A    # contains either class paths or class objects (not instances)%0A    components = settings.SIDEBARS
 %0A%0A  
@@ -560,35 +560,23 @@
         
-context%5B'sidebars'%5D
+klasses
  = %5B%5D%0A  
@@ -585,16 +585,25 @@
     for 
+class_or_
 class_pa
@@ -614,25 +614,87 @@
 n se
-ttings.SIDEBARS:%0A
+lf.components:%0A            if isinstance(class_or_class_path, basestring):%0A    
     
@@ -722,16 +722,25 @@
 _name = 
+class_or_
 class_pa
@@ -773,16 +773,27 @@
     
+   
+ 
 klass
- = 
+es.append(
 geta
@@ -830,21 +830,89 @@
 ss_name)
+)
 %0A    
+        else:%0A                klasses.append(class_or_class_path)%0A%0A
         
@@ -918,19 +918,21 @@
  sidebar
+s
  = 
+%5B
 klass(co
@@ -986,95 +986,167 @@
     
-context%5B'sidebars'%5D.append(%0A                sidebar.full_context(client, self.request))
+        for klass in klasses%5D%0A        context%5B'sidebars'%5D = %5Bsidebar.full_context(client, self.request)%0A                               for sidebar in sidebars%5D
 %0A%0A  

Allow sidebar view to be configured