# -*- encoding: utf-8 -*-
import copy
from collections import OrderedDict

from django.db import models
from django.template.base import Template
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _

from .behaviors import Orderable, Titled
from .validators import TemplateValidator


class ReportElement(Titled, Orderable):
    parent = models.ForeignKey('flexible_reports.Report')
    datasource = models.ForeignKey('flexible_reports.Datasource')
    table = models.ForeignKey('flexible_reports.Table')
    slug = models.CharField(max_length=200)

    class Meta:
        unique_together = [
            ('parent', 'position'),
            ('parent', 'slug')
        ]
        ordering = ('position',)


def _get_template(name):
    t = get_template(name)
    return t.template.source


def get_reports_template():
    return _get_template("flexible_reports/report.html")


class Report(Titled):
    """Report is a collection of multiple Datasources' data
    rendered in Tables. """

    slug = models.SlugField()

    template = models.TextField(
        default=get_reports_template,
        validators=[TemplateValidator, ]
    )

    class Meta:
        verbose_name_plural = _("Reports")
        verbose_name = _("Report")

    def set_base_queryset(self, queryset):
        self.base_queryset = queryset

    def as_html(self, parent_context=None):
        render_context = copy.copy(parent_context)
        render_context.update({
            'self': self,
            'tables': OrderedDict()
        })

        for elem in self.reportelement_set.all().select_related():
            datasource = elem.datasource
            object_list = self.base_queryset.filter(
                datasource.get_filter()
            )

            table_dict = {
                'title': elem.title,
                'subtitle': elem.subtitle,
                'object_list': object_list,
                'as_django_tables2': elem.table.as_django_tables2(object_list)
            }

            render_context['tables'][elem.slug] = table_dict

        return Template(self.template).render(render_context)

@@ -23,127 +23,92 @@
 -*-%0A
-import copy%0Afrom collections import OrderedDict%0A%0Afrom django.db import models%0Afrom django.template.base import Template
+from django.core.exceptions import ImproperlyConfigured%0Afrom django.db import models
 %0Afro
@@ -536,12 +536,12 @@
 els.
-Char
+Slug
 Fiel
@@ -1060,16 +1060,52 @@
 tField(%0A
+        verbose_name=_(%22Template%22),%0A
         
@@ -1190,98 +1190,43 @@
     
-class Meta:%0A        verbose_name_plural = _(%22Reports%22)%0A        verbose_name = _(%22Report%22)%0A
+_base_queryset = None%0A    @property
 %0A   
@@ -1234,12 +1234,8 @@
 def 
-set_
 base
@@ -1252,18 +1252,8 @@
 self
-, queryset
 ):%0A 
@@ -1259,21 +1259,25 @@
         
+if 
 self.
+_
 base_que
@@ -1286,62 +1286,15 @@
 set 
-= queryset%0A%0A    def as_html(self, parent_context=
+is 
 None
-)
 :%0A  
@@ -1303,153 +1303,107 @@
     
-render_context = copy.copy(parent_context)%0A        render_context.update(%7B%0A            'self': self,%0A            'tables': OrderedDict()%0A
+    raise ImproperlyConfigured(%22Please set base queryset for this %22%0A                       
         
 %7D)%0A%0A
@@ -1402,20 +1402,16 @@
     
-%7D)%0A%0A
         
 for 
@@ -1410,139 +1410,70 @@
     
-for elem in self.reportelement_set.all().select_related():%0A            datasource = elem.datasource%0A            object_list =
+%22report using Report.set_base_queryset%22)%0A        return
  self.
+_
 base
@@ -1485,437 +1485,183 @@
 yset
-.filter(%0A                datasource.get_filter()%0A            )%0A%0A            table_dict = %7B%0A                'title': elem.title,%0A                'subtitle': elem.subtitle,%0A                'object_list': object_list,%0A                'as_django_tables2': elem.table.as_django_tables2(object_list)%0A            %7D%0A%0A            render_context%5B'tables'%5D%5Belem.slug%5D = table_dict%0A%0A        return Template(self.template).render(render_context)
+%0A%0A    class Meta:%0A        verbose_name_plural = _(%22Reports%22)%0A        verbose_name = _(%22Report%22)%0A%0A    def set_base_queryset(self, queryset):%0A        self._base_queryset = queryset
 %0A

Move as_html to adapters.django_tables2