#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.contrib.sites.models import get_current_site
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django import template
from django.conf import settings

from opps.articles.models import ArticleBox, Article
from opps.channels.models import Channel


class OppsView(object):

    context_object_name = "context"
    paginate_by = settings.OPPS_PAGINATE_BY
    limit = settings.OPPS_VIEWS_LIMIT

    def __init__(self):
        self.slug = None
        self.channel = None
        self.long_slug = None
        self.channel_long_slug = []
        self.article = self.model.objects.select_related('publisher')

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

        article = Article.objects.filter(
            site=self.site,
            channel_long_slug__in=self.channel_long_slug,
            date_available__lte=timezone.now(),
            published=True)
        context['posts'] = article.filter(child_class='Post')[:self.limit]
        context['albums'] = article.filter(child_class='Album')[:self.limit]

        context['channel'] = {}
        context['channel']['long_slug'] = self.long_slug
        if self.channel:
            context['channel']['slug'] = self.channel.slug
            context['channel']['level'] = self.channel.get_level()
            context['channel']['root'] = self.channel.get_root()

        context['articleboxes'] = ArticleBox.objects.filter(
            channel__long_slug=self.long_slug)
        if self.slug:
            context['articleboxes'] = context['articleboxes'].filter(
                article__slug=self.slug)

        return context

    def get_template_folder(self):
        domain_folder = self.type
        if self.site.id > 1:
            domain_folder = "{0}/{1}".format(self.site, self.type)
        return domain_folder

    def get_long_slug(self):
        self.long_slug = self.kwargs.get('channel__long_slug', None)
        try:
            if not self.long_slug:
                self.long_slug = Channel.objects.get_homepage(
                    site=self.site).long_slug
        except AttributeError:
            pass
        return self.long_slug

    def set_channel_rules(self):
        self.channel = get_object_or_404(Channel, site=self.site,
                                         long_slug=self.long_slug,
                                         date_available__lte=timezone.now(),
                                         published=True)

        self.channel_long_slug = [self.long_slug]
        for children in self.channel.get_children():
            self.channel_long_slug.append(children)

    def check_template(self, _template):
        try:
            template.loader.get_template(_template)
            return True
        except template.TemplateDoesNotExist:
            return False


class OppsList(OppsView, ListView):

    @property
    def template_name(self):

        domain_folder = self.get_template_folder()

        if self.channel:
            if self.channel.group and self.channel.parent:

                _template = '{}/_{}.html'.format(
                    domain_folder, self.channel.parent.long_slug)
                if self.check_template(_template):
                    return _template

                _template = '{}/_post_list.html'.format(
                    domain_folder, self.channel.parent.long_slug)
                if self.check_template(_template):
                    return _template

        return '{0}/{1}.html'.format(domain_folder, self.long_slug)

    @property
    def queryset(self):
        self.site = get_current_site(self.request)
        self.long_slug = self.get_long_slug()

        if not self.long_slug:
            return None

        self.set_channel_rules()

        self.article = self.model.objects.filter(
            site=self.site,
            channel_long_slug__in=self.channel_long_slug,
            date_available__lte=timezone.now(),
            published=True).select_related('publisher')[:self.limit]

        return self.article


class OppsDetail(OppsView, DetailView):

    @property
    def template_name(self):
        domain_folder = self.get_template_folder()

        _template = '{0}/{1}/{2}.html'.format(
            domain_folder, self.long_slug, self.slug)
        if not self.check_template(_template):
            _template = '{0}/{1}.html'.format(domain_folder, self.long_slug)
        return _template

    @property
    def queryset(self):
        self.site = get_current_site(self.request)
        self.slug = self.kwargs.get('slug')
        self.long_slug = self.get_long_slug()
        if not self.long_slug:
            return None

        self.set_channel_rules()

        self.article = self.model.objects.filter(
            site=self.site,
            channel_long_slug=self.long_slug,
            slug=self.slug,
            date_available__lte=timezone.now(),
            published=True).select_related('publisher')
        return self.article

@@ -389,16 +389,23 @@
  Article
+, Album
 %0Afrom op
@@ -920,59 +920,47 @@
     
-article = Article.objects.filter(%0A            site=
+filters = %7B%7D%0A        filters%5B'site'%5D = 
 self
@@ -956,33 +956,32 @@
 te'%5D = self.site
-,
 %0A            cha
@@ -965,36 +965,41 @@
 lf.site%0A        
-    
+filters%5B'
 channel_long_slu
@@ -995,33 +995,37 @@
 el_long_slug__in
-=
+'%5D = 
 self.channel_lon
@@ -1022,33 +1022,32 @@
 hannel_long_slug
-,
 %0A            dat
@@ -1031,36 +1031,41 @@
 ng_slug%0A        
-    
+filters%5B'
 date_available__
@@ -1059,33 +1059,37 @@
 e_available__lte
-=
+'%5D = 
 timezone.now(),%0A
@@ -1078,33 +1078,32 @@
 = timezone.now()
-,
 %0A            pub
@@ -1095,34 +1095,94 @@
         
-    published=True
+filters%5B'published'%5D = True%0A        article = Article.objects.filter(**filters
 )%0A      
@@ -1282,42 +1282,38 @@
 %5D = 
-article.filter(child_class='Album'
+Album.objects.filter(**filters
 )%5B:s

Fix bug get album on view, get obj album on view