<commit_before>import json

from django.conf import settings

import caching.base as caching
import jinja2
import waffle
from jingo import env, register
from jingo_minify import helpers as jingo_minify_helpers
from tower import ugettext as _

import amo
from amo.helpers import urlparams
from amo.urlresolvers import get_outgoing_url, reverse
from amo.utils import JSONEncoder
from translations.helpers import truncate
from versions.compare import version_int as vint

import mkt


@jinja2.contextfunction
@register.function
def css(context, bundle, media=False, debug=None):
    if debug is None:
        debug = settings.TEMPLATE_DEBUG

    # ?debug=true gives you unminified CSS for testing on -dev/prod.
    if context['request'].GET.get('debug'):
        debug = True

    return jingo_minify_helpers.css(bundle, media, debug)


@jinja2.contextfunction
@register.function
def js(context, bundle, debug=None, defer=False, async=False):
    if debug is None:
        debug = settings.TEMPLATE_DEBUG

    # ?debug=true gives you unminified JS for testing on -dev/prod.
    if context['request'].GET.get('debug'):
        debug = True

    return jingo_minify_helpers.js(bundle, debug, defer, async)


@jinja2.contextfunction
@register.function
def get_media_hash(context):
    return jingo_minify_helpers.BUILD_ID_JS + jingo_minify_helpers.BUILD_ID_CSS


def new_context(context, **kw):
    c = dict(context.items())
    c.update(kw)
    return c


@register.function
def no_results():
    # This prints a "No results found" message. That's all. Carry on.
    t = env.get_template('site/helpers/no_results.html').render()
    return jinja2.Markup(t)


@jinja2.contextfunction
@register.function
def market_button(context, product, receipt_type=None, classes=None):
    request = context['request']
    if product.is_webapp():
        purchased = False
        classes = (classes or []) + ['button', 'product']
        reviewer = receipt_type == 'reviewer'
        data_attrs = {'manifest_url': product.get_manifest_url(reviewer),
                      'is_packaged': json.dumps(product.is_packaged)}

        installed = None

        if request.amo_user:
            installed_set = request.amo_user.installed_set
            installed = installed_set.filter(addon=product).exists()

        # Handle premium apps.
        if product.has_price():
            # User has purchased app.
            purchased = (request.amo_user and
                         product.pk in request.amo_user.purchase_ids())

            # App authors are able to install their apps free of charge.
            if (not purchased and
                    request.check_ownership(product, require_author=True)):
                purchased = True

        if installed or purchased or not product.has_price():
            label = _('Install')
        else:
            label = product.get_price(request.REGION.default_currency)

        # Free apps and purchased apps get active install buttons.
        if not product.is_premium() or purchased:
            classes.append('install')

        c = dict(product=product, label=label, purchased=purchased,
                 data_attrs=data_attrs, classes=' '.join(classes))
        t = env.get_template('site/helpers/webapp_button.html')
    return jinja2.Markup(t.render(c))


def product_as_dict(request, product, purchased=None, receipt_type=None,
                    src=''):
    # Dev environments might not have authors set.
    author = ''
    author_url = ''
    if product.listed_authors:
        author = product.listed_authors[0].name
        author_url = product.listed_authors[0].get_url_path()

    url = (reverse('receipt.issue', args=[product.app_slug])
           if receipt_type else product.get_detail_url('record'))
    src = src or request.GET.get('src', '')
    reviewer = receipt_type == 'reviewer'

    ret = {
        'id': product.id,
        'name': product.name,
        'categories': [unicode(cat.name) for cat in
                       product.categories.all()],
        'manifest_url': product.get_manifest_url(reviewer),
        'recordUrl': urlparams(url, src=src),
        'author': author,
        'author_url': author_url,
        'iconUrl': product.get_icon_url(64),
        'is_packaged': product.is_packaged,
        'src': src
    }<commit_msg>use id instead of name for categories in app install for linux/nightly (bug 876293)<commit_after>import json

from django.conf import settings

