<commit_before>import json
from django.views.generic.edit import FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.contrib.sites.models import Site
from rest_framework import viewsets
from .models import Moderator
from rest_framework.permissions import IsAuthenticated, AllowAny

from django.views.generic import TemplateView
from django.utils.translation import gettext as _
from django.contrib.postgres.fields.jsonb import KeyTextTransform
from django.db.models import F
from django.db.utils import DatabaseError
from django.conf import settings
from community.helpers import (
    get_community,
    get_community_bot_screen_name,
)
from moderation.thumbnail import get_thumbnail_url
from django.contrib.sites.shortcuts import get_current_site
import logging
from moderation.forms import SelfModerationForm
from social_django.models import UserSocialAuth
from moderation.models import UserCategoryRelationship
from django.core.mail import send_mail
from bot.tasks import handle_create_friendship
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator

logger = logging.getLogger(__name__)



@method_decorator(cache_page(60 * 60), name='dispatch')
class ModeratorPublicList(TemplateView):
    title = _("Moderators")
    template_name = "moderation/moderators.html"

    def get_context_data(self, *args, **kwargs):
        # Yield successive n-sized 
        # chunks from l. 
        def divide_chunks(l, n): 
            # looping till length l 
            for i in range(0, len(l), n):  
                yield l[i:i + n] 
        
        context = super(ModeratorPublicList, self).get_context_data(*args, **kwargs)
        community = get_community(self.request)
        moderators_qs = (
            Moderator.objects
            .filter(
                public=True,
                active=True,
                community=community
            )
            .annotate(
                screen_name=KeyTextTransform('screen_name', 'socialuser__profile__json'),
                name=KeyTextTransform('name', 'socialuser__profile__json'),
                user_id=KeyTextTransform('id', 'socialuser__profile__json'),
                avatar=F('socialuser__profile__biggeravatar')
            )
            .values(
                'screen_name',
                'name',
                #'socialuser__profile__normalavatar'
                'avatar'
            )
        )
        media_url = settings.MEDIA_URL
        moderators = []
        for moderator in moderators_qs:
            moderator["avatar"] = media_url + moderator["avatar"]
            moderators.append(moderator)
            
        n = 3
        moderators = list(divide_chunks(moderators, n))    
        context["moderators"] = moderators
        context["thumbnail"] = f"{self.request.scheme}://{self.request.get_host()}{get_thumbnail_url(community)}"
        logger.debug(context)
        return context


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def moderator_id_view(request):
        try:
            socialuser = request.user.socialuser
        except DatabaseError:
            return Response({"moderator_id": None})

        moderator = Moderator.objects.filter(
            socialuser=socialuser,
            community=get_current_site(request).community
            ).first()
        return Response({"id": moderator.id})


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def follower_view(request):
        try:
            socialuser = request.user.socialuser
        except DatabaseError:
            return Response({"is_following": False})
        if True:
            return Response({"is_following": True})
<commit_msg>to avoid downstream errors, return context if Site is not linked to any Community<commit_after>import json
from django.views.generic.edit import FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.contrib.sites.models import Site
from rest_framework import viewsets
from .models import Moderator
from rest_framework.permissions import IsAuthenticated, AllowAny

from django.views.generic import TemplateView
from django.utils.translation import gettext as _
from django.contrib.postgres.fields.jsonb import KeyTextTransform
from django.db.models import F
from django.db.utils import DatabaseError
from django.conf import settings
from community.helpers import (
    get_community,
    get_community_bot_screen_name,
)
from moderation.thumbnail import get_thumbnail_url
from django.contrib.sites.shortcuts import get_current_site
import logging
from moderation.forms import SelfModerationForm
from social_django.models import UserSocialAuth
from moderation.models import UserCategoryRelationship
from django.core.mail import send_mail
from bot.tasks import handle_create_friendship
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator

logger = logging.getLogger(__name__)



@method_decorator(cache_page(60 * 60), name='dispatch')
class ModeratorPublicList(TemplateView):
    title = _("Moderators")
    template_name = "moderation/moderators.html"

    def get_context_data(self, *args, **kwargs):
        # Yield successive n-sized 
        # chunks from l. 
        def divide_chunks(l, n): 
            # looping till length l 
            for i in range(0, len(l), n):  
                yield l[i:i + n] 

        context = super(ModeratorPublicList, self).get_context_data(*args, **kwargs)
        community = get_community(self.request)
        if not community:
            return context
        moderators_qs = (
            Moderator.objects
            .filter(
                public=True,
                active=True,
                community=community
            )
            .annotate(
                screen_name=KeyTextTransform('screen_name', 'socialuser__profile__json'),
                name=KeyTextTransform('name', 'socialuser__profile__json'),
                user_id=KeyTextTransform('id', 'socialuser__profile__json'),
                avatar=F('socialuser__profile__biggeravatar')
            )
            .values(
                'screen_name',
                'name',
                #'socialuser__profile__normalavatar'
                'avatar'
            )
        )
        media_url = settings.MEDIA_URL
        moderators = []
        for moderator in moderators_qs:
            moderator["avatar"] = media_url + moderator["avatar"]
            moderators.append(moderator)
            
        n = 3
        moderators = list(divide_chunks(moderators, n))    
        context["moderators"] = moderators
        context["thumbnail"] = f"{self.request.scheme}://{self.request.get_host()}{get_thumbnail_url(community)}"
        return context


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def moderator_id_view(request):
        try:
            socialuser = request.user.socialuser
        except DatabaseError:
            return Response({"moderator_id": None})

        moderator = Moderator.objects.filter(
            socialuser=socialuser,
            community=get_current_site(request).community
            ).first()
        return Response({"id": moderator.id})


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def follower_view(request):
        try:
            socialuser = request.user.socialuser
        except DatabaseError:
            return Response({"is_following": False})
        if True:
            return Response({"is_following": True})

{'fraction_non_alphanumeric': 0.08248073765315145, 'fraction_numerical': 0.0015157256536566881, 'mean_word_length': 2.890909090909091, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1843029', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2080, 'n_words': 487}