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

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

Speed up draft registrations query.
Speed up draft registrations query.

  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)
+     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 = DraftRegistration.objects.filter(
+         drafts = drafts.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
  
Speed up draft registrations query.
## Code 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

## Instruction:
Speed up draft registrations query.
## Code 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
