<commit_before># -*- coding: utf-8 -*-
"""Taxonomy views."""
from __future__ import absolute_import, unicode_literals

from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
# from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from taxonomy.filters import CommunityFilter, TaxonFilter
from taxonomy.models import Community, Taxon
from taxonomy.tables import CommunityAreaEncounterTable, TaxonAreaEncounterTable
from taxonomy.utils import update_taxon as update_taxon_util


@csrf_exempt
def update_taxon(request):
    """Update Taxon."""
    msg = update_taxon_util()
    messages.success(request, msg)
    return HttpResponseRedirect("/")


# ---------------------------------------------------------------------------#
# List Views
#
class TaxonListView(ListView):
    """A ListView for Taxon."""

    model = Taxon
    template_name = "taxonomy/taxon_list.html"
    paginate_by = 12

    def get_context_data(self, **kwargs):
        """Add extra items to context."""
        context = super(TaxonListView, self).get_context_data(**kwargs)
        context["now"] = timezone.now()
        context["taxon_filter"] = TaxonFilter(self.request.GET, queryset=self.get_queryset())
        return context

    def get_queryset(self):
        """Queryset: filter by name_id if in pars.

        There are two mutually exclusive ways of filtering data:

        * Taxon card > explore: GET name_id = show this taxon, its parents
          and all children. Do not process the other filter fields.
        * Search filter: name (icontains), rank, is current, publication status.

        DO NOT use taxon_filter.qs in template:
        https://github.com/django-mptt/django-mptt/issues/632
        """
        queryset = Taxon.objects.order_by(
            "-rank", "-current"
        ).prefetch_related(
            "taxon_gazettal", "conservationaction_set",  "document_set"
        )

        # name_id is mutually exclusive to other parameters
        if self.request.GET.get("name_id"):
            # t = queryset.filter(name_id=self.request.GET.get("name_id"))
            # return list(chain(t.first().get_ancestors(), t, t.first().get_children()))
            try:
                return queryset.filter(
                    name_id=self.request.GET.get("name_id")
                ).get().get_family()
            except ObjectDoesNotExist:
                messages.warning(self.request, "This Name ID does not exist.")
                return queryset

        return TaxonFilter(self.request.GET, queryset=queryset).qs


class CommunityListView(ListView):
    """A ListView for Community."""

    model = Community
    paginate_by = 12

    def get_context_data(self, **kwargs):
        """Add extra items to context."""
        context = super(CommunityListView, self).get_context_data(**kwargs)
        context["now"] = timezone.now()
        context["community_filter"] = CommunityFilter(
            self.request.GET,
            queryset=self.get_queryset()
        )
        return context

    def get_queryset(self):
        """Queryset."""
        queryset = Community.objects.all().prefetch_related(
            "community_gazettal",
            "conservationaction_set",
            "document_set"
        )
        return CommunityFilter(self.request.GET, queryset=queryset).qs


# ---------------------------------------------------------------------------#
# Detail Views
#
class TaxonDetailView(DetailView):
    """DetailView for Taxon."""

    model = Taxon
    context_object_name = "original"

    def get_object(self):
        """Get Object by name_id."""
        t = Taxon.objects.filter(
            name_id=self.kwargs.get("name_id")
        ).prefetch_related(
            "taxon_occurrences",
            "conservationaction_set"
        ).first()
        if not t:
            raise Http404
        return t

    def get_context_data(self, **kwargs):
        """Custom context."""
        context = super(TaxonDetailView, self).get_context_data(**kwargs)
        obj = self.object
        occ = obj.taxon_occurrences
        ma = obj.conservationaction_set.all()
        max_cards = 100
        context["occurrence_table"] = TaxonAreaEncounterTable(occ.all()[:max_cards])
        context["occurrences"] = occ.all()
        context["max_cards"] = max_cards
        if occ:
            context["occurrence_total"] = occ.count()
        else:
            context["occurrence_total"] = 0
        context["conservationactions_general"] = ma.filter(document=None, occurrence_area_code=None)
        context["conservationactions_area"] = ma.exclude(occurrence_area_code=None)
        return context


class CommunityDetailView(DetailView):
    """DetailView for Community."""

    model = Community
    context_object_name = "original"

    def get_object(self):
        """Get Object by pk."""
        return get_object_or_404(
            Community.objects.filter(
                pk=self.kwargs.get("pk")
            ).prefetch_related(
                "community_occurrences",
                "conservationaction_set"
            ).first()
        )

    def get_context_data(self, **kwargs):
        """Custom context."""
        context = super(CommunityDetailView, self).get_context_data(**kwargs)
        obj = self.get_object()
        occ = obj.community_occurrences
        ma = obj.conservationaction_set.all()
        max_cards = 100
        context["max_cards"] = max_cards
        if occ:
            context["occurrence_total"] = occ.count()
        else:
            context["occurrence_total"] = 0
        context["occurrence_table"] = CommunityAreaEncounterTable(occ.all()[:max_cards])
        context["occurrences"] = occ.all()
        context["conservationactions_general"] = ma.filter(document=None, occurrence_area_code=None)
        context["conservationactions_area"] = ma.exclude(occurrence_area_code=None)
        return context
<commit_msg>fix CommunityDetailView.get_object<commit_after># -*- coding: utf-8 -*-
"""Taxonomy views."""
from __future__ import absolute_import, unicode_literals

