Merge pull request #1423 from kmaglione/blocklist-pref-reset

Bug 941150: Support preference reset in the blocklist service
This commit is contained in:
Christopher Van 2014-01-06 13:54:36 -08:00
Родитель 20e4d9249c b8d88947af
Коммит 824455a7b0
6 изменённых файлов: 61 добавлений и 11 удалений

Просмотреть файл

@ -4,6 +4,11 @@ from . import forms
from . import models
def stacked_inline(model):
return type(model.__name__ + 'Inline', (admin.StackedInline,),
{'model': model})
class PluginAdmin(admin.ModelAdmin):
form = forms.BlocklistPluginForm
@ -12,10 +17,15 @@ class AppAdmin(admin.ModelAdmin):
form = forms.BlocklistAppForm
ms = models.BlocklistItem, models.BlocklistPlugin, models.BlocklistGfx
inlines = [type(cls.__name__ + 'Inline', (admin.StackedInline,),
{'model': cls})
for cls in ms]
# TODO: The prefs should be inlined in the detail edit form as well,
# and/or the detail edit form should be inlined here. Django does
# not make either of these things easy.
class ItemAdmin(admin.ModelAdmin):
inlines = stacked_inline(models.BlocklistPref),
ms = models.BlocklistItem, models.BlocklistPlugin, models.BlocklistGfx,
inlines = map(stacked_inline, ms)
class DetailAdmin(admin.ModelAdmin):
@ -24,7 +34,7 @@ class DetailAdmin(admin.ModelAdmin):
admin.site.register(models.BlocklistApp, AppAdmin)
admin.site.register(models.BlocklistCA)
admin.site.register(models.BlocklistItem)
admin.site.register(models.BlocklistItem, ItemAdmin)
admin.site.register(models.BlocklistPlugin, PluginAdmin)
admin.site.register(models.BlocklistGfx)
admin.site.register(models.BlocklistDetail, DetailAdmin)

Просмотреть файл

@ -145,3 +145,13 @@ class BlocklistGfx(BlocklistBase, amo.models.ModelBase):
def flush_urls(self):
return ['/blocklist*'] # no lang/app
class BlocklistPref(amo.models.ModelBase):
"""Preferences which should be reset when a blocked item is detected."""
blitem = models.ForeignKey('BlocklistItem', related_name='prefs')
pref = models.CharField(max_length=255)
class Meta:
db_table = 'blitemprefs'

Просмотреть файл

@ -2,9 +2,9 @@
<blocklist xmlns="http://www.mozilla.org/2006/addons-blocklist" lastupdate="{{ last_update }}">
{% if items %}
<emItems>
{% for guid, rows in items.items() %}
<emItem {{ attrs(id=guid, os=rows.os, blockID=rows.block_id) }}>
{% for row in rows.rows %}
{% for guid, details in items.items() %}
<emItem {{ attrs(id=guid, os=details.os, blockID=details.block_id) }}>
{% for row in details.rows %}
{% if row.min or row.max or row.severity or row.apps %}
<versionRange {{ attrs(minVersion=row.min, maxVersion=row.max,
severity=row.severity or None) }}>
@ -18,6 +18,11 @@
</versionRange>
{% endif %}
{% endfor %}
<prefs>
{% for pref in details.prefs %}
<pref>{{pref}}</pref>
{% endfor %}
</prefs>
</emItem>
{% endfor %}
</emItems>

Просмотреть файл

@ -12,7 +12,8 @@ import amo
import amo.tests
from amo.urlresolvers import reverse
from blocklist.models import (BlocklistApp, BlocklistCA, BlocklistDetail,
BlocklistGfx, BlocklistItem, BlocklistPlugin)
BlocklistGfx, BlocklistItem, BlocklistPlugin,
BlocklistPref)
base_xml = """
<?xml version="1.0"?>
@ -57,6 +58,8 @@ class BlocklistItemTest(BlocklistViewTest):
super(BlocklistItemTest, self).setUp()
self.item = BlocklistItem.objects.create(guid='guid@addon.com',
details=self.details)
self.pref = BlocklistPref.objects.create(blitem=self.item,
pref='foo.bar')
self.app = BlocklistApp.objects.create(blitem=self.item,
guid=amo.FIREFOX.guid)
@ -156,6 +159,14 @@ class BlocklistItemTest(BlocklistViewTest):
item = self.dom(self.fx4_url).getElementsByTagName('emItem')[0]
eq_(item.getAttribute('os'), 'win,mac')
def test_item_pref(self):
self.item.update(severity=2)
eq_(len(self.vr()), 1)
item = self.dom(self.fx4_url).getElementsByTagName('emItem')[0]
prefs = item.getElementsByTagName('prefs')
pref = prefs[0].getElementsByTagName('pref')
eq_(pref[0].firstChild.nodeValue, self.pref.pref)
def test_item_severity(self):
self.item.update(severity=2)
eq_(len(self.vr()), 1)

Просмотреть файл

@ -21,7 +21,7 @@ from .models import (BlocklistApp, BlocklistCA, BlocklistDetail, BlocklistGfx,
App = collections.namedtuple('App', 'guid min max')
BlItem = collections.namedtuple('BlItem', 'rows os modified block_id')
BlItem = collections.namedtuple('BlItem', 'rows os modified block_id prefs')
def blocklist(request, apiver, app, appver):
@ -83,6 +83,7 @@ def get_items(apiver, app, appver=None):
# item and collapse each item's apps.
addons = (BlocklistItem.objects.no_cache()
.select_related('details')
.prefetch_related('prefs')
.filter(Q(app__guid__isnull=True) | Q(app__guid=app))
.order_by('-modified')
.extra(select={'app_guid': 'blapps.guid',
@ -92,14 +93,16 @@ def get_items(apiver, app, appver=None):
for guid, rows in sorted_groupby(addons, 'guid'):
rows = list(rows)
rr = []
prefs = []
for id, rs in sorted_groupby(rows, 'id'):
rs = list(rs)
rr.append(rs[0])
prefs.extend(p.pref for p in rs[0].prefs.all())
rs[0].apps = [App(r.app_guid, r.app_min, r.app_max)
for r in rs if r.app_guid]
os = [r.os for r in rr if r.os]
items[guid] = BlItem(rr, os[0] if os else None, rows[0].modified,
rows[0].block_id)
rows[0].block_id, prefs)
details[guid] = sorted(rows, key=attrgetter('id'))[0]
return items, details

Просмотреть файл

@ -0,0 +1,11 @@
CREATE TABLE `blitemprefs` (
`id` int(11) unsigned AUTO_INCREMENT NOT NULL PRIMARY KEY,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`blitem_id` int(11) unsigned NOT NULL,
`pref` varchar(255) NOT NULL
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `blitemprefs`
ADD CONSTRAINT `blitem_id_refs_id_9e548741`
FOREIGN KEY (`blitem_id`) REFERENCES `blitems` (`id`)
ON DELETE CASCADE;