<commit_before>from rest_framework import viewsets
from rest_framework.generics import ListAPIView

from .serializers import (
	LocaleSerializer, 
	AppInfoSerializer, 
	KeyStringSerializer, 
	AppInfoKeyStringSerializer,
	KeyValueSerializer,
	)
from .models import (
	Locale, 
	AppInfo, 
	KeyString, 
	AppInfoKeyString
	)

from rest_framework.renderers import (
	JSONRenderer,
    BrowsableAPIRenderer,
    )

from .renderers import (
	KeyStringIOSRenderer,
	KeyStringAndroidRenderer,
	)


class LocaleViewSets(viewsets.ModelViewSet):
	"""
	API endpoint that allows Locale to be viewed or edited.
	"""
	queryset		 = Locale.objects.all()
	serializer_class = LocaleSerializer


class AppInfoViewSets(viewsets.ModelViewSet):
	"""
	API endpoint that allows AppInfo to be viewed or edited.
	"""
	queryset		 = AppInfo.objects.all()
	serializer_class = AppInfoSerializer

class KeyStringViewSets(viewsets.ModelViewSet):
	"""
	API endpoint that allows KeyString to be viewed or edited.
	"""
	queryset 		 = KeyString.objects.all()
	serializer_class = KeyStringSerializer


class AppInfoKeyStringViewSets(viewsets.ModelViewSet):
	"""
	API endpoint that allows AppInfoKeyString to be viewed or edited.
	"""
	queryset		 = AppInfoKeyString.objects.all()
	serializer_class = AppInfoKeyStringSerializer


class KeyStringLocalizedView(ListAPIView):
	"""
	API endpoint that allows to view the key-value list of an specified app and locale
	"""

	serializer_class = KeyValueSerializer
	renderer_classes = (
		BrowsableAPIRenderer, 
		JSONRenderer, 
		KeyStringIOSRenderer,
		KeyStringAndroidRenderer,
		)

	def get_queryset(self):
		# .extra(select={'renamed_value': 'cryptic_value_name'})
		app_slug = self.kwargs['app_slug']
		locale_code = self.kwargs['locale_code']
		
		query = AppInfoKeyString.objects.filter(
			app_info__slug=app_slug, 
			key_string__values__locale__code=locale_code).values(
			'key_string__key',
			'key_string__values__value'
			).order_by('key_string__key')
		print(list(query))
		return query


locale_list_view = LocaleViewSets.as_view({
	'get': 'list',
	'post': 'create',
	})

locale_detail_view = LocaleViewSets.as_view({
	'get'	:	'retrieve',
	'put'	: 	'update',
	'patch'	:	'partial_update',
	'delete':	'destroy',
	})

app_info_list_view = AppInfoViewSets.as_view({
	'get'	:	'list',
	'post'	: 	'create',
	})

app_info_detail_view = AppInfoViewSets.as_view({
	'get'	:	'retrieve',
	'put'	:	'update',
	'patch'	:	'partial_update',
	'delete': 	'destroy',
	})


key_string_list_view = KeyStringViewSets.as_view({
	'get'	:	'list',
	'post'	:	'create',
	})

key_string_detail_view = KeyStringViewSets.as_view({
	'get'	:	'retrieve',
	'put'	:	'update',
	'patch'	:	'partial_update',
	'delete':	'destroy',
	})

app_info_key_string_list_view = AppInfoKeyStringViewSets.as_view({
	'get'	:	'list',
	'post'	:	'create',
	})

app_info_key_string_detail_view = AppInfoKeyStringViewSets.as_view({
	'get'	:	'retrieve',
	'put'	:	'update',
	'patch'	:	'partial_update',
	'delete':	'destroy',
	})

key_value_list_view = KeyStringLocalizedView.as_view()

<commit_msg>implement base_locale<commit_after>from rest_framework import viewsets
from rest_framework.generics import ListAPIView
from django.http import Http404

from .serializers import (
    LocaleSerializer, 
    AppInfoSerializer, 
    KeyStringSerializer, 
    AppInfoKeyStringSerializer,
    KeyValueSerializer,
    )
from .models import (
    Locale, 
    AppInfo, 
    KeyString, 
    AppInfoKeyString
    )

from rest_framework.renderers import (
    JSONRenderer,
    BrowsableAPIRenderer,
    )