from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect, Http404
# from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
# from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from taxonomy.filters import CommunityFilter, TaxonFilter
from taxonomy.models import Community, Taxon
from taxonomy.tables import CommunityAreaEncounterTable, TaxonAreaEncounterTable
from taxonomy.utils import update_taxon as update_taxon_util


@csrf_exempt
def update_taxon(request):
    """Update Taxon."""
    msg = update_taxon_util()
    messages.success(request, msg)
    return HttpResponseRedirect("/")


# ---------------------------------------------------------------------------#
# List Views
#
class TaxonListView(ListView):
    """A ListView for Taxon."""

    model = Taxon
    template_name = "taxonomy/taxon_list.html"
    paginate_by = 12

    def get_context_data(self, **kwargs):
        """Add extra items to context."""
        context = super(TaxonListView, self).get_context_data(**kwargs)
        context["now"] = timezone.now()
        context["taxon_filter"] = TaxonFilter(self.request.GET, queryset=self.get_queryset())
        return context

    def get_queryset(self):
        """Queryset: filter by name_id if in pars.

        There are two mutually exclusive ways of filtering data:

        * Taxon card > explore: GET name_id = show this taxon, its parents
          and all children. Do not process the other filter fields.
        * Search filter: name (icontains), rank, is current, publication status.

        DO NOT use taxon_filter.qs in template:
        https://github.com/django-mptt/django-mptt/issues/632
        """
        queryset = Taxon.objects.order_by(
            "-rank", "-current"
        ).prefetch_related(
            "taxon_gazettal", "conservationaction_set",  "document_set"
        )

        # name_id is mutually exclusive to other parameters
        if self.request.GET.get("name_id"):
            # t = queryset.filter(name_id=self.request.GET.get("name_id"))
            # return list(chain(t.first().get_ancestors(), t, t.first().get_children()))
            try:
                return queryset.filter(
                    name_id=self.request.GET.get("name_id")
                ).get().get_family()
            except ObjectDoesNotExist:
                messages.warning(self.request, "This Name ID does not exist.")
                return queryset

        return TaxonFilter(self.request.GET, queryset=queryset).qs


class CommunityListView(ListView):
    """A ListView for Community."""

    model = Community
    paginate_by = 12

    def get_context_data(self, **kwargs):
        """Add extra items to context."""
        context = super(CommunityListView, self).get_context_data(**kwargs)
        context["now"] = timezone.now()
        context["community_filter"] = CommunityFilter(
            self.request.GET,
            queryset=self.get_queryset()
        )
        return context

    def get_queryset(self):
        """Queryset."""
        queryset = Community.objects.all().prefetch_related(
            "community_gazettal",
            "conservationaction_set",
            "document_set"
        )
        return CommunityFilter(self.request.GET, queryset=queryset).qs


# ---------------------------------------------------------------------------#
# Detail Views
#
class TaxonDetailView(DetailView):
    """DetailView for Taxon."""

    model = Taxon
    context_object_name = "original"

    def get_object(self):
        """Get Object by name_id."""
        t = Taxon.objects.filter(
            name_id=self.kwargs.get("name_id")
        ).prefetch_related(
            "taxon_occurrences",
            "conservationaction_set"
        ).first()
        if not t:
            raise Http404
        return t

    def get_context_data(self, **kwargs):
        """Custom context."""
        context = super(TaxonDetailView, self).get_context_data(**kwargs)
        obj = self.object
        occ = obj.taxon_occurrences
        ma = obj.conservationaction_set.all()
        max_cards = 100
        context["occurrence_table"] = TaxonAreaEncounterTable(occ.all()[:max_cards])
        context["occurrences"] = occ.all()
        context["max_cards"] = max_cards
        if occ:
            context["occurrence_total"] = occ.count()
        else:
            context["occurrence_total"] = 0
        context["conservationactions_general"] = ma.filter(document=None, occurrence_area_code=None)
        context["conservationactions_area"] = ma.exclude(occurrence_area_code=None)
        return context


class CommunityDetailView(DetailView):
    """DetailView for Community."""

    model = Community
    context_object_name = "original"

    def get_object(self):
        """Get Object by pk."""
        com = Community.objects.filter(
            pk=self.kwargs.get("pk")
        ).prefetch_related(
            "community_occurrences",
            "conservationaction_set"
        ).first()
        if not com:
            raise Http404
        return com

    def get_context_data(self, **kwargs):
        """Custom context."""
        context = super(CommunityDetailView, self).get_context_data(**kwargs)
        obj = self.get_object()
        occ = obj.community_occurrences
        ma = obj.conservationaction_set.all()
        max_cards = 100
        context["max_cards"] = max_cards
        if occ:
            context["occurrence_total"] = occ.count()
        else:
            context["occurrence_total"] = 0
        context["occurrence_table"] = CommunityAreaEncounterTable(occ.all()[:max_cards])
        context["occurrences"] = occ.all()
        context["conservationactions_general"] = ma.filter(document=None, occurrence_area_code=None)
        context["conservationactions_area"] = ma.exclude(occurrence_area_code=None)
        return context

{'fraction_non_alphanumeric': 0.12862202189311012, 'fraction_numerical': 0.004507405022537025, 'mean_word_length': 2.4960607765897582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1257342', 'n_tokens_mistral': 3563, 'n_tokens_neox': 3349, 'n_words': 755}