<commit_before># (c) 2012-2014, Ansible, Inc. <<EMAIL>>
#
# This file is part of Ansible Galaxy
#
# Ansible Galaxy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible Galaxy 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# standard python libraries
import sys
import math

# rest framework stuff
from rest_framework import filters
from rest_framework import mixins
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.permissions import AllowAny, IsAuthenticated

# django stuff
from django.contrib.auth.models import AnonymousUser
from django.db.models import F, Count, Avg, Prefetch
from django.http import Http404
from django.utils.datastructures import SortedDict
from django.apps import apps

# haystack
from drf_haystack.viewsets import HaystackViewSet
from drf_haystack.filters import HaystackAutocompleteFilter
from galaxy.api.filters import HaystackFilter
from haystack.query import SearchQuerySet

# elasticsearch dsl
from elasticsearch_dsl import Search, Q

# local stuff
from galaxy.api.aggregators import *
from galaxy.api.access import *
from galaxy.api.base_views import *
from galaxy.api.permissions import *
from galaxy.api.serializers import *
from galaxy.main.models import *
from galaxy.main.utils import camelcase_to_underscore


#--------------------------------------------------------------------------------
# Helper functions

def filter_user_queryset(qs):
    return qs.filter(is_active=True)

def filter_role_queryset(qs):
    return qs.filter(active=True, is_valid=True)

def filter_rating_queryset(qs):
    return qs.filter(
               active=True,
               role__active=True,
               role__is_valid=True,
               owner__is_active=True,
           )

def annotate_user_queryset(qs):
    return qs.annotate(
               num_ratings = Count('ratings', distinct=True),
               num_roles = Count('roles', distinct=True),
               avg_rating = AvgWithZeroForNull('ratings__score'),
               avg_role_score = AvgWithZeroForNull('roles__average_score'),
           )

def annotate_role_queryset(qs):
    return qs.annotate(
               num_ratings = Count('ratings', distinct=True),
               average_score = AvgWithZeroForNull('ratings__score'),
               avg_reliability   = AvgWithZeroForNull('ratings__reliability'),
               avg_documentation = AvgWithZeroForNull('ratings__documentation'),
               avg_code_quality  = AvgWithZeroForNull('ratings__code_quality'),
               avg_wow_factor    = AvgWithZeroForNull('ratings__wow_factor'),
           )

#--------------------------------------------------------------------------------

class ApiRootView(APIView):
    permission_classes = (AllowAny,)
    view_name = 'REST API'
    def get(self, request, format=None):
        ''' list supported API versions '''
        current = reverse('api:api_v1_root_view', args=[])
        data = dict(
            description = 'GALAXY REST API',
            current_version = current,
            available_versions = dict(
                v1 = current
            )
        )
        return Response(data)

class ApiV1RootView(APIView):
    permission_classes = (AllowAny,)
    view_name = 'Version 1'
    def get(self, request, format=None):
        ''' list top level resources '''
        data = SortedDict()
        data['me']         = reverse('api:user_me_list')
        data['users']      = reverse('api:user_list')
        data['roles']      = reverse('api:role_list')
        data['categories'] = reverse('api:category_list')
        data['tags']       = reverse('api:tag_list')
        data['platforms']  = reverse('api:platform_list')
        data['ratings']    = reverse('api:rating_list')
        data['search']     = reverse('api:search_view')
        return Response(data)

class ApiV1SearchView(APIView):
    permission_classes = (AllowAny,)
    view_name = 'Search'
    def get(self, request, *args, **kwargs):
        data = OrderedDict()
        data['platforms'] = reverse('api:platforms_search_view')
        data['roles'] = reverse('api:search-roles-list')<commit_msg>Return the correct value in current_version.<commit_after># (c) 2012-2014, Ansible, Inc. <<EMAIL>>
#
# This file is part of Ansible Galaxy
#
# Ansible Galaxy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible Galaxy 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# standard python libraries
import sys
import math

# rest framework stuff
from rest_framework import filters
from rest_framework import mixins
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.permissions import AllowAny, IsAuthenticated

# django stuff
from django.contrib.auth.models import AnonymousUser
from django.db.models import F, Count, Avg, Prefetch
from django.http import Http404
from django.utils.datastructures import SortedDict
from django.apps import apps

# haystack
from drf_haystack.viewsets import HaystackViewSet
from drf_haystack.filters import HaystackAutocompleteFilter
from galaxy.api.filters import HaystackFilter
from haystack.query import SearchQuerySet

# elasticsearch dsl
from elasticsearch_dsl import Search, Q

# local stuff
from galaxy.api.aggregators import *
from galaxy.api.access import *
from galaxy.api.base_views import *
from galaxy.api.permissions import *
from galaxy.api.serializers import *
from galaxy.main.models import *
from galaxy.main.utils import camelcase_to_underscore


#--------------------------------------------------------------------------------
# Helper functions

def filter_user_queryset(qs):
    return qs.filter(is_active=True)

def filter_role_queryset(qs):
    return qs.filter(active=True, is_valid=True)

def filter_rating_queryset(qs):
    return qs.filter(
               active=True,
               role__active=True,
               role__is_valid=True,
               owner__is_active=True,
           )

def annotate_user_queryset(qs):
    return qs.annotate(
               num_ratings = Count('ratings', distinct=True),
               num_roles = Count('roles', distinct=True),
               avg_rating = AvgWithZeroForNull('ratings__score'),
               avg_role_score = AvgWithZeroForNull('roles__average_score'),
           )

def annotate_role_queryset(qs):
    return qs.annotate(
               num_ratings = Count('ratings', distinct=True),
               average_score = AvgWithZeroForNull('ratings__score'),
               avg_reliability   = AvgWithZeroForNull('ratings__reliability'),
               avg_documentation = AvgWithZeroForNull('ratings__documentation'),
               avg_code_quality  = AvgWithZeroForNull('ratings__code_quality'),
               avg_wow_factor    = AvgWithZeroForNull('ratings__wow_factor'),
           )

#--------------------------------------------------------------------------------

class ApiRootView(APIView):
    permission_classes = (AllowAny,)
    view_name = 'REST API'
    def get(self, request, format=None):
        ''' list supported API versions '''
        current = reverse('api:api_v1_root_view', args=[])
        data = dict(
            description = 'GALAXY REST API',
            current_version = current,
            available_versions = dict(
                v1 = "v1"
            )
        )
        return Response(data)

class ApiV1RootView(APIView):
    permission_classes = (AllowAny,)
    view_name = 'Version 1'
    def get(self, request, format=None):
        ''' list top level resources '''
        data = SortedDict()
        data['me']         = reverse('api:user_me_list')
        data['users']      = reverse('api:user_list')
        data['roles']      = reverse('api:role_list')
        data['categories'] = reverse('api:category_list')
        data['tags']       = reverse('api:tag_list')
        data['platforms']  = reverse('api:platform_list')
        data['ratings']    = reverse('api:rating_list')
        data['search']     = reverse('api:search_view')
        return Response(data)

class ApiV1SearchView(APIView):
    permission_classes = (AllowAny,)
    view_name = 'Search'
    def get(self, request, *args, **kwargs):
        data = OrderedDict()
        data['platforms'] = reverse('api:platforms_search_view')
        data['roles'] = reverse('api:search-roles-list')
{'fraction_non_alphanumeric': 0.12028252161079486, 'fraction_numerical': 0.003689647902171621, 'mean_word_length': 2.8993012741471436, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3783838', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2568, 'n_words': 718}