<commit_before>import jingo
import jinja2
from l10n import ugettext as _, ugettext_lazy as _lazy

import amo
from amo.helpers import urlparams
from addons.models import Addon


@jinja2.contextfunction
def install_button(context, addon, version=None, show_eula=True,
                   show_contrib=True, show_warning=True):
    """If version isn't given, we use the latest version."""
    app, lang = context['APP'], context['LANG']
    show_eula = bool(context['request'].GET.get('eula', show_eula))
    button = install_button_factory(addon, app, lang, version,
                                    show_eula, show_contrib)
    c = {'button': button, 'addon': addon, 'version': button.version,
         'APP': app}
    t = jingo.env.get_template('addons/button.html').render(c)
    return jinja2.Markup(t)


def install_button_factory(*args, **kwargs):
    button = InstallButton(*args, **kwargs)
    # Order matters.  We want to highlight unreviewed before featured.  They
    # should be mutually exclusive, but you never know.
    classes = (('unreviewed', UnreviewedInstallButton),
               ('self_hosted', SelfHostedInstallButton),
               ('featured', FeaturedInstallButton))
    for pred, cls in classes:
        if getattr(button, pred, False):
            button.__class__ = cls
            break
    button.prepare()
    return button


class InstallButton(object):
    button_class = ['download']
    install_class = []
    install_text = ''

    def __init__(self, addon, app, lang, version=None, show_eula=True,
                 show_contrib=True, show_warning=True):
        self.addon, self.app, self.lang = addon, app, lang
        self.latest = version is None
        self.version = version or addon.current_version

        self.featured = addon.is_featured(app, lang)
        self.unreviewed = addon.is_unreviewed() or self.version.is_unreviewed
        self.self_hosted = addon.status == amo.STATUS_LISTED

        self.show_eula = show_eula and addon.has_eula
        self.show_contrib = (show_contrib and addon.takes_contributions
                             and addon.annoying == amo.CONTRIB_ROADBLOCK)
        self.show_warning = show_warning and (self.unreviewed or
                                              self.self_hosted)

    def prepare(self):
        """Called after the class is set to manage eulas, contributions."""
        # Get a copy for this instance.
        self.button_class = list(self.__class__.button_class)
        self.install_class = list(self.__class__.install_class)
        tests = (self.show_eula, 'eula'), (self.show_contrib, 'contrib')
        for pred, cls in tests:
            if bool(pred):
                try:
                    self.button_class.remove('download')
                except ValueError:
                    pass
                self.button_class.append(cls)
                self.install_class.append(cls)

    def attrs(self):
        rv = {}
        addon = self.addon
        if addon.takes_contributions and addon.annoying == amo.CONTRIB_AFTER:
            rv['data-after'] = 'contrib'
        if addon.type_id == amo.ADDON_SEARCH:
            rv['data-search'] = 'true'
        return rv

    def links(self):
        rv = []
        for file in self.version.files.all():
            platform = file.platform_id
            # TODO(jbalogh): this is wrong if we got a version argument.
            url = file.latest_xpi_url()
            if platform == amo.PLATFORM_ALL.id:
                text, os = _('Download Now'), None
            else:
                text, os = _('Download'), amo.PLATFORMS[platform]
            if self.show_eula:
                text, url = _('Continue to Download &rarr;'), file.eula_url()
            elif self.show_contrib:
                # The eula doesn't exist or has been hit already.
                text = _('Continue to Download &rarr;')
                roadblock = self.addon.meet_the_dev_url(extra='roadblock')
                url = urlparams(roadblock, eula='')
            rv.append(Link(text, url, os, file))
        return rv


class FeaturedInstallButton(InstallButton):
    install_class = ['featuredaddon']
    install_text = _lazy(u'Featured', 'install_button')
<commit_msg>propagate src and collection references<commit_after>import jingo
import jinja2
from l10n import ugettext as _, ugettext_lazy as _lazy

import amo
from amo.helpers import urlparams
from addons.models import Addon


