'Tests midair collisions for non-ajax submissions.'

@pytest.mark.midair
def test_edit_midair_collisions(self, is_ajax=False, translate_locale=None):

    self.client.login(username='admin', password='testpass')
    data = new_document_data()
    resp = self.client.post(reverse('wiki.create'), data)
    doc = Document.objects.get(slug=data['slug'])
    posting_url = reverse('wiki.edit', args=[doc.slug])
    resp = self.client.get(reverse('wiki.edit', args=[doc.slug]))
    page = pq(resp.content)
    rev_id1 = page.find('input[name="current_rev"]').attr('value')
    resp = self.client.get(reverse('wiki.edit', args=[doc.slug]))
    page = pq(resp.content)
    rev_id2 = page.find('input[name="current_rev"]').attr('value')
    data.update({'form-type': 'rev', 'content': 'This    edit    got    there    first', 'current_rev': rev_id2})
    if translate_locale:
        translation = document(parent=doc, locale=translate_locale, save=True)
        translation_rev = revision(document=translation, based_on=translation.parent.current_or_latest_revision(), save=True)
        rev_id1 = rev_id2 = translation_rev.id
        posting_url = reverse('wiki.edit', args=[translation_rev.document.slug], locale=translate_locale)
        data.update({'title': translation.title, 'locale': translation.locale, 'slug': translation.slug, 'current_rev': rev_id2})
    if is_ajax:
        resp = self.client.post(posting_url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        eq_(False, json.loads(resp.content)['error'])
    else:
        resp = self.client.post(posting_url, data)
        eq_(302, resp.status_code)
    data.update({'form-type': 'rev', 'content': 'This    edit    gets    mid-aired', 'current_rev': rev_id1})
    if is_ajax:
        resp = self.client.post(posting_url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    else:
        resp = self.client.post(posting_url, data)
    locale = (translate_locale if translate_locale else doc.locale)
    doc_path = (translation.slug if translate_locale else doc.slug)
    history_url = reverse('wiki.document_revisions', kwargs={'document_path': doc_path}, locale=locale)
    midair_collission_error = (unicode(MIDAIR_COLLISION) % {'url': history_url}).encode('utf-8')
    if is_ajax:
        location_of_error = json.loads(resp.content)['error_message']
    else:
        start_of_error = resp.content.index(midair_collission_error[0:20])
        end_of_error = ((start_of_error + len(midair_collission_error)) + 20)
        location_of_error = HTMLParser.HTMLParser().unescape(resp.content[start_of_error:end_of_error])
    ok_((midair_collission_error in location_of_error), 'Midair    collision    message    should    appear')
