Пример #1
0
def document_api_data(doc=None, ensure_contributors=False, redirect_url=None):
    """
    Returns the JSON data for the document for the document API.
    """
    if doc:
        job = DocumentContributorsJob()
        # If "ensure_contributors" is True, we need the contributors since the
        # result will likely be cached, so we'll set "fetch_on_miss" and wait
        # for the result if it's not already available or stale.
        job.fetch_on_miss = ensure_contributors
        contributors = [c['username'] for c in job.get(doc.pk)]
    else:
        contributors = None

    return {
        'locale':
        doc and doc.locale,
        'slug':
        doc and doc.slug,
        'id':
        doc and doc.id,
        'title':
        doc and doc.title,
        'summary':
        doc and doc.get_summary_html(),
        'language':
        doc and doc.language,
        'absoluteURL':
        doc and doc.get_absolute_url(),
        'redirectURL':
        redirect_url,
        'editURL':
        doc and absolutify(doc.get_edit_url(), for_wiki_site=True),
        'bodyHTML':
        doc and doc.get_body_html(),
        'quickLinksHTML':
        doc and doc.get_quick_links_html(),
        'tocHTML':
        doc and doc.get_toc_html(),
        'parents':
        doc and [{
            'url': d.get_absolute_url(),
            'title': d.title
        } for d in doc.parents],
        'translations':
        doc and [{
            'language': t.language,
            'localizedLanguage': _(settings.LOCALES[t.locale].english),
            'locale': t.locale,
            'url': t.get_absolute_url(),
            'title': t.title
        } for t in doc.get_other_translations(fields=('locale', 'slug',
                                                      'title'))],
        'contributors':
        contributors,
        'lastModified': (doc and doc.current_revision
                         and doc.current_revision.created.isoformat()),
        'lastModifiedBy': (doc and doc.current_revision
                           and str(doc.current_revision.creator))
    }
Пример #2
0
def document_api_data(doc=None, ensure_contributors=False, redirect_url=None):
    """
    Returns the JSON data for the document for the document API.
    """
    if redirect_url:
        return {
            'documentData': None,
            'redirectURL': redirect_url,
        }

    job = DocumentContributorsJob()
    # If "ensure_contributors" is True, we need the contributors since the
    # result will likely be cached, so we'll set "fetch_on_miss" and wait
    # for the result if it's not already available or stale.
    job.fetch_on_miss = ensure_contributors
    contributors = [c['username'] for c in job.get(doc.pk)]

    return {
        'documentData': {
            'locale': doc.locale,
            'slug': doc.slug,
            'id': doc.id,
            'title': doc.title,
            'summary': doc.get_summary_html(),
            'language': doc.language,
            'absoluteURL': doc.get_absolute_url(),
            'editURL': absolutify(doc.get_edit_url(), for_wiki_site=True),
            'bodyHTML': doc.get_body_html(),
            'quickLinksHTML': doc.get_quick_links_html(),
            'tocHTML': doc.get_toc_html(),
            'parents': [
                {
                    'url': d.get_absolute_url(),
                    'title': d.title
                } for d in doc.parents
            ],
            'translations': [
                {
                    'language': t.language,
                    'localizedLanguage': _(settings.LOCALES[t.locale].english),
                    'locale': t.locale,
                    'url': t.get_absolute_url(),
                    'title': t.title
                } for t in doc.get_other_translations(
                    fields=('locale', 'slug', 'title'))
            ],
            'contributors': contributors,
            'lastModified': (doc.current_revision and
                             doc.current_revision.created.isoformat()),
            'lastModifiedBy': (doc.current_revision and
                               str(doc.current_revision.creator))
        },
        'redirectURL': None,
    }
Пример #3
0
def invalidate_document_contribution(user):
    """
    Invalidate the contributor list for Documents the user has edited.

    This will remove them if they have been banned, and add them if they
    have been unbanned.
    """
    revisions = user.created_revisions
    doc_ids = set(revisions.values_list('document_id', flat=True))
    job = DocumentContributorsJob()
    for doc_id in doc_ids:
        job.invalidate(doc_id)
