<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def validate_location_for_story_element(sender, instance, action, reverse, pk_set, *args, **kwargs):
    '''
    Validates that location is from same outline as story node.
    '''
    if action == 'pre_add':
        if reverse:
            for spk in pk_set:
                story_node = StoryElementNode.objects.get(pk=spk)
                if instance.outline != story_node.outline:
                    raise IntegrityError(_('Location must be from same outline as story node.'))
        else:
            for lpk in pk_set:
                loc_instance = LocationInstance.objects.get(pk=lpk)
                if instance.outline != loc_instance.outline:
                    raise IntegrityError(_('Location must be from the same outline as story node.'))