from .renderers import (
    KeyStringIOSRenderer,
    KeyStringAndroidRenderer,
    )


class LocaleViewSets(viewsets.ModelViewSet):
    """
    API endpoint that allows Locale to be viewed or edited.
    """
    queryset         = Locale.objects.all()
    serializer_class = LocaleSerializer


class AppInfoViewSets(viewsets.ModelViewSet):
    """
    API endpoint that allows AppInfo to be viewed or edited.
    """
    queryset         = AppInfo.objects.all()
    serializer_class = AppInfoSerializer

class KeyStringViewSets(viewsets.ModelViewSet):
    """
    API endpoint that allows KeyString to be viewed or edited.
    """
    queryset         = KeyString.objects.all()
    serializer_class = KeyStringSerializer


class AppInfoKeyStringViewSets(viewsets.ModelViewSet):
    """
    API endpoint that allows AppInfoKeyString to be viewed or edited.
    """
    queryset         = AppInfoKeyString.objects.all()
    serializer_class = AppInfoKeyStringSerializer


class KeyStringLocalizedView(ListAPIView):
    """
    API endpoint that allows to view the key-value list of an specified app and locale
    """

    serializer_class = KeyValueSerializer
    renderer_classes = (
        BrowsableAPIRenderer, 
        JSONRenderer, 
        KeyStringIOSRenderer,
        KeyStringAndroidRenderer,
        )

    def get_queryset(self):

        app = None
        app_slug = self.kwargs['app_slug']

        try:
            app = AppInfo.objects.select_related().get(slug=app_slug)
        except AppInfo.DoesNotExist:
            raise Http404("App does not exist.") 

        locale_code = self.kwargs['locale_code']

        all_key_strings = AppInfoKeyString.objects.filter(
            app_info__slug=app_slug,
        )

        localized_strings_query = all_key_strings.filter(
            key_string__values__locale__code=locale_code
        ).values(
            'key_string__key',
            'key_string__values__value'
        ).order_by('key_string__key')

        if app.base_locale:
            
            # just create a new queryset to avoid evaluation of the localized_string_query
            localized_ids_query = all_key_strings.filter(
                key_string__values__locale__code=locale_code
            ).values_list(
                'key_string__pk',
                flat=True
            )

            # then check for missing ones by excluding the found ones
            missing_strings_query = all_key_strings.filter(
                key_string__values__locale__code=app.base_locale.code
            ).exclude(key_string__pk__in=localized_ids_query).values(
                'key_string__key',
                'key_string__values__value'
            )

            localized_strings_query = localized_strings_query | missing_strings_query

        return localized_strings_query.order_by('key_string__key')


locale_list_view = LocaleViewSets.as_view({
    'get': 'list',
    'post': 'create',
    })

locale_detail_view = LocaleViewSets.as_view({
    'get'   :   'retrieve',
    'put'   :   'update',
    'patch' :   'partial_update',
    'delete':   'destroy',
    })

app_info_list_view = AppInfoViewSets.as_view({
    'get'   :   'list',
    'post'  :   'create',
    })

app_info_detail_view = AppInfoViewSets.as_view({
    'get'   :   'retrieve',
    'put'   :   'update',
    'patch' :   'partial_update',
    'delete':   'destroy',
    })


key_string_list_view = KeyStringViewSets.as_view({
    'get'   :   'list',
    'post'  :   'create',
    })

key_string_detail_view = KeyStringViewSets.as_view({
    'get'   :   'retrieve',
    'put'   :   'update',
    'patch' :   'partial_update',
    'delete':   'destroy',
    })

app_info_key_string_list_view = AppInfoKeyStringViewSets.as_view({
    'get'   :   'list',
    'post'  :   'create',
    })

app_info_key_string_detail_view = AppInfoKeyStringViewSets.as_view({
    'get'   :   'retrieve',
    'put'   :   'update',
    'patch' :   'partial_update',
    'delete':   'destroy',
    })

key_value_list_view = KeyStringLocalizedView.as_view()


{'fraction_non_alphanumeric': 0.1016971451124852, 'fraction_numerical': 0.0007893698197605578, 'mean_word_length': 3.3589449541284404, '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': '7183077', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2493, 'n_words': 465}