<commit_before>from rest_framework import generics

from django.contrib.auth.models import Permission, Group

from common.utilities import CustomRetrieveUpdateDestroyView

from .models import MflUser, MFLOAuthApplication, ProxyGroup

from .serializers import (
    MflUserSerializer,
    MFLOAuthApplicationSerializer,
    PermissionSerializer,
    GroupSerializer
)

from .filters import MFLUserFilter, PermissionFilter, GroupFilter


class PermissionsListView(generics.ListAPIView):
    """
    This is a read-only list view; intentionally.

    The system's role based access control is built on the foundations put in
    place in `django.contrib.auth` and `django.contrib.admin`. The official
    web front-ends rely on the "automatic" permissions.
    """
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer
    filter_class = PermissionFilter
    ordering_fields = ('name', )


class GroupListView(generics.ListCreateAPIView):
    queryset = ProxyGroup.objects.all()
    serializer_class = GroupSerializer
    filter_class = GroupFilter
    ordering_fields = ('name', )


class GroupDetailView(CustomRetrieveUpdateDestroyView):
    queryset = ProxyGroup.objects.all()
    serializer_class = GroupSerializer


class UserList(generics.ListCreateAPIView):
    queryset = MflUser.objects.all()
    serializer_class = MflUserSerializer
    filter_class = MFLUserFilter
    ordering_fields = ('first_name', 'last_name', 'email', 'username',)

    def get_queryset(self, *args, **kwargs):
        from common.models import UserCounty, UserConstituency
        user = self.request.user
        if user.county and not user.is_national:
            county_users = [
                const_user.user.id for const_user in
                UserCounty.objects.filter(
                    county=user.county).distinct()
            ]
            sub_county_users = [
                const_user.user.id for const_user in
                UserConstituency.objects.filter(
                    constituency__county=user.county).distinct()
            ]
            area_users = county_users + sub_county_users
            return MflUser.objects.filter(
                id__in=area_users).exclude(id=self.request.user.id)
        elif user.is_national and not user.is_superuser:
            # Should see the county users and the national users
            # Also should not see the system user
            county_users = [
                county_user.user.id for county_user in
                UserCounty.objects.all().distinct()
            ]
            national_users = [
                nat_user.id for nat_user in MflUser.objects.filter(
                    is_national=True)
            ]
            all_users = county_users + national_users
            return MflUser.objects.filter(
                id__in=all_users).exclude(id=self.request.user.id)
        elif user.constituency:
            all_users = MflUser.objects.all()
            users_to_see = []
            group = Group.objects.get(name='Facility Viewing Group')
            for user in all_users:
                if group in user.groups.all():
                    users_to_see.append(user.id)
                if len(user.groups.all()) == 0:
                    users_to_see.append(user.id)

            return MflUser.objects.filter(
                id__in=users_to_see, created_by_id=self.request.user.id
            ).exclude(id=self.request.user.id)
        elif user.is_superuser:
            return MflUser.objects.all().exclude(id=self.request.user.id)

        else:
            # The user is not allowed to see the users
            return MflUser.objects.none()


class UserDetailView(CustomRetrieveUpdateDestroyView):
    queryset = MflUser.objects.all()
    serializer_class = MflUserSerializer
<commit_msg>Minor clean up<commit_after>from rest_framework import generics

from django.contrib.auth.models import Permission, Group

from common.utilities import CustomRetrieveUpdateDestroyView

from .models import MflUser, MFLOAuthApplication, ProxyGroup

from .serializers import (
    MflUserSerializer,
    MFLOAuthApplicationSerializer,
    PermissionSerializer,
    GroupSerializer
)

from .filters import MFLUserFilter, PermissionFilter, GroupFilter


class PermissionsListView(generics.ListAPIView):
    """
    This is a read-only list view; intentionally.

    The system's role based access control is built on the foundations put in
    place in `django.contrib.auth` and `django.contrib.admin`. The official
    web front-ends rely on the "automatic" permissions.
    """
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer
    filter_class = PermissionFilter
    ordering_fields = ('name', )


class GroupListView(generics.ListCreateAPIView):
    queryset = ProxyGroup.objects.all()
    serializer_class = GroupSerializer
    filter_class = GroupFilter
    ordering_fields = ('name', )


class GroupDetailView(CustomRetrieveUpdateDestroyView):
    queryset = ProxyGroup.objects.all()
    serializer_class = GroupSerializer


class UserList(generics.ListCreateAPIView):
    queryset = MflUser.objects.all()
    serializer_class = MflUserSerializer
    filter_class = MFLUserFilter
    ordering_fields = ('first_name', 'last_name', 'email', 'username',)

    def get_queryset(self, *args, **kwargs):
        from common.models import UserCounty, UserConstituency
        user = self.request.user
        if user.county and not user.is_national:
            county_users = [
                const_user.user.id for const_user in
                UserCounty.objects.filter(
                    county=user.county).distinct()
            ]
            sub_county_users = [
                const_user.user.id for const_user in
                UserConstituency.objects.filter(
                    constituency__county=user.county).distinct()
            ]
            area_users = county_users + sub_county_users
            return MflUser.objects.filter(
                id__in=area_users).exclude(id=self.request.user.id)
        elif user.is_national and not user.is_superuser:
            # Should see the county users and the national users
            # Also should not see the system user
            county_users = [
                county_user.user.id for county_user in
                UserCounty.objects.all().distinct()
            ]
            national_users = [
                nat_user.id for nat_user in MflUser.objects.filter(
                    is_national=True)
            ]
            all_users = county_users + national_users
            return MflUser.objects.filter(
                id__in=all_users).exclude(id=self.request.user.id)
        elif user.constituency:
            all_users = MflUser.objects.all()
            users_to_see = []
            group = Group.objects.get(name='Facility Viewing Group')
            for user in all_users:
                if group in user.groups.all():
                    users_to_see.append(user.id)
                if not user.groups.all():
                    users_to_see.append(user.id)

            return MflUser.objects.filter(
                id__in=users_to_see, created_by_id=self.request.user.id
            ).exclude(id=self.request.user.id)
        elif user.is_superuser:
            return MflUser.objects.all().exclude(id=self.request.user.id)

        else:
            # The user is not allowed to see the users
            return MflUser.objects.none()


class UserDetailView(CustomRetrieveUpdateDestroyView):
    queryset = MflUser.objects.all()
    serializer_class = MflUserSerializer

{'fraction_non_alphanumeric': 0.07725660228616477, 'fraction_numerical': 0.00013138877939823938, 'mean_word_length': 2.374113475177305, 'pattern_counts': {'":': 0, '<': 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': '4689321', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2051, 'n_words': 462}