Пример #4
0
def invalidate_document_contribution(user):
    """
    Invalidate the contributor list for Documents the user has edited.

    This will remove them if they have been banned, and add them if they
    have been unbanned.
    """
    revisions = user.created_revisions
    doc_ids = set(revisions.values_list('document_id', flat=True))
    job = DocumentContributorsJob()
    for doc_id in doc_ids:
        job.invalidate(doc_id)
Пример #5
0
def test_doc_api_for_redirect_to_doc(client, api_settings, root_doc,
                                     redirect_doc, cleared_cacheback_cache,
                                     ensure_contributors):
    """
    Test the document API when we're requesting data for a document that
    redirects to another document.
    """
    if ensure_contributors:
        # Pre-populate the cache for the call to document_api_data()
        # made within the view that serves the "api.v1.doc" endpoint.
        DocumentContributorsJob().refresh(root_doc.pk)

    url = reverse('api.v1.doc', args=[redirect_doc.locale, redirect_doc.slug])
    response = client.get(url, HTTP_HOST=api_settings.BETA_HOST, follow=True)
    assert response.status_code == 200
    assert_no_cache_header(response)

    data = response.json()
    assert data['documentData']
    assert data['redirectURL'] is None
    doc_data = data['documentData']
    assert doc_data['locale'] == root_doc.locale
    assert doc_data['slug'] == root_doc.slug
    assert doc_data['id'] == root_doc.id
    assert doc_data['title'] == root_doc.title
    assert doc_data['language'] == root_doc.language
    assert doc_data['hrefLang'] == 'en'
    assert doc_data['absoluteURL'] == root_doc.get_absolute_url()
    assert doc_data['editURL'] == absolutify(root_doc.get_edit_url(),
                                             for_wiki_site=True)
    assert doc_data['translateURL'] == absolutify(reverse(
        'wiki.select_locale',
        args=(root_doc.slug, ),
        locale=root_doc.locale,
    ),
                                                  for_wiki_site=True)
    assert doc_data['bodyHTML'] == root_doc.get_body_html()
    assert doc_data['quickLinksHTML'] == root_doc.get_quick_links_html()
    assert doc_data['tocHTML'] == root_doc.get_toc_html()
    assert doc_data['translations'] == []
    assert doc_data['contributors'] == (['wiki_user']
                                        if ensure_contributors else [])
    assert doc_data['lastModified'] == '2017-04-14T12:15:00'
    assert doc_data['lastModifiedBy'] == 'wiki_user'

    # Clear the cache for a clean slate when calling document_api_data().
    DocumentContributorsJob().delete(root_doc.pk)

    # Also ensure that we get exactly the same data by calling
    # the document_api_data() function directly
    assert data == document_api_data(root_doc,
                                     ensure_contributors=ensure_contributors)
