Esempio n. 1
0
def addon_to_dict(addon, disco=False, src='api'):
    """
    Renders an addon in JSON for the API.
    """
    def url(u, **kwargs):
        return settings.SITE_URL + urlparams(u, **kwargs)

    v = addon.current_version

    if disco:
        learnmore = settings.SERVICES_URL + reverse('discovery.addons.detail',
                                                    args=[addon.slug])
        learnmore = urlparams(learnmore, src='discovery-personalrec')
    else:
        learnmore = url(addon.get_url_path(), src=src)

    d = {
        'id':
        addon.id,
        'name':
        unicode(addon.name) if addon.name else None,
        'guid':
        addon.guid,
        'status':
        amo.STATUS_CHOICES_API[addon.status],
        'type':
        amo.ADDON_SLUGS_UPDATE[addon.type],
        'authors': [{
            'id': a.id,
            'name': unicode(a.name),
            'link': absolutify(a.get_url_path(src=src))
        } for a in addon.listed_authors],
        'summary':
        (strip_tags(unicode(addon.summary)) if addon.summary else None),
        'description':
        strip_tags(unicode(addon.description)),
        'icon':
        addon.icon_url,
        'learnmore':
        learnmore,
        'reviews':
        url(addon.reviews_url),
        'total_dls':
        addon.total_downloads,
        'weekly_dls':
        addon.weekly_downloads,
        'adu':
        addon.average_daily_users,
        'created':
        epoch(addon.created),
        'last_updated':
        epoch(addon.last_updated),
        'homepage':
        unicode(addon.homepage) if addon.homepage else None,
        'support':
        unicode(addon.support_url) if addon.support_url else None,
    }
    if addon.is_persona():
        d['theme'] = addon.persona.theme_data

    if v:
        d['version'] = v.version
        d['platforms'] = [unicode(a.name) for a in v.supported_platforms]
        d['compatible_apps'] = [{
            unicode(amo.APP_IDS[obj.application].pretty): {
                'min': unicode(obj.min),
                'max': unicode(obj.max)
            }
        } for obj in v.compatible_apps.values()]
    if addon.eula:
        d['eula'] = unicode(addon.eula)

    if addon.developer_comments:
        d['dev_comments'] = unicode(addon.developer_comments)

    if addon.takes_contributions:
        contribution = {
            'link': url(addon.contribution_url, src=src),
            'meet_developers': url(addon.meet_the_dev_url(), src=src),
            'suggested_amount': addon.suggested_amount,
        }
        d['contribution'] = contribution

    if addon.type == amo.ADDON_PERSONA:
        d['previews'] = [addon.persona.preview_url]
    else:
        d['previews'] = [p.as_dict(src=src) for p in addon.all_previews]

    return d
Esempio n. 2
0
def addon_to_dict(addon, disco=False, src='api'):
    """
    Renders an addon into a dict for the legacy API.
    """
    def url(u, **kwargs):
        return settings.SITE_URL + urlparams(u, **kwargs)

    v = addon.current_version

    if disco:
        learnmore = settings.SERVICES_URL + reverse('discovery.addons.detail',
                                                    args=[addon.slug])
        learnmore = urlparams(learnmore, src='discovery-personalrec')
    else:
        learnmore = url(addon.get_url_path(), src=src)

    d = {
        'id':
        addon.id,
        'name':
        unicode(addon.name) if addon.name else None,
        'guid':
        addon.guid,
        'status':
        amo.STATUS_CHOICES_API[addon.status],
        'type':
        amo.ADDON_SLUGS_UPDATE[addon.type],
        'authors': [{
            'id': a.id,
            'name': unicode(a.name),
            'link': absolutify(a.get_url_path(src=src))
        } for a in addon.listed_authors],
        'summary':
        (strip_tags(unicode(addon.summary)) if addon.summary else None),
        'description':
        strip_tags(unicode(addon.description)),
        'icon':
        addon.icon_url,
        'learnmore':
        learnmore,
        'reviews':
        url(addon.ratings_url),
        'total_dls':
        addon.total_downloads,
        'weekly_dls':
        addon.weekly_downloads,
        'adu':
        addon.average_daily_users,
        'created':
        epoch(addon.created),
        'last_updated':
        epoch(addon.last_updated),
        'homepage':
        unicode(addon.homepage) if addon.homepage else None,
        'support':
        unicode(addon.support_url) if addon.support_url else None,
    }
    if addon.is_persona():
        d['theme'] = addon.persona.theme_data

    if v:
        d['version'] = v.version
        d['platforms'] = [unicode(a.name) for a in v.supported_platforms]
        d['compatible_apps'] = [{
            unicode(amo.APP_IDS[appver.application].pretty): {
                'min':
                unicode(appver.min) if appver else
                (amo.D2C_MIN_VERSIONS.get(app.id, '1.0')),
                'max':
                unicode(appver.max) if appver else amo.FAKE_MAX_VERSION,
            }
        } for app, appver in v.compatible_apps.items() if appver]
    if addon.eula:
        d['eula'] = unicode(addon.eula)

    if addon.developer_comments:
        d['dev_comments'] = unicode(addon.developer_comments)

    if addon.contributions:
        d['contribution'] = {
            'meet_developers': addon.contributions,
        }

    if addon.type == amo.ADDON_PERSONA:
        d['previews'] = [addon.persona.preview_url]
    else:

        def preview_as_dict(preview, src):
            d = {
                'full': urlparams(preview.image_url, src=src),
                'thumbnail': urlparams(preview.thumbnail_url, src=src),
                'caption': unicode(preview.caption)
            }
            return d

        d['previews'] = [preview_as_dict(p, src) for p in addon.all_previews]

    return d