import caching.base as caching
import jinja2
import waffle
from jingo import env, register
from jingo_minify import helpers as jingo_minify_helpers
from tower import ugettext as _

import amo
from amo.helpers import urlparams
from amo.urlresolvers import get_outgoing_url, reverse
from amo.utils import JSONEncoder
from translations.helpers import truncate
from versions.compare import version_int as vint

import mkt


@jinja2.contextfunction
@register.function
def css(context, bundle, media=False, debug=None):
    if debug is None:
        debug = settings.TEMPLATE_DEBUG

    # ?debug=true gives you unminified CSS for testing on -dev/prod.
    if context['request'].GET.get('debug'):
        debug = True

    return jingo_minify_helpers.css(bundle, media, debug)


@jinja2.contextfunction
@register.function
def js(context, bundle, debug=None, defer=False, async=False):
    if debug is None:
        debug = settings.TEMPLATE_DEBUG

    # ?debug=true gives you unminified JS for testing on -dev/prod.
    if context['request'].GET.get('debug'):
        debug = True

    return jingo_minify_helpers.js(bundle, debug, defer, async)


@jinja2.contextfunction
@register.function
def get_media_hash(context):
    return jingo_minify_helpers.BUILD_ID_JS + jingo_minify_helpers.BUILD_ID_CSS


def new_context(context, **kw):
    c = dict(context.items())
    c.update(kw)
    return c


@register.function
def no_results():
    # This prints a "No results found" message. That's all. Carry on.
    t = env.get_template('site/helpers/no_results.html').render()
    return jinja2.Markup(t)


@jinja2.contextfunction
@register.function
def market_button(context, product, receipt_type=None, classes=None):
    request = context['request']
    if product.is_webapp():
        purchased = False
        classes = (classes or []) + ['button', 'product']
        reviewer = receipt_type == 'reviewer'
        data_attrs = {'manifest_url': product.get_manifest_url(reviewer),
                      'is_packaged': json.dumps(product.is_packaged)}

        installed = None

        if request.amo_user:
            installed_set = request.amo_user.installed_set
            installed = installed_set.filter(addon=product).exists()

        # Handle premium apps.
        if product.has_price():
            # User has purchased app.
            purchased = (request.amo_user and
                         product.pk in request.amo_user.purchase_ids())

            # App authors are able to install their apps free of charge.
            if (not purchased and
                    request.check_ownership(product, require_author=True)):
                purchased = True

        if installed or purchased or not product.has_price():
            label = _('Install')
        else:
            label = product.get_price(request.REGION.default_currency)

        # Free apps and purchased apps get active install buttons.
        if not product.is_premium() or purchased:
            classes.append('install')

        c = dict(product=product, label=label, purchased=purchased,
                 data_attrs=data_attrs, classes=' '.join(classes))
        t = env.get_template('site/helpers/webapp_button.html')
    return jinja2.Markup(t.render(c))


def product_as_dict(request, product, purchased=None, receipt_type=None,
                    src=''):
    # Dev environments might not have authors set.
    author = ''
    author_url = ''
    if product.listed_authors:
        author = product.listed_authors[0].name
        author_url = product.listed_authors[0].get_url_path()

    url = (reverse('receipt.issue', args=[product.app_slug])
           if receipt_type else product.get_detail_url('record'))
    src = src or request.GET.get('src', '')
    reviewer = receipt_type == 'reviewer'

    ret = {
        'id': product.id,
        'name': product.name,
        'categories': [unicode(cat.id) for cat in
                       product.categories.all()],
        'manifest_url': product.get_manifest_url(reviewer),
        'recordUrl': urlparams(url, src=src),
        'author': author,
        'author_url': author_url,
        'iconUrl': product.get_icon_url(64),
        'is_packaged': product.is_packaged,
        'src': src
    }
{'fraction_non_alphanumeric': 0.09988479262672811, 'fraction_numerical': 0.0032258064516129032, 'mean_word_length': 2.9191873589164787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5152232', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2512, 'n_words': 651}