コード例 #1
0
ファイル: utils.py プロジェクト: almosteverywhere/zamboni
def app_to_dict(app, region=None, profile=None, request=None):
    """Return app data as dict for API."""
    # Sad circular import issues.
    from mkt.api.resources import AppResource
    from mkt.developers.api import AccountResource
    from mkt.developers.models import AddonPaymentAccount
    from mkt.submit.api import PreviewResource
    from mkt.webapps.models import reverse_version

    supported_locales = getattr(app.current_version, 'supported_locales', '')

    data = {
        'app_type': app.app_type,
        'author': app.developer_name,
        'categories': list(app.categories.values_list('slug', flat=True)),
        'content_ratings': dict([(cr.get_body().name, {
            'name': cr.get_rating().name,
            'description': unicode(cr.get_rating().description),
        }) for cr in app.content_ratings.all()]) or None,
        'created': app.created,
        'current_version': (app.current_version.version if
                            getattr(app, 'current_version') else None),
        'default_locale': app.default_locale,
        'icons': dict([(icon_size,
                        app.get_icon_url(icon_size))
                       for icon_size in (16, 48, 64, 128)]),
        'is_packaged': app.is_packaged,
        'manifest_url': app.get_manifest_url(),
        'payment_required': False,
        'previews': PreviewResource().dehydrate_objects(app.previews.all()),
        'premium_type': amo.ADDON_PREMIUM_API[app.premium_type],
        'public_stats': app.public_stats,
        'price': None,
        'price_locale': None,
        'ratings': {'average': app.average_rating,
                    'count': app.total_reviews},
        'regions': RegionResource().dehydrate_objects(app.get_regions()),
        'slug': app.app_slug,
        'supported_locales': (supported_locales.split(',') if supported_locales
                              else []),
        'weekly_downloads': app.weekly_downloads if app.public_stats else None,
        'versions': dict((v.version, reverse_version(v)) for
                         v in app.versions.all())
    }

    data['upsell'] = False
    if app.upsell and region in app.upsell.premium.get_price_region_ids():
        upsell = app.upsell.premium
        data['upsell'] = {
            'id': upsell.id,
            'app_slug': upsell.app_slug,
            'icon_url': upsell.get_icon_url(128),
            'name': unicode(upsell.name),
            'resource_uri': AppResource().get_resource_uri(upsell),
        }

    if app.premium:
        q = AddonPaymentAccount.objects.filter(addon=app)
        if len(q) > 0 and q[0].payment_account:
            data['payment_account'] = AccountResource().get_resource_uri(
                q[0].payment_account)

        if (region in app.get_price_region_ids() or
            payments_enabled(request)):
            data['price'] = app.get_price(region=region)
            data['price_locale'] = app.get_price_locale(region=region)
        data['payment_required'] = (bool(app.get_tier().price)
                                    if app.get_tier() else False)

    with no_translation():
        data['device_types'] = [n.api_name
                                for n in app.device_types]
    if profile:
        data['user'] = {
            'developed': app.addonuser_set.filter(
                user=profile, role=amo.AUTHOR_ROLE_OWNER).exists(),
            'installed': app.has_installed(profile),
            'purchased': app.pk in profile.purchase_ids(),
        }

    return data
コード例 #2
0
ファイル: utils.py プロジェクト: sunbiz/zamboni
def app_to_dict(app, region=None, profile=None, request=None):
    """Return app data as dict for API."""
    # Sad circular import issues.
    from mkt.api.resources import AppResource
    from mkt.developers.api import AccountResource
    from mkt.developers.models import AddonPaymentAccount
    from mkt.submit.api import PreviewResource
    from mkt.webapps.models import reverse_version

    supported_locales = getattr(app.current_version, 'supported_locales', '')

    data = {
        'app_type':
        app.app_type,
        'author':
        app.developer_name,
        'categories':
        list(app.categories.values_list('slug', flat=True)),
        'content_ratings':
        dict([(cr.get_body().name, {
            'name': cr.get_rating().name,
            'description': unicode(cr.get_rating().description),
        }) for cr in app.content_ratings.all()]) or None,
        'created':
        app.created,
        'current_version': (app.current_version.version if getattr(
            app, 'current_version') else None),
        'default_locale':
        app.default_locale,
        'icons':
        dict([(icon_size, app.get_icon_url(icon_size))
              for icon_size in (16, 48, 64, 128)]),
        'is_packaged':
        app.is_packaged,
        'manifest_url':
        app.get_manifest_url(),
        'payment_required':
        False,
        'previews':
        PreviewResource().dehydrate_objects(app.previews.all()),
        'premium_type':
        amo.ADDON_PREMIUM_API[app.premium_type],
        'public_stats':
        app.public_stats,
        'price':
        None,
        'price_locale':
        None,
        'ratings': {
            'average': app.average_rating,
            'count': app.total_reviews
        },
        'regions':
        RegionResource().dehydrate_objects(app.get_regions()),
        'slug':
        app.app_slug,
        'supported_locales':
        (supported_locales.split(',') if supported_locales else []),
        'weekly_downloads':
        app.weekly_downloads if app.public_stats else None,
        'versions':
        dict((v.version, reverse_version(v)) for v in app.versions.all())
    }

    data['upsell'] = False
    if app.upsell and region in app.upsell.premium.get_price_region_ids():
        upsell = app.upsell.premium
        data['upsell'] = {
            'id': upsell.id,
            'app_slug': upsell.app_slug,
            'icon_url': upsell.get_icon_url(128),
            'name': unicode(upsell.name),
            'resource_uri': AppResource().get_resource_uri(upsell),
        }

    if app.premium:
        q = AddonPaymentAccount.objects.filter(addon=app)
        if len(q) > 0 and q[0].payment_account:
            data['payment_account'] = AccountResource().get_resource_uri(
                q[0].payment_account)

        if (region in app.get_price_region_ids() or payments_enabled(request)):
            data['price'] = app.get_price(region=region)
            data['price_locale'] = app.get_price_locale(region=region)
        data['payment_required'] = (bool(app.get_tier().price)
                                    if app.get_tier() else False)

    with no_translation():
        data['device_types'] = [n.api_name for n in app.device_types]
    if profile:
        data['user'] = {
            'developed':
            app.addonuser_set.filter(user=profile,
                                     role=amo.AUTHOR_ROLE_OWNER).exists(),
            'installed':
            app.has_installed(profile),
            'purchased':
            app.pk in profile.purchase_ids(),
        }

    return data