Esempio n. 3
0
def addon_to_dict(addon, disco=False, src='api'):
    """
    Renders an addon in JSON for the API.
    """
    def url(u, **kwargs):
        return settings.SITE_URL + urlparams(u, **kwargs)

    v = addon.current_version

    if disco:
        learnmore = settings.SERVICES_URL + reverse('discovery.addons.detail',
                                                    args=[addon.slug])
        learnmore = urlparams(learnmore, src='discovery-personalrec')
    else:
        learnmore = url(addon.get_url_path(), src=src)

    d = {
        'id': addon.id,
        'name': unicode(addon.name) if addon.name else None,
        'guid': addon.guid,
        'status': amo.STATUS_CHOICES_API[addon.status],
        'type': amo.ADDON_SLUGS_UPDATE[addon.type],
        'authors': [{'id': a.id, 'name': unicode(a.name),
                     'link': absolutify(a.get_url_path(src=src))}
                    for a in addon.listed_authors],
        'summary': (
            strip_tags(unicode(addon.summary)) if addon.summary else None),
        'description': strip_tags(unicode(addon.description)),
        'icon': addon.icon_url,
        'learnmore': learnmore,
        'reviews': url(addon.reviews_url),
        'total_dls': addon.total_downloads,
        'weekly_dls': addon.weekly_downloads,
        'adu': addon.average_daily_users,
        'created': epoch(addon.created),
        'last_updated': epoch(addon.last_updated),
        'homepage': unicode(addon.homepage) if addon.homepage else None,
        'support': unicode(addon.support_url) if addon.support_url else None,
    }
    if addon.is_persona():
        d['theme'] = addon.persona.theme_data

    if v:
        d['version'] = v.version
        d['platforms'] = [unicode(a.name) for a in v.supported_platforms]
        d['compatible_apps'] = [
            {unicode(amo.APP_IDS[obj.application].pretty): {
                'min': unicode(obj.min), 'max': unicode(obj.max)}}
            for obj in v.compatible_apps.values()]
    if addon.eula:
        d['eula'] = unicode(addon.eula)

    if addon.developer_comments:
        d['dev_comments'] = unicode(addon.developer_comments)

    simple_contributions = waffle.switch_is_active('simple-contributions')
    if addon.takes_contributions and not simple_contributions:
        contribution = {
            'link': url(addon.contribution_url, src=src),
            'meet_developers': url(addon.meet_the_dev_url(), src=src),
            'suggested_amount': addon.suggested_amount,
        }
        d['contribution'] = contribution
    elif addon.contributions and simple_contributions:
        d['contribution'] = {
            'meet_developers': addon.contributions,
        }

    if addon.type == amo.ADDON_PERSONA:
        d['previews'] = [addon.persona.preview_url]
    else:
        d['previews'] = [p.as_dict(src=src) for p in addon.all_previews]

    return d
Esempio n. 4
0
def addon_to_dict(addon, disco=False, src='api'):
    """
    Renders an addon into a dict for the legacy API.
    """
    def url(u, **kwargs):
        return settings.SITE_URL + urlparams(u, **kwargs)

    v = addon.current_version

    if disco:
        learnmore = settings.SERVICES_URL + reverse('discovery.addons.detail',
                                                    args=[addon.slug])
        learnmore = urlparams(learnmore, src='discovery-personalrec')
    else:
        learnmore = url(addon.get_url_path(), src=src)

    d = {
        'id': addon.id,
        'name': unicode(addon.name) if addon.name else None,
        'guid': addon.guid,
        'status': amo.STATUS_CHOICES_API[addon.status],
        'type': amo.ADDON_SLUGS_UPDATE[addon.type],
        'authors': [{'id': a.id, 'name': unicode(a.name),
                     'link': absolutify(a.get_url_path(src=src))}
                    for a in addon.listed_authors],
        'summary': (
            strip_tags(unicode(addon.summary)) if addon.summary else None),
        'description': strip_tags(unicode(addon.description)),
        'icon': addon.icon_url,
        'learnmore': learnmore,
        'reviews': url(addon.ratings_url),
        'total_dls': addon.total_downloads,
        'weekly_dls': addon.weekly_downloads,
        'adu': addon.average_daily_users,
        'created': epoch(addon.created),
        'last_updated': epoch(addon.last_updated),
        'homepage': unicode(addon.homepage) if addon.homepage else None,
        'support': unicode(addon.support_url) if addon.support_url else None,
    }
    if addon.is_persona():
        d['theme'] = addon.persona.theme_data

    if v:
        d['version'] = v.version
        d['platforms'] = [unicode(a.name) for a in v.supported_platforms]
        d['compatible_apps'] = [{
            unicode(amo.APP_IDS[appver.application].pretty): {
                'min': unicode(appver.min) if appver else (
                    amo.D2C_MIN_VERSIONS.get(app.id, '1.0')),
                'max': unicode(appver.max) if appver else amo.FAKE_MAX_VERSION,
            }} for app, appver in v.compatible_apps.items() if appver]
    if addon.eula:
        d['eula'] = unicode(addon.eula)

    if addon.developer_comments:
        d['dev_comments'] = unicode(addon.developer_comments)

    if addon.contributions:
        d['contribution'] = {
            'meet_developers': addon.contributions,
        }

    if addon.type == amo.ADDON_PERSONA:
        d['previews'] = [addon.persona.preview_url]
    else:
        def preview_as_dict(preview, src):
            d = {'full': urlparams(preview.image_url, src=src),
                 'thumbnail': urlparams(preview.thumbnail_url, src=src),
                 'caption': unicode(preview.caption)}
            return d

        d['previews'] = [preview_as_dict(p, src) for p in addon.all_previews]

    return d