Пример #6
0
def test_doc_api(client, api_settings, trans_doc, cleared_cacheback_cache,
                 ensure_contributors):
    """On success we get document details in a JSON response."""
    if ensure_contributors:
        # Pre-populate the cache for the call to document_api_data()
        # made within the view that serves the "api.v1.doc" endpoint.
        DocumentContributorsJob().refresh(trans_doc.pk)

    url = reverse('api.v1.doc', args=[trans_doc.locale, trans_doc.slug])
    response = client.get(url, HTTP_HOST=api_settings.BETA_HOST)
    assert response.status_code == 200
    assert_no_cache_header(response)

    data = response.json()
    assert data['documentData']
    assert data['redirectURL'] is None
    doc_data = data['documentData']
    assert doc_data['locale'] == trans_doc.locale
    assert doc_data['slug'] == trans_doc.slug
    assert doc_data['id'] == trans_doc.id
    assert doc_data['title'] == trans_doc.title
    assert doc_data['language'] == trans_doc.language
    assert doc_data['hrefLang'] == 'fr'
    assert doc_data['absoluteURL'] == trans_doc.get_absolute_url()
    assert doc_data['editURL'] == absolutify(trans_doc.get_edit_url(),
                                             for_wiki_site=True)
    assert doc_data['translateURL'] is None
    assert doc_data['bodyHTML'] == trans_doc.get_body_html()
    assert doc_data['quickLinksHTML'] == trans_doc.get_quick_links_html()
    assert doc_data['tocHTML'] == trans_doc.get_toc_html()
    assert doc_data['translations'] == [{
        'locale': 'en-US',
        'language': 'English (US)',
        'hrefLang': 'en',
        'localizedLanguage': u'Anglais am\u00e9ricain',
        'title': 'Root Document',
        'url': '/en-US/docs/Root'
    }]
    assert doc_data['contributors'] == (['wiki_user']
                                        if ensure_contributors else [])
    assert doc_data['lastModified'] == '2017-04-14T12:20:00'
    assert doc_data['lastModifiedBy'] == 'wiki_user'

    # Clear the cache for a clean slate when calling document_api_data().
    DocumentContributorsJob().delete(trans_doc.pk)

    # Also ensure that we get exactly the same data by calling
    # the document_api_data() function directly
    assert data == document_api_data(trans_doc,
                                     ensure_contributors=ensure_contributors)
Пример #7
0
def document_api_data(doc=None, ensure_contributors=False, redirect_url=None):
    """
    Returns the JSON data for the document for the document API.
    """
    if redirect_url:
        return {
            'documentData': None,
            'redirectURL': redirect_url,
        }

    job = DocumentContributorsJob()
    # If "ensure_contributors" is True, we need the contributors since the
    # result will likely be cached, so we'll set "fetch_on_miss" and wait
    # for the result if it's not already available or stale.
    job.fetch_on_miss = ensure_contributors
    contributors = [c['username'] for c in job.get(doc.pk)]

    # The original english slug for this document, for google analytics
    if doc.locale == 'en-US':
        en_slug = doc.slug
    elif doc.parent_id and doc.parent.locale == 'en-US':
        en_slug = doc.parent.slug
    else:
        en_slug = ''

    other_translations = doc.get_other_translations(fields=('locale', 'slug',
                                                            'title'))
    available_locales = (set([doc.locale]) | set(t.locale
                                                 for t in other_translations))

    return {
        'documentData': {
            'locale':
            doc.locale,
            'slug':
            doc.slug,
            'enSlug':
            en_slug,
            'id':
            doc.id,
            'title':
            doc.title,
            'summary':
            doc.get_summary_html(),
            'language':
            doc.language,
            'hrefLang':
            doc.get_hreflang(available_locales),
            'absoluteURL':
            doc.get_absolute_url(),
            'editURL':
            absolutify(doc.get_edit_url(), for_wiki_site=True),
            'translateURL':
            (absolutify(reverse(
                'wiki.select_locale',
                args=(doc.slug, ),
                locale=doc.locale,
            ),
                        for_wiki_site=True) if doc.is_localizable else None),
            'bodyHTML':
            doc.get_body_html(),
            'quickLinksHTML':
            doc.get_quick_links_html(),
            'tocHTML':
            doc.get_toc_html(),
            'parents': [{
                'url': d.get_absolute_url(),
                'title': d.title
            } for d in doc.parents],
            'translations': [{
                'language':
                t.language,
                'hrefLang':
                t.get_hreflang(available_locales),
                'localizedLanguage':
                _(settings.LOCALES[t.locale].english),
                'locale':
                t.locale,
                'url':
                t.get_absolute_url(),
                'title':
                t.title
            } for t in other_translations],
            'contributors':
            contributors,
            'lastModified': (doc.current_revision
                             and doc.current_revision.created.isoformat()),
            'lastModifiedBy': (doc.current_revision
                               and str(doc.current_revision.creator))
        },
        'redirectURL': None,
    }