def combine_a_ptr_to_interface(request, addr_pk, ptr_pk):
    """
    When a PTR/AddressRecord have the same ip/name it's likely that they could
    become an interface attached to a system. This view (function) takes an
    AddressRecord (addr) and PTR (ptr) and using their data creates an
    StaticInterface. Finally, addr and ptr are deleted.
    """
    addr = get_object_or_404(AddressRecord, pk=addr_pk)
    ptr = get_object_or_404(PTR, pk=ptr_pk)
    is_ajax = request.POST.get('is_ajax')
    system_hostname = request.POST.get('system_hostname')
    if is_ajax and system_hostname:
        system = None
        try:
            system = System.objects.get(hostname=system_hostname)
        except:
            try:
                system_hostname = re.sub('mozilla\.[com|net|org]', '',
                                         system_hostname)
                system = System.objects.get(hostname=system_hostname)
            except:
                system = None
        if system:
            try:
                x = do_combine_a_ptr_to_interface(
                    addr, ptr, system, mac_address="00:00:00:00:00:00")
                (intr, addr_deleted, ptr_deleted) = x
            except __HOLE__, e:
                return HttpResponse(json.dumps({'success': False, 'error':
                                                e.messages[0]}))
            ret_dict = {}
            ret_dict['success'] = True
            ret_dict['hostname'] = system.hostname
            ret_dict['id'] = system.id
            return HttpResponse(json.dumps(ret_dict))
        else:
            return HttpResponse(json.dumps({'success': False, 'error':
                                            'Unable to find system'}))

    if request.method == "POST":
        form = CombineForm(request.POST)
        if form.is_valid():
            system = form.cleaned_data['system']
            try:
                x = do_combine_a_ptr_to_interface(addr, ptr, system)
                intr, addr_deleted, ptr_deleted = x
                return redirect(intr)
            except ValidationError, e:
                form.errors['__all__'] = ErrorList(e.messages)
                return render(request, 'static_intr/combine.html', {
                    'addr': addr,
                    'ptr': ptr,
                    'form': form
                })
    else:
        form = CombineForm()
        return render(request, 'static_intr/combine.html', {
            'form': form,
            'addr': addr,
            'ptr': ptr,
        })
dataset/ETHPy150Open rtucker-mozilla/mozilla_inventory/core/interface/static_intr/views.py/combine_a_ptr_to_interface