<commit_before>REG_CAMPAIGNS = {
    'prereg': 'Prereg Challenge',
    'registered_report': 'Registered Report Protocol Preregistration',
}

def get_campaign_schema(campaign):
    from osf.models import RegistrationSchema
    if campaign not in REG_CAMPAIGNS:
        raise ValueError('campaign must be one of: {}'.format(', '.join(REG_CAMPAIGNS.keys())))
    schema_name = REG_CAMPAIGNS[campaign]

    return RegistrationSchema.objects.get(name=schema_name, schema_version=2)

def drafts_for_user(user, campaign=None):
    from osf.models import DraftRegistration, Node
    from guardian.shortcuts import get_objects_for_user

    if not user or user.is_anonymous:
        return None

    node_qs = get_objects_for_user(user, 'admin_node', Node, with_superuser=False).exclude(is_deleted=True)

    if campaign:
        drafts = DraftRegistration.objects.filter(
            registration_schema=get_campaign_schema(campaign),
            approval=None,
            registered_node=None,
            deleted__isnull=True,
            branched_from__in=list(node_qs),
            initiator=user
        )
    else:
        drafts = DraftRegistration.objects.filter(
            approval=None,
            registered_node=None,
            deleted__isnull=True,
            branched_from__in=list(node_qs),
            initiator=user
        )
    return drafts<commit_msg>Speed up draft registrations query.<commit_after>REG_CAMPAIGNS = {
    'prereg': 'Prereg Challenge',
    'registered_report': 'Registered Report Protocol Preregistration',
}

def get_campaign_schema(campaign):
    from osf.models import RegistrationSchema
    if campaign not in REG_CAMPAIGNS:
        raise ValueError('campaign must be one of: {}'.format(', '.join(REG_CAMPAIGNS.keys())))
    schema_name = REG_CAMPAIGNS[campaign]

    return RegistrationSchema.objects.get(name=schema_name, schema_version=2)

def drafts_for_user(user, campaign=None):
    from osf.models import DraftRegistration, Node
    from guardian.shortcuts import get_objects_for_user

    if not user or user.is_anonymous:
        return None

    node_qs = get_objects_for_user(user, 'admin_node', Node, with_superuser=False).exclude(is_deleted=True).values_list('id', flat=True)

    drafts = DraftRegistration.objects.filter(
        approval=None,
        registered_node=None,
        deleted__isnull=True,
        branched_from__in=node_qs,
        initiator=user
    )

    if campaign:
        drafts = drafts.filter(
            registration_schema=get_campaign_schema(campaign),
        )

    return drafts
{'fraction_non_alphanumeric': 0.08538011695906433, 'fraction_numerical': 0.0007797270955165692, 'mean_word_length': 3.3863247863247863, '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': '5258202', 'n_tokens_mistral': 792, 'n_tokens_neox': 749, 'n_words': 152}