<commit_before>from rdrf.models import Registry
from rdrf.models import RDRFContext
from django.contrib.contenttypes.models import ContentType

import logging

logger = logging.getLogger("registry_log")


class RDRFContextError(Exception):
    pass


def create_rdrf_default_contexts(patient, registry_ids):
    content_type = ContentType.objects.get_for_model(patient)
    for registry_id in registry_ids:
        try:
            registry_model = Registry.objects.get(id=registry_id)
        except Registry.DoesNotExist:
            continue

        existing_contexts_count = RDRFContext.objects.filter(registry=registry_model,
                                                             content_type=content_type,
                                                             object_id=patient.pk).count()

        if existing_contexts_count == 0:
            logger.debug("creating default context for patient %s registry %s" % (patient, registry_model))
            rdrf_context = RDRFContext(registry=registry_model, content_object=patient)
            rdrf_context.display_name = "default"
            rdrf_context.save()
            logger.debug("context saved ok")


class RDRFContextManager(object):
    def __init__(self, registry_model):
        self.registry_model = registry_model
        self.supports_contexts = self.registry_model.has_feature("contexts")

    def get_or_create_default_context(self, patient_model, new_patient=False):
        if not self.supports_contexts:
            content_type = ContentType.objects.get_for_model(patient_model)
            contexts = [c for c in RDRFContext.objects.filter(registry=self.registry_model,
                                                              content_type=content_type,
                                                              object_id=patient_model.pk)]
            if len(contexts) == 0:
                # No default setup so create one
                return self.create_context(patient_model, "default")
            elif len(contexts) == 1:
                return contexts[0]
            else:
                raise RDRFContextError("Patient %s in %s has more than 1 context" % (patient_model, self.registry_model))
        else:
            if new_patient:
                return self._create_initial_context_for_new_patient(patient_model)
            else:
                raise RDRFContextError("Registry %s supports contexts so there is no default context" % self.registry_model)

    def _create_initial_context_for_new_patient(self, patient_model):
        initial_context_name = "Initial %s" % self.registry_model.context_name
        return self.create_context(patient_model, initial_context_name)

    def create_context(self, patient_model, display_name):
        rdrf_context = RDRFContext(registry=self.registry_model, content_object=patient_model, display_name=display_name)
        rdrf_context.save()
        return rdrf_context

    def get_context(self, context_id, patient_model):
        if context_id is None:
            return self.get_or_create_default_context(patient_model)

        content_type = ContentType.objects.get_for_model(patient_model)
        try:
            rdrf_context_model = RDRFContext.objects.get(pk=context_id,
                                                         registry=self.registry_model,
                                                         content_type=content_type,
                                                         object_id=patient_model.pk)
            return rdrf_context_model
        except RDRFContext.DoesNotExist:
            raise RDRFContextError("Context does not exist")<commit_msg>REG-10 When new patient added, use the default context or create one if none exists<commit_after>from rdrf.models import Registry
from rdrf.models import RDRFContext
from django.contrib.contenttypes.models import ContentType

import logging

logger = logging.getLogger("registry_log")


class RDRFContextError(Exception):
    pass


def create_rdrf_default_contexts(patient, registry_ids):
    content_type = ContentType.objects.get_for_model(patient)
    for registry_id in registry_ids:
        try:
            registry_model = Registry.objects.get(id=registry_id)
        except Registry.DoesNotExist:
            continue

        existing_contexts_count = RDRFContext.objects.filter(registry=registry_model,
                                                             content_type=content_type,
                                                             object_id=patient.pk).count()

        if existing_contexts_count == 0:
            logger.debug("creating default context for patient %s registry %s" % (patient, registry_model))
            rdrf_context = RDRFContext(registry=registry_model, content_object=patient)
            rdrf_context.display_name = "default"
            rdrf_context.save()
            logger.debug("context saved ok")


class RDRFContextManager(object):
    def __init__(self, registry_model):
        self.registry_model = registry_model
        self.supports_contexts = self.registry_model.has_feature("contexts")

    def get_or_create_default_context(self, patient_model, new_patient=False):
        if not self.supports_contexts:
            content_type = ContentType.objects.get_for_model(patient_model)
            contexts = [c for c in RDRFContext.objects.filter(registry=self.registry_model,
                                                              content_type=content_type,
                                                              object_id=patient_model.pk)]
            if len(contexts) == 0:
                # No default setup so create one
                return self.create_context(patient_model, "default")
            elif len(contexts) == 1:
                return contexts[0]
            else:
                raise RDRFContextError("Patient %s in %s has more than 1 context" % (patient_model, self.registry_model))
        else:
            if new_patient:
                return self._create_initial_context_for_new_patient(patient_model)
            else:
                raise RDRFContextError("Registry %s supports contexts so there is no default context" % self.registry_model)

    def _create_initial_context_for_new_patient(self, patient_model):
        content_type = ContentType.objects.get_for_model(patient_model)
        contexts = [c for c in RDRFContext.objects.filter(registry=self.registry_model,
                                                           content_type=content_type,
                                                           object_id=patient_model.pk).order_by("pk")]
        if len(contexts) > 0:
            return contexts[0]
        else:
            return self.create_context(patient_model, "default")

    def create_context(self, patient_model, display_name):
        rdrf_context = RDRFContext(registry=self.registry_model, content_object=patient_model, display_name=display_name)
        rdrf_context.save()
        return rdrf_context

    def get_context(self, context_id, patient_model):
        if context_id is None:
            return self.get_or_create_default_context(patient_model)

        content_type = ContentType.objects.get_for_model(patient_model)
        try:
            rdrf_context_model = RDRFContext.objects.get(pk=context_id,
                                                         registry=self.registry_model,
                                                         content_type=content_type,
                                                         object_id=patient_model.pk)
            return rdrf_context_model
        except RDRFContext.DoesNotExist:
            raise RDRFContextError("Context does not exist")
{'fraction_non_alphanumeric': 0.06505961638154484, 'fraction_numerical': 0.0018144116122343183, 'mean_word_length': 2.0671701112877585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1520424', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1913, 'n_words': 389}