コード例 #3
0
ファイル: utils.py プロジェクト: kmaglione/zamboni
def app_to_dict(app, region=None, profile=None, request=None):
    """Return app data as dict for API."""
    # Sad circular import issues.
    from mkt.api.resources import AppResource
    from mkt.developers.api import AccountResource
    from mkt.developers.models import AddonPaymentAccount
    from mkt.submit.api import PreviewResource
    from mkt.webapps.models import reverse_version

    supported_locales = getattr(app.current_version, "supported_locales", "")

    data = {
        "app_type": app.app_type,
        "author": app.developer_name,
        "categories": list(app.categories.values_list("slug", flat=True)),
        "content_ratings": dict(
            [
                (
                    cr.get_body().name,
                    {"name": cr.get_rating().name, "description": unicode(cr.get_rating().description)},
                )
                for cr in app.content_ratings.all()
            ]
        )
        or None,
        "created": app.created,
        "current_version": (app.current_version.version if getattr(app, "current_version") else None),
        "default_locale": app.default_locale,
        "image_assets": dict([(ia.slug, (ia.image_url, ia.hue)) for ia in app.image_assets.all()]),
        "icons": dict([(icon_size, app.get_icon_url(icon_size)) for icon_size in (16, 48, 64, 128)]),
        "is_packaged": app.is_packaged,
        "manifest_url": app.get_manifest_url(),
        "payment_required": False,
        "previews": PreviewResource().dehydrate_objects(app.previews.all()),
        "premium_type": amo.ADDON_PREMIUM_API[app.premium_type],
        "public_stats": app.public_stats,
        "price": None,
        "price_locale": None,
        "ratings": {"average": app.average_rating, "count": app.total_reviews},
        "regions": RegionResource().dehydrate_objects(app.get_regions()),
        "slug": app.app_slug,
        "supported_locales": (supported_locales.split(",") if supported_locales else []),
        "weekly_downloads": app.weekly_downloads if app.public_stats else None,
        "versions": dict((v.version, reverse_version(v)) for v in app.versions.all()),
    }

    data["upsell"] = False
    if app.upsell and region in settings.PURCHASE_ENABLED_REGIONS:
        upsell = app.upsell.premium
        data["upsell"] = {
            "id": upsell.id,
            "app_slug": upsell.app_slug,
            "icon_url": upsell.get_icon_url(128),
            "name": unicode(upsell.name),
            "resource_uri": AppResource().get_resource_uri(upsell),
        }

    if app.premium:
        q = AddonPaymentAccount.objects.filter(addon=app)
        if len(q) > 0 and q[0].payment_account:
            data["payment_account"] = AccountResource().get_resource_uri(q[0].payment_account)

        if region in settings.PURCHASE_ENABLED_REGIONS or (
            request and waffle.flag_is_active(request, "allow-paid-app-search")
        ):
            data["price"] = app.get_price(region=region)
            data["price_locale"] = app.get_price_locale(region=region)
        data["payment_required"] = bool(app.get_tier().price) if app.get_tier() else False

    with no_translation():
        data["device_types"] = [n.api_name for n in app.device_types]
    if profile:
        data["user"] = {
            "developed": app.addonuser_set.filter(user=profile, role=amo.AUTHOR_ROLE_OWNER).exists(),
            "installed": app.has_installed(profile),
            "purchased": app.pk in profile.purchase_ids(),
        }

    return data
コード例 #4
0
ファイル: api.py プロジェクト: aricha/zamboni
 def get_versions(self, app):
     return dict((v.version, reverse_version(v)) for
                 v in app.versions.all())
コード例 #5
0
 def get_versions(self, app):
     return dict(
         (v.version, reverse_version(v)) for v in app.versions.all())