<commit_before>import json
import logging

from restlib2.resources import Resource
from restlib2.http import codes
from django.http import HttpResponse

from api.helpers import FormHelpers
from constants import ErrorConstants
from core.models.identities import ExternalSystem, ExternalRecord, Organization
from core.forms import ExternalSystemForm

log = logging.getLogger(__name__)

class ExternalSystemCrossReference(Resource):
    supported_accept_types = ['application/json']
    model = 'core.models.identities.ExternalSystem'

    class Meta(object):
        abstract = True

    def _read_and_action(self, request, func, pk):
        '''
        This method attempts to find an ExternalSystem object given the pk. If found it
        submits it to func for processing. This function is expected to operate on the
        response list object which is then returned by this function'''
        es = None
        try:
            es = ExternalSystem.objects.get(pk=pk)
        except ExternalSystem.DoesNotExist:
            log.error("ExternalSystem[{0}] not found".format(pk))
            return HttpResponse(status=codes.not_found)
        if es:
            response = []
            func(es, response)
            return json.dumps(response)

    def getOrganization(self, org_id):
        org = None
        if org_id:
            org = Organization.objects.get(pk=org_id)
        return org


class ExternalSystemRecords(ExternalSystemCrossReference):

    def get(self, request, pk, **kwargs):
        '''
        This resource provides a response with all of the external records that are
        associated with this external system. If the org_pk is provided,
        then only external records whos subject record belong to the organization
        will be returned. If the path is provide only external records with the
        path will be included
        '''

        try:
            org = self.getOrganization(kwargs.pop('org_pk', None))
        except Organization.DoesNotExist:
            log.error("Unable to retrieve ExternalSystemRecords. Organization not found")
            return HttpResponse(status=codes.not_found)

        def process(es, response):
            qs = ExternalRecord.objects.filter(external_system=es)

            if org:
                qs = qs.filter(subject__organization=org)

            for er in qs:
                r = er.responseFieldDict()
                response.append(r)

        return self._read_and_action(request, process, pk)


class ExternalSystemSubjects(ExternalSystemCrossReference):

    def get(self, request, pk, **kwargs):
        '''
        This resource provides a response with all of the subjects that have
        records associated with this external system. If the org_pk is provided,
        then only subject records that belong to this organization will be returned
        '''
        try:
            org = self.getOrganization(kwargs.pop('org_pk', None))
        except Organization.DoesNotExist:
            log.error("Unable to retrieve ExternalSystemSubjects. Organization not found")
            return HttpResponse(status=codes.not_found)

        def process(es, response):
            if not org:
                qs = es.subjects.all()
            else:
                qs = es.subjects.filter(organization=org)

            for s in qs:
                r = s.responseFieldDict()
                response.append(r)

        return self._read_and_action(request, process, pk)


class ExternalSystemQuery(Resource):

    supported_accept_types = ['application/json']
    model = 'core.models.identities.ExternalSystem'<commit_msg>Make external system resource distinct<commit_after>import json
import logging

from restlib2.resources import Resource
from restlib2.http import codes
from django.http import HttpResponse

from api.helpers import FormHelpers
from constants import ErrorConstants
from core.models.identities import ExternalSystem, ExternalRecord, Organization
from core.forms import ExternalSystemForm

log = logging.getLogger(__name__)

class ExternalSystemCrossReference(Resource):
    supported_accept_types = ['application/json']
    model = 'core.models.identities.ExternalSystem'

    class Meta(object):
        abstract = True

    def _read_and_action(self, request, func, pk):
        '''
        This method attempts to find an ExternalSystem object given the pk. If found it
        submits it to func for processing. This function is expected to operate on the
        response list object which is then returned by this function'''
        es = None
        try:
            es = ExternalSystem.objects.get(pk=pk)
        except ExternalSystem.DoesNotExist:
            log.error("ExternalSystem[{0}] not found".format(pk))
            return HttpResponse(status=codes.not_found)
        if es:
            response = []
            func(es, response)
            return json.dumps(response)

    def getOrganization(self, org_id):
        org = None
        if org_id:
            org = Organization.objects.get(pk=org_id)
        return org


class ExternalSystemRecords(ExternalSystemCrossReference):

    def get(self, request, pk, **kwargs):
        '''
        This resource provides a response with all of the external records that are
        associated with this external system. If the org_pk is provided,
        then only external records whos subject record belong to the organization
        will be returned. If the path is provide only external records with the
        path will be included
        '''

        try:
            org = self.getOrganization(kwargs.pop('org_pk', None))
        except Organization.DoesNotExist:
            log.error("Unable to retrieve ExternalSystemRecords. Organization not found")
            return HttpResponse(status=codes.not_found)

        def process(es, response):
            qs = ExternalRecord.objects.filter(external_system=es)

            if org:
                qs = qs.filter(subject__organization=org)

            for er in qs:
                r = er.responseFieldDict()
                response.append(r)

        return self._read_and_action(request, process, pk)


class ExternalSystemSubjects(ExternalSystemCrossReference):

    def get(self, request, pk, **kwargs):
        '''
        This resource provides a response with all of the subjects that have
        records associated with this external system. If the org_pk is provided,
        then only subject records that belong to this organization will be returned
        '''
        try:
            org = self.getOrganization(kwargs.pop('org_pk', None))
        except Organization.DoesNotExist:
            log.error("Unable to retrieve ExternalSystemSubjects. Organization not found")
            return HttpResponse(status=codes.not_found)

        def process(es, response):
            if not org:
                qs = es.subjects.all().distinct()
            else:
                qs = es.subjects.filter(organization=org).distinct()
            for s in qs:
                r = s.responseFieldDict()
                response.append(r)

        return self._read_and_action(request, process, pk)


class ExternalSystemQuery(Resource):

    supported_accept_types = ['application/json']
    model = 'core.models.identities.ExternalSystem'
{'fraction_non_alphanumeric': 0.07095890410958904, 'fraction_numerical': 0.0008219178082191781, 'mean_word_length': 2.5270531400966183, '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': '2551915', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1772, 'n_words': 597}