<commit_before>from django.core.exceptions import ValidationError
from django.db.models import Q
from django.http import HttpResponse
from rest_framework import mixins, status
from rest_framework.generics import RetrieveAPIView, ListAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.response import Response
from concepts.permissions import CanEditParentDictionary, CanViewParentDictionary
from mappings.filters import PublicMappingsSearchFilter, SourceRestrictedMappingsFilter, CollectionRestrictedMappingFilter
from mappings.models import Mapping, MappingVersion
from mappings.serializers import MappingCreateSerializer, MappingUpdateSerializer, MappingDetailSerializer, MappingListSerializer, \
    MappingVersionDetailSerializer
from oclapi.mixins import ListWithHeadersMixin
from oclapi.models import ACCESS_TYPE_NONE
from oclapi.views import ConceptDictionaryMixin, BaseAPIView
from sources.models import SourceVersion
from orgs.models import Organization
from users.models import UserProfile

INCLUDE_RETIRED_PARAM = 'includeRetired'
LIMIT_PARAM = 'limit'


class MappingBaseView(ConceptDictionaryMixin):
    lookup_field = 'mapping'
    pk_field = 'id'
    model = Mapping
    child_list_attribute = 'mappings'
    include_retired = False
    permission_classes = (CanViewParentDictionary,)
    parent_resource_version = None
    parent_resource_version_model = SourceVersion
    child_list_attribute = 'mappings'

    def initialize(self, request, path_info_segment, **kwargs):
        super(MappingBaseView, self).initialize(request, path_info_segment, **kwargs)
        if self.parent_resource:
            if hasattr(self.parent_resource, 'versioned_object'):
                self.parent_resource_version = self.parent_resource
                self.parent_resource = self.parent_resource.versioned_object
            else:
                self.parent_resource_version = SourceVersion.get_latest_version_of(self.parent_resource)
<commit_msg>#213 +Bhawna fix fetching of latest mapping in case of HEAD and latest version<commit_after>from django.core.exceptions import ValidationError
from django.db.models import Q
from django.http import HttpResponse
from rest_framework import mixins, status
from rest_framework.generics import RetrieveAPIView, ListAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.response import Response
from concepts.permissions import CanEditParentDictionary, CanViewParentDictionary
from mappings.filters import PublicMappingsSearchFilter, SourceRestrictedMappingsFilter, CollectionRestrictedMappingFilter
from mappings.models import Mapping, MappingVersion
from mappings.serializers import MappingCreateSerializer, MappingUpdateSerializer, MappingDetailSerializer, MappingListSerializer, \
    MappingVersionDetailSerializer
from oclapi.mixins import ListWithHeadersMixin
from oclapi.models import ACCESS_TYPE_NONE
from oclapi.views import ConceptDictionaryMixin, BaseAPIView
from sources.models import SourceVersion
from orgs.models import Organization
from users.models import UserProfile

INCLUDE_RETIRED_PARAM = 'includeRetired'
LIMIT_PARAM = 'limit'


class MappingBaseView(ConceptDictionaryMixin):
    lookup_field = 'mapping'
    pk_field = 'id'
    model = Mapping
    child_list_attribute = 'mappings'
    include_retired = False
    permission_classes = (CanViewParentDictionary,)
    parent_resource_version = None
    parent_resource_version_model = SourceVersion
    child_list_attribute = 'mappings'

    def initialize(self, request, path_info_segment, **kwargs):
        super(MappingBaseView, self).initialize(request, path_info_segment, **kwargs)
        if self.parent_resource:
            if hasattr(self.parent_resource, 'versioned_object'):
                self.parent_resource_version = self.parent_resource
                self.parent_resource = self.parent_resource.versioned_object
            else:
                self.parent_resource_version = self.parent_resource.get_head()

{'fraction_non_alphanumeric': 0.05311951891756452, 'fraction_numerical': 0.0007516913054372337, 'mean_word_length': 5.438709677419355, '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': '3292901', 'n_tokens_mistral': 1044, 'n_tokens_neox': 1006, 'n_words': 259}