def match_client(self, client): from snippets.base.models import ( CHANNELS, CLIENT_NAMES, STARTPAGE_VERSIONS) filters = {} # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". client_channel = first(CHANNELS, client.channel.startswith) if client_channel: filters.update(**{'on_{0}'.format(client_channel): True}) # Same matching for the startpage version. startpage_version = first(STARTPAGE_VERSIONS, client.startpage_version.startswith) if startpage_version: filters.update( **{'on_startpage_{0}'.format(startpage_version): True}) # Only filter the client name here if it matches our whitelist. client_name = CLIENT_NAMES.get(client.name, False) if client_name: filters.update(**{'on_{0}'.format(client_name): True}) # Only filter by locale if they pass a valid locale. locales = filter(client.locale.lower().startswith, LANGUAGE_VALUES) if locales: filters.update(locale_set__locale__in=locales) else: # If the locale is invalid, only match snippets with no # locales specified. filters.update(locale_set__isnull=True) return self.filter(**filters).distinct()
def match_client(self, client): from snippets.base.models import ( CHANNELS, FENNEC_STARTPAGE_VERSIONS, FIREFOX_STARTPAGE_VERSIONS, JSONSnippet, ClientMatchRule) filters = {} # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". if client.channel == 'default': client_channel = 'nightly' elif client.channel == 'esr': client_channel = 'release' else: client_channel = first(CHANNELS, client.channel.startswith) if client_channel: filters.update(**{'on_{0}'.format(client_channel): True}) # Same matching for the startpage version. STARTPAGE_VERSIONS = FIREFOX_STARTPAGE_VERSIONS if client.name.lower() == 'fennec': STARTPAGE_VERSIONS = FENNEC_STARTPAGE_VERSIONS startpage_version = first(STARTPAGE_VERSIONS, client.startpage_version.startswith) if startpage_version: filters.update( **{'on_startpage_{0}'.format(startpage_version): True}) # Only filter by locale if they pass a valid locale. locales = filter(client.locale.lower().startswith, LANGUAGE_VALUES) if locales: filters.update(locales__code__in=locales) else: # If the locale is invalid, only match snippets with no # locales specified. filters.update(locales__isnull=True) snippets = self.filter(**filters).distinct() if issubclass(self.model, JSONSnippet): filtering = {'jsonsnippet__in': snippets} else: filtering = {'snippet__in': snippets} # Filter based on ClientMatchRules passed_rules, failed_rules = (ClientMatchRule.cached_objects .filter(**filtering) .distinct() .evaluate(client)) return snippets.exclude(client_match_rules__in=failed_rules)
def match_client(self, client): from snippets.base.models import (CHANNELS, FENNEC_STARTPAGE_VERSIONS, FIREFOX_STARTPAGE_VERSIONS, JSONSnippet, ClientMatchRule) filters = {} # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". if client.channel == 'default': client_channel = 'nightly' elif client.channel == 'esr': client_channel = 'release' else: client_channel = first(CHANNELS, client.channel.startswith) if client_channel: filters.update(**{'on_{0}'.format(client_channel): True}) # Same matching for the startpage version. STARTPAGE_VERSIONS = FIREFOX_STARTPAGE_VERSIONS if client.name.lower() == 'fennec': STARTPAGE_VERSIONS = FENNEC_STARTPAGE_VERSIONS startpage_version = first(STARTPAGE_VERSIONS, client.startpage_version.startswith) if startpage_version: filters.update( **{'on_startpage_{0}'.format(startpage_version): True}) # Only filter by locale if they pass a valid locale. locales = filter(client.locale.lower().startswith, LANGUAGE_VALUES) if locales: filters.update(locales__code__in=locales) else: # If the locale is invalid, only match snippets with no # locales specified. filters.update(locales__isnull=True) snippets = self.filter(**filters).distinct() if issubclass(self.model, JSONSnippet): filtering = {'jsonsnippet__in': snippets} else: filtering = {'snippet__in': snippets} # Filter based on ClientMatchRules passed_rules, failed_rules = (ClientMatchRule.objects.filter( **filtering).distinct().evaluate(client)) return snippets.exclude(client_match_rules__in=failed_rules)
def match_client(self, client): from snippets.base.models import CHANNELS, ClientMatchRule, Target snippet_filters = {} target_filters = {} # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". if client.channel == 'default': client_channel = 'nightly' else: client_channel = first(CHANNELS, client.channel.startswith) if client_channel: target_filters.update(**{'on_{0}'.format(client_channel): True}) targets = Target.objects.filter(**target_filters).distinct() # Only filter by locale if they pass a valid locale. locales = list( filter(client.locale.lower().startswith, LANGUAGE_VALUES)) if locales: snippet_filters.update(locales__code__in=locales) else: # If the locale is invalid, only match snippets with no # locales specified. snippet_filters.update(locales__isnull=True) snippets = self.filter(**snippet_filters).filter(targets__in=targets) # Filter based on ClientMatchRules passed_rules, failed_rules = (ClientMatchRule.objects.filter( target__asrsnippet__in=snippets).distinct().evaluate(client)) return snippets.exclude(targets__client_match_rules__in=failed_rules)
def match_client(self, client): from snippets.base.models import CHANNELS, ClientMatchRule, Target snippet_filters = {} target_filters = {} # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". if client.channel == 'default': client_channel = 'nightly' else: client_channel = first(CHANNELS, client.channel.startswith) if client_channel: target_filters.update(**{'on_{0}'.format(client_channel): True}) targets = Target.objects.filter(**target_filters).distinct() # Only filter by locale if they pass a valid locale. locales = list(filter(client.locale.lower().startswith, LANGUAGE_VALUES)) if locales: snippet_filters.update(locales__code__in=locales) else: # If the locale is invalid, only match snippets with no # locales specified. snippet_filters.update(locales__isnull=True) snippets = self.filter(**snippet_filters).filter(targets__in=targets) # Filter based on ClientMatchRules passed_rules, failed_rules = (ClientMatchRule.objects .filter(target__asrsnippet__in=snippets) .distinct() .evaluate(client)) return snippets.exclude(targets__client_match_rules__in=failed_rules)
def match_client(self, client): from snippets.base.models import CHANNELS, Target # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". if client.channel == 'default': client_channel = 'nightly' else: client_channel = first(CHANNELS, client.channel.startswith) or 'release' targets = Target.objects.filter(**{ 'on_{0}'.format(client_channel): True }).distinct() # Include both Jobs with Snippets targeted at the specific full locale (e.g. # en-us) but also Snippets targeted to all territories (en) full_locale = ',{},'.format(client.locale.lower()) splitted_locale = ',{},'.format(client.locale.lower().split('-', 1)[0]) jobs = self.filter( Q(snippet__locale__code__contains=splitted_locale) | Q(snippet__locale__code__contains=full_locale)) jobs = jobs.filter(targets__in=targets) return jobs
def match_client(self, client): from snippets.base.models import (CHANNELS, FENNEC_STARTPAGE_VERSIONS, FIREFOX_STARTPAGE_VERSIONS) filters = {} # Retrieve the first channel that starts with the client's channel. # Allows things like "release-cck-mozilla14" to match "release". client_channel = first(CHANNELS, client.channel.startswith) if client_channel: filters.update(**{'on_{0}'.format(client_channel): True}) # Same matching for the startpage version. STARTPAGE_VERSIONS = FIREFOX_STARTPAGE_VERSIONS if client.name.lower() == 'fennec': STARTPAGE_VERSIONS = FENNEC_STARTPAGE_VERSIONS startpage_version = first(STARTPAGE_VERSIONS, client.startpage_version.startswith) if startpage_version: filters.update( **{'on_startpage_{0}'.format(startpage_version): True}) # Only filter by locale if they pass a valid locale. locales = filter(client.locale.lower().startswith, LANGUAGE_VALUES) if locales: filters.update(locale_set__locale__in=locales) else: # If the locale is invalid, only match snippets with no # locales specified. filters.update(locale_set__isnull=True) snippets = self.filter(**filters).distinct() # Filter based on ClientMatchRules ClientMatchRule = get_model('base', 'ClientMatchRule') passed_rules, failed_rules = (ClientMatchRule.cached_objects.filter( snippet__in=snippets).distinct().evaluate(client)) return snippets.exclude(client_match_rules__in=failed_rules)
def test_no_match(self): """Return None if the callback never passes for any item.""" items = [(0, 'foo'), (1, 'bar'), (2, 'baz')] self.assertEqual(first(items, lambda x: x[0] == 17), None)
def test_basic(self): items = [(0, 'foo'), (1, 'bar'), (2, 'baz')] self.assertEqual(first(items, lambda x: x[0] == 1), (1, 'bar'))
def test_basic(self): items = [(0, 'foo'), (1, 'bar'), (2, 'baz')] eq_(first(items, lambda x: x[0] == 1), (1, 'bar'))