@jinja2.contextfunction
def install_button(context, addon, version=None, show_eula=True,
                   show_contrib=True, show_warning=True, src='',
                   collection=None):
    """If version isn't given, we use the latest version."""
    request = context['request']
    app, lang = context['APP'], context['LANG']
    show_eula = bool(request.GET.get('eula', show_eula))
    src = src or context.get('src') or request.GET.get('src', '')
    collection = (collection or context.get('collection')
                  or request.GET.get('collection')
                  or request.GET.get('collection_id')
                  or request.GET.get('collection_uuid'))
    button = install_button_factory(addon, app, lang, version,
                                    show_eula, show_contrib, show_warning,
                                    src, collection)
    c = {'button': button, 'addon': addon, 'version': button.version,
         'APP': app}
    t = jingo.env.get_template('addons/button.html').render(c)
    return jinja2.Markup(t)


def install_button_factory(*args, **kwargs):
    button = InstallButton(*args, **kwargs)
    # Order matters.  We want to highlight unreviewed before featured.  They
    # should be mutually exclusive, but you never know.
    classes = (('unreviewed', UnreviewedInstallButton),
               ('self_hosted', SelfHostedInstallButton),
               ('featured', FeaturedInstallButton))
    for pred, cls in classes:
        if getattr(button, pred, False):
            button.__class__ = cls
            break
    button.prepare()
    return button


class InstallButton(object):
    button_class = ['download']
    install_class = []
    install_text = ''

    def __init__(self, addon, app, lang, version=None, show_eula=True,
                 show_contrib=True, show_warning=True, src='', collection=None):
        self.addon, self.app, self.lang = addon, app, lang
        self.latest = version is None
        self.version = version or addon.current_version
        self.src = src
        self.collection = collection

        self.featured = addon.is_featured(app, lang)
        self.unreviewed = addon.is_unreviewed() or self.version.is_unreviewed
        self.self_hosted = addon.status == amo.STATUS_LISTED

        self.show_eula = show_eula and addon.has_eula
        self.show_contrib = (show_contrib and addon.takes_contributions
                             and addon.annoying == amo.CONTRIB_ROADBLOCK)
        self.show_warning = show_warning and (self.unreviewed or
                                              self.self_hosted)

    def prepare(self):
        """Called after the class is set to manage eulas, contributions."""
        # Get a copy for this instance.
        self.button_class = list(self.__class__.button_class)
        self.install_class = list(self.__class__.install_class)
        tests = (self.show_eula, 'eula'), (self.show_contrib, 'contrib')
        for pred, cls in tests:
            if bool(pred):
                try:
                    self.button_class.remove('download')
                except ValueError:
                    pass
                self.button_class.append(cls)
                self.install_class.append(cls)

    def attrs(self):
        rv = {}
        addon = self.addon
        if addon.takes_contributions and addon.annoying == amo.CONTRIB_AFTER:
            rv['data-after'] = 'contrib'
        if addon.type_id == amo.ADDON_SEARCH:
            rv['data-search'] = 'true'
        return rv

    def links(self):
        rv = []
        for file in self.version.files.all():
            platform = file.platform_id
            # TODO(jbalogh): this is wrong if we got a version argument.
            url = file.latest_xpi_url()
            if platform == amo.PLATFORM_ALL.id:
                text, os = _('Download Now'), None
            else:
                text, os = _('Download'), amo.PLATFORMS[platform]
            if self.show_eula:
                text, url = _('Continue to Download &rarr;'), file.eula_url()
            elif self.show_contrib:
                # The eula doesn't exist or has been hit already.
                text = _('Continue to Download &rarr;')
                roadblock = self.addon.meet_the_dev_url(extra='roadblock')
                url = urlparams(roadblock, eula='')
            rv.append(Link(text, self.fix_link(url), os, file))
        return rv

    def fix_link(self, url):
        if self.src:
            url = urlparams(url, src=self.src)
        if self.collection:
            url = urlparams(url, collection=self.collection)
        return url


class FeaturedInstallButton(InstallButton):
    install_class = ['featuredaddon']
    install_text = _lazy(u'Featured', 'install_button')

{'fraction_non_alphanumeric': 0.10314438037210315, 'fraction_numerical': 0.001088020890001088, 'mean_word_length': 2.211740041928721, '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': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181126', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2617, 'n_words': 658}