<commit_before># Copyright (C) 2015 <NAME>
#
# This file is part of paiji2-forum
#
# paiji2-forum is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# paiji2-forum is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404
# from django.http import Http404
from django.views.generic import ListView, CreateView,\
    TemplateView
from django.utils.decorators import method_decorator
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from .models import Message, MessageIcon
from django.db.models import Count
from django.forms import ModelForm, RadioSelect,\
    ModelChoiceField, TextInput, Textarea


class TopicListView(ListView):

    template_name = 'forum/index.html'
    paginate_by = 12
    queryset = Message.objects.root_nodes().order_by('-pub_date')


class NewMessagesView(ListView):

    template_name = 'forum/recents.html'
    paginate_by = 30
    queryset = Message.objects.order_by('-pub_date')

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


class UnreadMessagesView(NewMessagesView):

    template_name = 'forum/unread.html'
    paginate_by = 30

    def get_queryset(self):
        return Message.objects\
            .exclude(
                readers__pk=self.request.user.pk
            ).order_by(
                '-pub_date'
            )


class TopicView(TemplateView):

    template_name = 'forum/topic.html'

    def get_context_data(self, **kwargs):
        context = super(TemplateView, self).get_context_data(**kwargs)
        message = get_object_or_404(
            Message,
            pk=self.kwargs['pk'],
        )
        user = self.request.user
        object_list = list(
            message.topic()
            .get_tree()
            .select_related(
                "author",
                "icon",
            ).annotate(
                readings=Count('readers'),
            ).all()
        )
        context.update({
            'message': message,
            'prev_topic': message.prev_topic,
            'next_topic': message.next_topic,
            'object_list': object_list,
        })
        read_messages = list(user.read_messages.all())
        for msg in context['object_list']:
            if msg not in read_messages:
                msg.readers.add(user)
                msg.not_read = True  # to show the label...<commit_msg>optimizes db accesses in TopicView (bis)<commit_after># Copyright (C) 2015 <NAME>
#
# This file is part of paiji2-forum
#
# paiji2-forum is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# paiji2-forum is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404
# from django.http import Http404
from django.views.generic import ListView, CreateView,\
    TemplateView
from django.utils.decorators import method_decorator
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from .models import Message, MessageIcon
from django.db.models import Count
from django.forms import ModelForm, RadioSelect,\
    ModelChoiceField, TextInput, Textarea


class TopicListView(ListView):

    template_name = 'forum/index.html'
    paginate_by = 12
    queryset = Message.objects.root_nodes().order_by('-pub_date')


class NewMessagesView(ListView):

    template_name = 'forum/recents.html'
    paginate_by = 30
    queryset = Message.objects.order_by('-pub_date')

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


class UnreadMessagesView(NewMessagesView):

    template_name = 'forum/unread.html'
    paginate_by = 30

    def get_queryset(self):
        return Message.objects\
            .exclude(
                readers__pk=self.request.user.pk
            ).order_by(
                '-pub_date'
            )


class TopicView(TemplateView):

    template_name = 'forum/topic.html'

    def get_context_data(self, **kwargs):
        context = super(TemplateView, self).get_context_data(**kwargs)
        message = get_object_or_404(
            Message,
            pk=self.kwargs['pk'],
        )
        user = self.request.user
        object_list = message.topic()\
            .get_tree()\
            .select_related(
                "author",
                "icon",
            ).annotate(
                readings=Count('readers'),
            )
        context.update({
            'message': message,
            'prev_topic': message.prev_topic,
            'next_topic': message.next_topic,
            'object_list': object_list,
        })
        read_messages = user.read_messages.filter(pk__in=object_list)
        for msg in context['object_list']:
            if msg not in read_messages:
                msg.readers.add(user)
                msg.not_read = True  # to show the label...
{'fraction_non_alphanumeric': 0.08722438391699092, 'fraction_numerical': 0.0074578469520103765, 'mean_word_length': 2.696225284601558, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4496415', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1681, 'n_words': 510}