def addon_name_blocklist(request): rn = ReverseNameLookup() addon = None if request.method == "POST": rn.delete(rn.get(request.GET["addon"])) if request.GET.get("addon"): id = rn.get(request.GET.get("addon")) if id: qs = Addon.objects.filter(id=id) addon = qs[0] if qs else None return jingo.render(request, "zadmin/addon-name-blocklist.html", dict(rn=rn, addon=addon))
def _build_reverse_name_lookup(names, **kw): clear = kw.get("clear", False) translations = Translation.objects.filter(id__in=names).values_list("id", "localized_string") if clear: for addon_id in names.values(): ReverseNameLookup.delete(addon_id) for t_id, string in translations: if string: ReverseNameLookup.add(string, names[t_id])
def _build_reverse_name_lookup(names, **kw): clear = kw.get('clear', False) translations = (Translation.objects.filter(id__in=names).values_list( 'id', 'localized_string')) if clear: for addon_id in names.values(): ReverseNameLookup().delete(addon_id) for t_id, string in translations: if string: ReverseNameLookup().add(string, names[t_id])
def build_reverse_name_lookup(): """Builds a Reverse Name lookup table in REDIS.""" ReverseNameLookup().clear() ReverseNameLookup(webapp=True).clear() # Get all add-on name ids names = list(Addon.objects.filter( name__isnull=False, type__in=[amo.ADDON_EXTENSION, amo.ADDON_THEME, amo.ADDON_WEBAPP]) .values('name_id', 'id', 'type')) for chunk in chunked(names, 100): _build_reverse_name_lookup.delay(chunk)
def test_name_taken(self): """Test that the add-on name is not already taken.""" ReverseNameLookup().add('Delicious Bookmarks', 34) data = self._form_data({'name': 'Delicious Bookmarks'}) r = self.client.post(self.url, data) self.assertFormError( r, 'basic_form', 'name', 'This name is already in use. Please choose another.')
def _build_reverse_name_lookup(data, **kw): task_log.debug('Updating reverse name lookup table for %s addons.' % len(data)) clear = kw.get('clear', False) name_ids = [a['name_id'] for a in data] translations = dict(Translation.objects.filter(id__in=name_ids) .values_list('id', 'localized_string')) for addon in data: webapp = addon['type'] == amo.ADDON_WEBAPP if clear: ReverseNameLookup(webapp).delete(addon['id']) task_log.debug('Clearing name for addon %s.' % addon['id']) if translations.get(addon['name_id'], ''): ReverseNameLookup(webapp).add(translations.get(addon['name_id']), addon['id']) task_log.debug('Adding name_id %s for addon %s.' % (addon['name_id'], addon['id']))
def addon_name_blocklist(request): rn = ReverseNameLookup() addon = None if request.method == 'POST': rn.delete(rn.get(request.GET['addon'])) if request.GET.get('addon'): id = rn.get(request.GET.get('addon')) if id: qs = Addon.objects.filter(id=id) addon = qs[0] if qs else None return jingo.render(request, 'zadmin/addon-name-blocklist.html', dict(rn=rn, addon=addon))
def test_addon_and_app_namespaces(self): eq_(ReverseNameLookup(webapp=False).get('Delicious Bookmarks'), 3615) eq_(ReverseNameLookup(webapp=True).get('Delicious Bookmarks'), None) # Note: The factory creates the app which calls the ReverseNameLookup # in a post_save signal, so no need to call it explicitly here. app = amo.tests.addon_factory(type=amo.ADDON_WEBAPP) self.assertTrue(app.is_webapp()) eq_(ReverseNameLookup(webapp=False).get(app.name), None) eq_(ReverseNameLookup(webapp=True).get(app.name), app.id) # Show we can also create an app with the same name as an addon name = 'Delicious Bookmarks' app = amo.tests.addon_factory(name=name, type=amo.ADDON_WEBAPP) self.assertTrue(app.is_webapp()) eq_(ReverseNameLookup(webapp=False).get(name), 3615) eq_(ReverseNameLookup(webapp=True).get(name), app.id)
def _setup_other_webapp(self): self._step() # Generate another webapp to test name uniqueness. app = amo.tests.addon_factory(type=amo.ADDON_WEBAPP, name='Cool App') eq_(ReverseNameLookup(webapp=True).get(app.name), app.id)
def test_lookup(self): cron.build_reverse_name_lookup() eq_(ReverseNameLookup().get('Delicious Bookmarks'), 3615)
def test_get_case(self): eq_(ReverseNameLookup.get('delicious bookmarks'), 3615)
def test_get_strip(self): eq_(ReverseNameLookup.get('Delicious Bookmarks '), 3615)
def test_update_addon(self): eq_(ReverseNameLookup.get('Delicious Bookmarks'), 3615) self.addon.name = 'boo' self.addon.save() eq_(ReverseNameLookup.get('Delicious Bookmarks'), None) eq_(ReverseNameLookup.get('boo'), 3615)
def test_delete_addon(self): eq_(ReverseNameLookup.get('Delicious Bookmarks'), 3615) self.addon.delete('farewell my sweet amo, it was a good run') eq_(ReverseNameLookup.get('Delicious Bookmarks'), None)
def clear_name_table(sender, **kw): addon = kw['instance'] ReverseNameLookup.delete(addon.id)
def test_lookup(self): cron.build_reverse_name_lookup() eq_(ReverseNameLookup.get('Delicious Bookmarks'), 3615)