<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.contrib.sites.models import get_current_site
from django.core.paginator import Paginator, InvalidPage
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.shortcuts import get_object_or_404
from django import template
from django.utils import timezone
from django.http import Http404

from haystack.views import SearchView

from .models import Post
from opps.channels.models import Channel
from opps.core.utils import set_context_data


class OppsList(ListView):

    context_object_name = "context"

    def get_context_data(self, **kwargs):
        return set_context_data(self, OppsList, **kwargs)

    @property
    def template_name(self):
        domain_folder = 'channels'
        if self.site.id > 1:
            domain_folder = "{0}/channels".format(self.site)

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

    @property
    def queryset(self):
        self.site = get_current_site(self.request)
        self.long_slug = None
        if not self.kwargs.get('channel__long_slug'):
            self.article = Post.objects.filter(channel__homepage=True,
                                               site=self.site,
                                               date_available__lte=timezone.now(),
                                               published=True).all()
            homepage = Channel.objects.get_homepage(site=self.site)
            if homepage:
                self.long_slug = homepage.long_slug
            return self.article
        self.long_slug = self.kwargs['channel__long_slug']
        get_object_or_404(Channel, site=self.site, long_slug=self.long_slug,
                          date_available__lte=timezone.now(), published=True)
        self.article = Post.objects.filter(site=self.site,
                                           channel__long_slug=self.long_slug,
                                           date_available__lte=timezone.now(),
                                           published=True).all()
        return self.article


class OppsDetail(DetailView):

    context_object_name = "context"

    def get_context_data(self, **kwargs):
        return set_context_data(self, OppsDetail, **kwargs)

    @property
    def template_name(self):
        domain_folder = 'articles'
        if self.site.id > 1:
            domain_folder = "{0}/articles".format(self.site)
        try:
            _template = '{0}/{1}/{2}.html'.format(
                domain_folder, self.long_slug, self.article.get().slug)
            template.loader.get_template(_template)
        except template.TemplateDoesNotExist:
            _template = '{0}/{1}.html'.format(domain_folder, self.long_slug)
        return _template

    @property
    def queryset(self):
        self.site = get_current_site(self.request)<commit_msg>refactor oppslist use get object or 404 channel on post filter object reuse<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.contrib.sites.models import get_current_site
from django.core.paginator import Paginator, InvalidPage
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.shortcuts import get_object_or_404
from django import template
from django.utils import timezone
from django.http import Http404

from haystack.views import SearchView

from .models import Post
from opps.channels.models import Channel
from opps.core.utils import set_context_data


class OppsList(ListView):

    context_object_name = "context"

    def get_context_data(self, **kwargs):
        return set_context_data(self, OppsList, **kwargs)

    @property
    def template_name(self):
        domain_folder = 'channels'
        if self.site.id > 1:
            domain_folder = "{0}/channels".format(self.site)

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

    @property
    def queryset(self):
        self.site = get_current_site(self.request)
        self.long_slug = None
        if not self.kwargs.get('channel__long_slug'):
            self.article = Post.objects.filter(channel__homepage=True,
                                               site=self.site,
                                               date_available__lte=timezone.now(),
                                               published=True).all()
            homepage = Channel.objects.get_homepage(site=self.site)
            if homepage:
                self.long_slug = homepage.long_slug
            return self.article
        self.long_slug = self.kwargs['channel__long_slug']
        self.channel = get_object_or_404(Channel, site=self.site,
                                         long_slug=self.long_slug,
                                         date_available__lte=timezone.now(),
                                         published=True)
        self.article = Post.objects.filter(site=self.site,
                                           channel=self.channel,
                                           date_available__lte=timezone.now(),
                                           published=True).all()
        return self.article


class OppsDetail(DetailView):

    context_object_name = "context"

    def get_context_data(self, **kwargs):
        return set_context_data(self, OppsDetail, **kwargs)

    @property
    def template_name(self):
        domain_folder = 'articles'
        if self.site.id > 1:
            domain_folder = "{0}/articles".format(self.site)
        try:
            _template = '{0}/{1}/{2}.html'.format(
                domain_folder, self.long_slug, self.article.get().slug)
            template.loader.get_template(_template)
        except template.TemplateDoesNotExist:
            _template = '{0}/{1}.html'.format(domain_folder, self.long_slug)
        return _template

    @property
    def queryset(self):
        self.site = get_current_site(self.request)
{'fraction_non_alphanumeric': 0.09268863978584574, 'fraction_numerical': 0.0075288606324242936, 'mean_word_length': 2.1022314478463935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '317940', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1582, 'n_words': 320}