def get_queryset(self): params = self.get_params() edition = params.pop('edition', 'default') position_field = 'position_china' if edition == 'china' else 'position' # Base queryset for editorial content. qs = (DiscoveryItem.objects .prefetch_related('addon') .filter(**{position_field + '__gt': 0}) .order_by(position_field)) # Recommendations stuff, potentially replacing some/all items in # the queryset with recommendations if applicable. if edition == 'china': # No TAAR for China Edition. telemetry_id = None else: telemetry_id = params.pop('telemetry-client-id', None) if switch_is_active('disco-recommendations') and telemetry_id: recommendations = get_recommendations( telemetry_id, params) if recommendations: # if we got some recommendations then replace the # extensions in the queryset with them. # Leave the non-extensions (personas) alone. qs = replace_extensions(qs, recommendations) return qs
def get_queryset(self): edition = self.request.GET.get('edition', 'default') position_field = 'position_china' if edition == 'china' else 'position' # Base queryset for editorial content. qs = (DiscoveryItem.objects.prefetch_related( 'addon___current_version__previews').filter( **{ position_field + '__gt': 0 }).order_by(position_field)) # Recommendations stuff, potentially replacing some/all items in # the queryset with recommendations if applicable. if edition == 'china': # No TAAR for China Edition. telemetry_id = None else: telemetry_id = self.request.GET.get('telemetry-client-id', None) if switch_is_active('disco-recommendations') and ( telemetry_id and VALID_CLIENT_ID.match(telemetry_id)): overrides = list( DiscoveryItem.objects.values_list( 'addon__guid', flat=True).filter( position_override__gt=0).order_by('position_override')) recommendations = get_disco_recommendations( telemetry_id, overrides) if recommendations: # if we got some recommendations then replace the # extensions in the queryset with them. # Leave the non-extensions (personas) alone. qs = replace_extensions(qs, recommendations) return qs
def test_recommendations(self): author = user_factory() recommendations = { 101: addon_factory(id=101, guid='101@mozilla', users=[author]), 102: addon_factory(id=102, guid='102@mozilla', users=[author]), 103: addon_factory(id=103, guid='103@mozilla', users=[author]), 104: addon_factory(id=104, guid='104@mozilla', users=[author]), } replacement_items = [ DiscoItem(addon_id=101, is_recommendation=True), DiscoItem(addon_id=102, is_recommendation=True), DiscoItem(addon_id=103, is_recommendation=True), DiscoItem(addon_id=104, is_recommendation=True), ] self.addons.update(recommendations) self.get_recommendations.return_value = replacement_items response = self.client.get(self.url, {'lang': 'en-US', 'telemetry-client-id': '666'}) # should still be the same number of results. assert response.data['count'] == len(discopane_items) assert response.data['results'] # personas aren't replaced by recommendations, so should be as before. new_discopane_items = replace_extensions( discopane_items, replacement_items) for i, result in enumerate(response.data['results']): if 'theme_data' in result['addon']: self._check_disco_theme(result, new_discopane_items[i]) # There aren't any theme recommendations. assert result['is_recommendation'] is False else: self._check_disco_addon(result, new_discopane_items[i]) assert result['is_recommendation'] is True
def test_recommendations(self): author = user_factory() recommendations = { 101: addon_factory(id=101, guid='101@mozilla', users=[author]), 102: addon_factory(id=102, guid='102@mozilla', users=[author]), 103: addon_factory(id=103, guid='103@mozilla', users=[author]), 104: addon_factory(id=104, guid='104@mozilla', users=[author]), } replacement_items = [ DiscoItem(addon_id=101), DiscoItem(addon_id=102), DiscoItem(addon_id=103), DiscoItem(addon_id=104), ] self.addons.update(recommendations) self.get_recommendations.return_value = replacement_items response = self.client.get(self.url, { 'lang': 'en-US', 'telemetry-client-id': '666' }) # should still be the same number of results. assert response.data['count'] == len(discopane_items) assert response.data['results'] # personas aren't replaced by recommendations, so should be as before. new_discopane_items = replace_extensions(discopane_items, replacement_items) for i, result in enumerate(response.data['results']): if 'theme_data' in result['addon']: self._check_disco_theme(result, new_discopane_items[i]) else: self._check_disco_addon(result, new_discopane_items[i])
def test_replace_extensions(): source = [ DiscoveryItem(addon=addon_factory(), custom_addon_name=u'replacê me'), DiscoveryItem( addon=addon_factory(), custom_addon_name=u'replace me tøø'), DiscoveryItem( addon=addon_factory(type=amo.ADDON_PERSONA), custom_addon_name=u'ŋot me'), DiscoveryItem( addon=addon_factory(type=amo.ADDON_PERSONA), custom_addon_name=u'ŋor me'), DiscoveryItem(addon=addon_factory(), custom_addon_name=u'probably me'), DiscoveryItem( addon=addon_factory(type=amo.ADDON_PERSONA), custom_addon_name=u'safê') ] # Just 2 replacements replacements = [ DiscoveryItem( addon=addon_factory(), custom_addon_name=u'just for you'), DiscoveryItem( addon=addon_factory(), custom_addon_name=u'and this øne'), ] result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], # we only had two replacements. source[2], source[3], source[4], source[5], ], result # Add a few more so all extensions are replaced, with one spare. replacements.append(DiscoveryItem( addon=addon_factory(), custom_addon_name=u'extra ône')) replacements.append(DiscoveryItem( addon=addon_factory(), custom_addon_name=u'extra tôo')) result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], source[2], # Not an extension, so not replaced. source[3], # Not an extension, so not replaced. replacements[2], source[5], # Not an extension, so not replaced. ], result
def test_replace_extensions(): source = [ DiscoveryItem(addon=addon_factory(), custom_addon_name=u'replacê me'), DiscoveryItem( addon=addon_factory(), custom_addon_name=u'replace me tøø'), DiscoveryItem( addon=addon_factory(type=amo.ADDON_STATICTHEME), custom_addon_name=u'ŋot me'), DiscoveryItem( addon=addon_factory(type=amo.ADDON_STATICTHEME), custom_addon_name=u'ŋor me'), DiscoveryItem(addon=addon_factory(), custom_addon_name=u'probably me'), DiscoveryItem( addon=addon_factory(type=amo.ADDON_STATICTHEME), custom_addon_name=u'safê') ] # Just 2 replacements replacements = [ DiscoveryItem( addon=addon_factory(), custom_addon_name=u'just for you'), DiscoveryItem( addon=addon_factory(), custom_addon_name=u'and this øne'), ] result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], # we only had two replacements. source[2], source[3], source[4], source[5], ], result # Add a few more so all extensions are replaced, with one spare. replacements.append(DiscoveryItem( addon=addon_factory(), custom_addon_name=u'extra ône')) replacements.append(DiscoveryItem( addon=addon_factory(), custom_addon_name=u'extra tôo')) result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], source[2], # Not an extension, so not replaced. source[3], # Not an extension, so not replaced. replacements[2], source[5], # Not an extension, so not replaced. ], result
def get_discopane_items(self): if not getattr(self, 'discopane_items', None): telemetry_id = (self.kwargs.get('telemetry-client-id') or self.request.GET.get('telemetry-client-id')) self.discopane_items = discopane_items if switch_is_active('disco-recommendations') and telemetry_id: recommendations = get_recommendations(telemetry_id) if recommendations: # if we got some recommendations then replace the # extensions in discopane_items with them. # Leave the non-extensions (personas) alone. self.discopane_items = replace_extensions( discopane_items, recommendations) return self.discopane_items
def get_discopane_items(self): if not getattr(self, 'discopane_items', None): params = self.get_params() telemetry_id = params.pop('telemetry-client-id', None) self.discopane_items = discopane_items if switch_is_active('disco-recommendations') and telemetry_id: recommendations = get_recommendations(telemetry_id, params) if recommendations: # if we got some recommendations then replace the # extensions in discopane_items with them. # Leave the non-extensions (personas) alone. self.discopane_items = replace_extensions( discopane_items, recommendations) return self.discopane_items
def test_replace_extensions(): source = [ DiscoItem(addon_id=101, addon_name=u'replacê me'), DiscoItem(addon_id=102, addon_name=u'replace me tøø'), DiscoItem(addon_id=103, addon_name=u'ŋot me', type=amo.ADDON_PERSONA), DiscoItem(addon_id=104, addon_name=u'ŋor me', type=amo.ADDON_PERSONA), DiscoItem(addon_id=105, addon_name=u'probably me'), DiscoItem(addon_id=106, addon_name=u'safê', type=amo.ADDON_PERSONA), ] # Just 2 replacements replacements = [ DiscoItem(addon_id=999, addon_name=u'just for you'), DiscoItem(addon_id=998, addon_name=u'and this øne'), ] result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], # we only had two replacements. source[2], source[3], source[4], source[5], ], result # Add a few more so all extensions are replaced, with one spare. replacements.append(DiscoItem(addon_id=997, addon_name='extra one')) replacements.append(DiscoItem(addon_id=997, addon_name='extra too')) result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], source[2], # Not an extension, so not replaced. source[3], # Not an extension, so not replaced. replacements[2], source[5], # Not an extension, so not replaced. ], result
def test_replace_extensions(): source = [ DiscoveryItem(addon=addon_factory()), # replaced DiscoveryItem(addon=addon_factory()), # also replaced DiscoveryItem(addon=addon_factory(type=amo.ADDON_STATICTHEME)), # not DiscoveryItem(addon=addon_factory(type=amo.ADDON_STATICTHEME)), # nope DiscoveryItem(addon=addon_factory()), # possibly replaced DiscoveryItem(addon=addon_factory(type=amo.ADDON_STATICTHEME)), # nope ] # Just 2 replacements replacements = [ DiscoveryItem(addon=addon_factory()), DiscoveryItem(addon=addon_factory()), ] result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], # we only had two replacements. source[2], source[3], source[4], source[5], ], result # Add a few more so all extensions are replaced, with one spare. replacements.append(DiscoveryItem(addon=addon_factory())) replacements.append(DiscoveryItem(addon=addon_factory())) result = replace_extensions(source, replacements) assert result == [ replacements[0], replacements[1], source[2], # Not an extension, so not replaced. source[3], # Not an extension, so not replaced. replacements[2], source[5], # Not an extension, so not replaced. ], result
def test_recommendations(self): author = user_factory() recommendations = [ addon_factory(guid='101@mozilla', users=[author]), addon_factory(guid='102@mozilla', users=[author]), addon_factory(guid='103@mozilla', users=[author]), addon_factory(guid='104@mozilla', users=[author]), ] replacement_items = [ DiscoveryItem(addon=recommendations[0]), DiscoveryItem(addon=recommendations[1]), DiscoveryItem(addon=recommendations[2]), DiscoveryItem(addon=recommendations[3]), ] self.addons.extend(recommendations) self.get_recommendations.return_value = replacement_items response = self.client.get(self.url, { 'lang': 'en-US', 'telemetry-client-id': '666', 'platform': 'WINNT' }) self.get_recommendations.assert_called_with('666', { 'locale': 'en-US', 'platform': 'WINNT' }) # should still be the same number of results. discopane_items = DiscoveryItem.objects.filter( position__gt=0).order_by('position') assert response.data['count'] == len(discopane_items) assert response.data['results'] # personas aren't replaced by recommendations, so should be as before. new_discopane_items = replace_extensions(discopane_items, replacement_items) for i, result in enumerate(response.data['results']): if 'theme_data' in result['addon']: self._check_disco_theme(result, new_discopane_items[i]) # There aren't any theme recommendations. assert result['is_recommendation'] is False else: self._check_disco_addon(result, new_discopane_items[i]) assert result['is_recommendation'] is True
def get_discopane_items(self): if not getattr(self, 'discopane_items', None): params = self.get_params() edition = params.pop('edition', 'default') self.discopane_items = discopane_items.get( edition, discopane_items['default']) if edition == 'china': # No TAAR for China Edition. telemetry_id = None else: telemetry_id = params.pop('telemetry-client-id', None) if switch_is_active('disco-recommendations') and telemetry_id: recommendations = get_recommendations(telemetry_id, params) if recommendations: # if we got some recommendations then replace the # extensions in discopane_items with them. # Leave the non-extensions (personas) alone. self.discopane_items = replace_extensions( self.discopane_items, recommendations) return self.discopane_items
def test_recommendations(self): author = user_factory() recommendations = [ addon_factory(guid='101@mozilla', users=[author]), addon_factory(guid='102@mozilla', users=[author]), addon_factory(guid='103@mozilla', users=[author]), addon_factory(guid='104@mozilla', users=[author]), ] replacement_items = [ DiscoveryItem(addon=recommendations[0]), DiscoveryItem(addon=recommendations[1]), DiscoveryItem(addon=recommendations[2]), DiscoveryItem(addon=recommendations[3]), ] self.addons.extend(recommendations) self.get_recommendations.return_value = replacement_items response = self.client.get( self.url, {'lang': 'en-US', 'telemetry-client-id': '666', 'platform': 'WINNT'}) self.get_recommendations.assert_called_with( '666', {'locale': 'en-US', 'platform': 'WINNT'}) # should still be the same number of results. discopane_items = DiscoveryItem.objects.filter( position__gt=0).order_by('position') assert response.data['count'] == len(discopane_items) assert response.data['results'] # personas aren't replaced by recommendations, so should be as before. new_discopane_items = replace_extensions( discopane_items, replacement_items) for i, result in enumerate(response.data['results']): if 'theme_data' in result['addon']: self._check_disco_theme(result, new_discopane_items[i]) # There aren't any theme recommendations. assert result['is_recommendation'] is False else: self._check_disco_addon(result, new_discopane_items[i]) assert result['is_recommendation'] is True