<commit_before>from django.http import Http404, HttpResponseForbidden

from rest_framework import viewsets, decorators, status, mixins
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from dry_rest_permissions.generics import DRYPermissions

from sigma_core.models.group_field import GroupField
from sigma_core.serializers.group_field import GroupFieldSerializer, GroupFieldCreateSerializer

class GroupFieldViewSet(mixins.CreateModelMixin,    # Only Group admin
                   mixins.RetrieveModelMixin,       # TODO
                   mixins.UpdateModelMixin,         # TODO
                   mixins.DestroyModelMixin,        # Only Group admin
                   mixins.ListModelMixin,           # Every Group member (including not accepted group members - for "open" groups)
                   viewsets.GenericViewSet):
    queryset = GroupField.objects.all()
    serializer_class = GroupFieldSerializer
    permission_classes = [IsAuthenticated, DRYPermissions, ]
    filter_fields = ('name', )

    # You will never see fields for groups you are not a member of
    def get_queryset(self):
        from sigma_core.models.group_member import GroupMember
        if not self.request.user.is_authenticated():
            return self.queryset.none()
        if self.request.user.is_sigma_admin():
            return self.queryset
        # @sqlperf: Find which one is the most efficient
        my_groups = GroupMember.objects.filter(user=self.request.user.id).values_list('group', flat=True)
        #my_groups = Group.objects.filter(memberships__user=self.request.user.id)
        return self.queryset.filter(group__in=my_groups)<commit_msg>Implement GET /group-field/{pk} ... But I don't believe we will need it<commit_after>from django.http import Http404, HttpResponseForbidden

from rest_framework import viewsets, decorators, status, mixins
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from dry_rest_permissions.generics import DRYPermissions

from sigma_core.models.group_field import GroupField
from sigma_core.serializers.group_field import GroupFieldSerializer, GroupFieldCreateSerializer

class GroupFieldViewSet(mixins.CreateModelMixin,    # Only Group admin
                   mixins.RetrieveModelMixin,       # Every Group member (including not accepted group members - for "open" groups)
                   mixins.UpdateModelMixin,         # TODO
                   mixins.DestroyModelMixin,        # Same permission as create
                   mixins.ListModelMixin,           # Same permission as retrieve
                   viewsets.GenericViewSet):
    queryset = GroupField.objects.all()
    serializer_class = GroupFieldSerializer
    permission_classes = [IsAuthenticated, DRYPermissions, ]
    filter_fields = ('name', )

    # You will never see fields for groups you are not a member of
    def get_queryset(self):
        from sigma_core.models.group_member import GroupMember
        if not self.request.user.is_authenticated():
            return self.queryset.none()
        if self.request.user.is_sigma_admin():
            return self.queryset
        # @sqlperf: Find which one is the most efficient
        my_groups = GroupMember.objects.filter(user=self.request.user.id).values_list('group', flat=True)
        #my_groups = Group.objects.filter(memberships__user=self.request.user.id)
        return self.queryset.filter(group__in=my_groups)
{'fraction_non_alphanumeric': 0.06786941580756013, 'fraction_numerical': 0.001718213058419244, 'mean_word_length': 3.208433734939759, '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': '6251744', 'n_tokens_mistral': 926, 'n_tokens_neox': 904, 'n_words': 235}