from .models import Campaign, InformationObject


def connect_info_object(sender, **kwargs):
    reference = kwargs.get('reference')
    if reference is None:
        return
    if 'campaign' not in reference:
        return
    try:
        campaign, slug = reference['campaign'].split('@', 1)
    except (ValueError, IndexError):
        return

    try:
        campaign_pk = int(campaign)
    except ValueError:
        return

    try:
        campaign = Campaign.objects.get(pk=campaign_pk)
    except Campaign.DoesNotExist:
        return

    try:
        iobj = InformationObject.objects.get(campaign=campaign, slug=slug)
    except InformationObject.DoesNotExist:
        return

    if iobj.foirequest is not None:
        return

    if iobj.publicbody != sender.public_body:
        return

    if not sender.public:
        return

    iobj.foirequest = sender
    iobj.save()

from .models import Campaign, InformationObject


def connect_info_object(sender, **kwargs):
    reference = kwargs.get('reference')
    if not reference:
        return
    if not reference.startswith('campaign:'):
        return
    namespace, campaign_value = reference.split(':', 1)
    try:
        campaign, slug = campaign_value.split('@', 1)
    except (ValueError, IndexError):
        return

    try:
        campaign_pk = int(campaign)
    except ValueError:
        return

    try:
        campaign = Campaign.objects.get(pk=campaign_pk)
    except Campaign.DoesNotExist:
        return

    try:
        iobj = InformationObject.objects.get(campaign=campaign, slug=slug)
    except InformationObject.DoesNotExist:
        return

    if iobj.foirequest is not None:
        return

    if iobj.publicbody != sender.public_body:
        return

    if not sender.public:
        return

    iobj.foirequest = sender
    iobj.save()

Adjust to new reference handling
Adjust to new reference handling
  from .models import Campaign, InformationObject
  
  
  def connect_info_object(sender, **kwargs):
      reference = kwargs.get('reference')
-     if reference is None:
+     if not reference:
          return
-     if 'campaign' not in reference:
+     if not reference.startswith('campaign:'):
          return
+     namespace, campaign_value = reference.split(':', 1)
      try:
-         campaign, slug = reference['campaign'].split('@', 1)
+         campaign, slug = campaign_value.split('@', 1)
      except (ValueError, IndexError):
          return
  
      try:
          campaign_pk = int(campaign)
      except ValueError:
          return
  
      try:
          campaign = Campaign.objects.get(pk=campaign_pk)
      except Campaign.DoesNotExist:
          return
  
      try:
          iobj = InformationObject.objects.get(campaign=campaign, slug=slug)
      except InformationObject.DoesNotExist:
          return
  
      if iobj.foirequest is not None:
          return
  
      if iobj.publicbody != sender.public_body:
          return
  
      if not sender.public:
          return
  
      iobj.foirequest = sender
      iobj.save()
  
Adjust to new reference handling
## Code Before:
from .models import Campaign, InformationObject


def connect_info_object(sender, **kwargs):
    reference = kwargs.get('reference')
    if reference is None:
        return
    if 'campaign' not in reference:
        return
    try:
        campaign, slug = reference['campaign'].split('@', 1)
    except (ValueError, IndexError):
        return

    try:
        campaign_pk = int(campaign)
    except ValueError:
        return

    try:
        campaign = Campaign.objects.get(pk=campaign_pk)
    except Campaign.DoesNotExist:
        return

    try:
        iobj = InformationObject.objects.get(campaign=campaign, slug=slug)
    except InformationObject.DoesNotExist:
        return

    if iobj.foirequest is not None:
        return

    if iobj.publicbody != sender.public_body:
        return

    if not sender.public:
        return

    iobj.foirequest = sender
    iobj.save()

## Instruction:
Adjust to new reference handling
## Code After:
from .models import Campaign, InformationObject


def connect_info_object(sender, **kwargs):
    reference = kwargs.get('reference')
    if not reference:
        return
    if not reference.startswith('campaign:'):
        return
    namespace, campaign_value = reference.split(':', 1)
    try:
        campaign, slug = campaign_value.split('@', 1)
    except (ValueError, IndexError):
        return

    try:
        campaign_pk = int(campaign)
    except ValueError:
        return

    try:
        campaign = Campaign.objects.get(pk=campaign_pk)
    except Campaign.DoesNotExist:
        return

    try:
        iobj = InformationObject.objects.get(campaign=campaign, slug=slug)
    except InformationObject.DoesNotExist:
        return

    if iobj.foirequest is not None:
        return

    if iobj.publicbody != sender.public_body:
        return

    if not sender.public:
        return

    iobj.foirequest = sender
    iobj.save()
