<commit_before>from rest_condition import Or
from rest_framework import viewsets
from rest_framework.decorators import api_view, detail_route
from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated, \
    IsAuthenticatedOrReadOnly
from rest_framework.response import Response

from account.api.v1.filtersets import UserProfileFilterSet, SchoolFilterSet
from account.api.v1.serializers import UserProfileSerializer, SchoolSerializer
from account.models import Profile, CITY_CHOICES, GENDER_TYPES, School
from authx.permissions import IsAdminUser, SelfOnly


@api_view(['GET'])
def city_list(request, version=None):
    result = [ {'value': city_tuple[0], 'label': city_tuple[1], } for city_tuple in CITY_CHOICES ]
    return Response(result)

@api_view(['GET'])
def gender_list(request, version=None):
    result = [ {'value': gender_tuple[0], 'label': gender_tuple[1], } for gender_tuple in GENDER_TYPES ]
    return Response(result)

'''
from rest_framework.filters import SearchFilter, OrderingFilter
from url_filter.integrations.drf import DjangoFilterBackend
class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = UserProfileSerializer
    permission_classes = (permissions.IsAuthenticated,)
    filter_class = UserProfileFilterSet
    filter_backends = (
        DjangoFilterBackend, # this backend expose the structure of object definition
                            # good for internal projects not for external ones
        SearchFilter,
        OrderingFilter,
    )
'''

class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = UserProfileSerializer
    permission_classes = (IsAuthenticated,
                          Or(IsAdminUser, SelfOnly), )
    filter_class = UserProfileFilterSet
    
    def get_permissions(self):
        # based on the answer on stackoverflow, this is the best solution
        # decorator perview on viewset is verified as not working <commit_msg>profile without associated user considered as not found<commit_after>from rest_condition import Or
from rest_framework import viewsets
from rest_framework.decorators import api_view, detail_route
from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated, \
    IsAuthenticatedOrReadOnly
from rest_framework.response import Response

from account.api.v1.filtersets import UserProfileFilterSet, SchoolFilterSet
from account.api.v1.serializers import UserProfileSerializer, SchoolSerializer
from account.models import Profile, CITY_CHOICES, GENDER_TYPES, School
from authx.permissions import IsAdminUser, SelfOnly


@api_view(['GET'])
def city_list(request, version=None):
    result = [ {'value': city_tuple[0], 'label': city_tuple[1], } for city_tuple in CITY_CHOICES ]
    return Response(result)

@api_view(['GET'])
def gender_list(request, version=None):
    result = [ {'value': gender_tuple[0], 'label': gender_tuple[1], } for gender_tuple in GENDER_TYPES ]
    return Response(result)

'''
from rest_framework.filters import SearchFilter, OrderingFilter
from url_filter.integrations.drf import DjangoFilterBackend
class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = UserProfileSerializer
    permission_classes = (permissions.IsAuthenticated,)
    filter_class = UserProfileFilterSet
    filter_backends = (
        DjangoFilterBackend, # this backend expose the structure of object definition
                            # good for internal projects not for external ones
        SearchFilter,
        OrderingFilter,
    )
'''

class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = Profile.objects.filter(user__isnull=False)
    serializer_class = UserProfileSerializer
    permission_classes = (IsAuthenticated,
                          Or(IsAdminUser, SelfOnly), )
    filter_class = UserProfileFilterSet
    
    def get_permissions(self):
        # based on the answer on stackoverflow, this is the best solution
        # decorator perview on viewset is verified as not working 
{'fraction_non_alphanumeric': 0.06873094065212292, 'fraction_numerical': 0.00422237860661506, 'mean_word_length': 4.62532981530343, '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': '4856373', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1135, 'n_words': 317}