Skip to content

Commit

Permalink
Allow adding, viewing information about and deleting soft-blocks in t…
Browse files Browse the repository at this point in the history
…he admin (#22765)

* Allow adding, viewing information about and deleting soft-blocks in the admin

Like other blocklist operations, it goes through a BlocklistSubmission, which
gains a new "block_type" field to determine what kind of block to apply for
this particular submission.

Note: If a version is already soft-blocked or hard-blocked, that version won't
be selectable when creating a new BlocklistSubmission, even of another type.
Future actions will be added to soften or harden blocks.
  • Loading branch information
diox authored Oct 29, 2024
1 parent 4401310 commit 97fe21c
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/olympia/amo/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,13 +989,13 @@ def version_factory(file_kw=None, **kw):
return ver


def block_factory(*, version_ids=None, **kwargs):
def block_factory(*, version_ids=None, soft=False, **kwargs):
block = Block.objects.create(**kwargs)
if version_ids is None and block.addon:
version_ids = list(block.addon.versions.values_list('id', flat=True))
if version_ids is not None:
BlockVersion.objects.bulk_create(
BlockVersion(block=block, version_id=version_id)
BlockVersion(block=block, version_id=version_id, soft=soft)
for version_id in version_ids
)
return block
Expand Down
5 changes: 3 additions & 2 deletions src/olympia/blocklist/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class BlocklistSubmissionAdmin(AMOModelAdmin):
'blocks_count',
'action',
'state',
'block_type',
'delayed_until',
'updated_by',
'modified',
Expand Down Expand Up @@ -276,6 +277,7 @@ def get_fieldsets(self, request, obj):
{
'fields': (
changed_version_ids_field,
'block_type',
'disable_addon',
'update_url_value',
'url',
Expand Down Expand Up @@ -334,7 +336,7 @@ def get_readonly_fields(self, request, obj=None):
self.get_fieldsets(request, obj)
)
if obj or not self.is_add_change_submission(request, obj):
ro_fields.append('delay_days')
ro_fields += ['block_type', 'delay_days']

return ro_fields

Expand Down Expand Up @@ -407,7 +409,6 @@ def add_view(self, request, **kwargs):
'errors': admin.helpers.AdminErrorList(form, []),
'preserved_filters': self.get_preserved_filters(request),
# extra context we use in our custom template
'is_delete': is_delete,
'block_history': self.block_history(self.model(input_guids=guids_data)),
'submission_published': False,
}
Expand Down
2 changes: 2 additions & 0 deletions src/olympia/blocklist/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_context(self, name, value, attrs):
},
'blocks': self.blocks,
'total_adu': sum(block.current_adu for block in self.blocks),
'is_add_change': self.is_add_change,
}


Expand Down Expand Up @@ -192,6 +193,7 @@ def setup_changed_version_ids_field(self, field, data):
self.changed_version_ids_choices = self.instance.changed_version_ids
field.widget.choices = self.changed_version_ids_choices
field.widget.blocks = self.blocks
field.widget.is_add_change = self.is_add_change

def get_value(self, field_name, default):
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-29 17:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blocklist', '0035_alter_blockversion_soft'),
]

operations = [
migrations.AddField(
model_name='blocklistsubmission',
name='block_type',
field=models.IntegerField(choices=[(0, '🛑 Hard-Block'), (1, '⚠️ Soft-Block')], default=0),
),
migrations.AlterField(
model_name='blockversion',
name='soft',
field=models.BooleanField(choices=[(0, '🛑 Hard-Blocked'), (1, '⚠️ Soft-Blocked')], default=False),
),
]
21 changes: 18 additions & 3 deletions src/olympia/blocklist/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ def get_blocks_from_guids(cls, guids):

class BlockVersion(ModelBase):
BLOCK_TYPE_CHOICES = Choices(
('BLOCKED', 0, 'Blocked'),
('SOFT_BLOCKED', 1, 'Soft-Blocked'),
('BLOCKED', 0, '🛑 Hard-Blocked'),
('SOFT_BLOCKED', 1, '⚠️ Soft-Blocked'),
)
version = models.OneToOneField(Version, on_delete=models.CASCADE)
block = models.ForeignKey(Block, on_delete=models.CASCADE)
Expand Down Expand Up @@ -216,6 +216,18 @@ class BlocklistSubmission(ModelBase):
ACTION_ADDCHANGE: 'Add/Change',
ACTION_DELETE: 'Delete',
}
BLOCK_TYPE_CHOICES = Choices(
(
BlockVersion.BLOCK_TYPE_CHOICES.BLOCKED.constant,
BlockVersion.BLOCK_TYPE_CHOICES.BLOCKED.value,
'🛑 Hard-Block',
),
(
BlockVersion.BLOCK_TYPE_CHOICES.SOFT_BLOCKED.constant,
BlockVersion.BLOCK_TYPE_CHOICES.SOFT_BLOCKED.value,
'⚠️ Soft-Block',
),
)
FakeBlockAddonVersion = namedtuple(
'FakeBlockAddonVersion',
(
Expand All @@ -236,7 +248,6 @@ class BlocklistSubmission(ModelBase):
)

action = models.SmallIntegerField(choices=ACTIONS.items(), default=ACTION_ADDCHANGE)

input_guids = models.TextField()
changed_version_ids = models.JSONField(default=list)
to_block = models.JSONField(default=list)
Expand Down Expand Up @@ -265,6 +276,10 @@ class BlocklistSubmission(ModelBase):
help_text='The submission will not be published into blocks before this time.',
)
disable_addon = models.BooleanField(default=True)
block_type = models.IntegerField(
default=BLOCK_TYPE_CHOICES.BLOCKED,
choices=BLOCK_TYPE_CHOICES,
)

objects = BlocklistSubmissionManager()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ <h2>{{ fieldsets.2.0 }}</h2>
{{ adminform.form.non_field_errors }}
</div>
{% endif %}
<div class="form-row field-block_type">
{{ adminform.form.block_type.errors }}
{{ adminform.form.block_type.label_tag }}
{{ adminform.form.block_type }}
<p class="help">{{ adminform.form.block_type.help_text }}</p>
</div>
<div class="form-row">
{{ adminform.form.disable_addon.errors }}
{{ adminform.form.disable_addon.label_tag }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
{% if log.details %}{{ log.details.guid }}{% else %}{{ log.arguments.1 }}{% endif %}{% if 'min_version' in log.details %}
, versions {{ log.details.min_version }} - {{ log.details.max_version }}.
{% elif 'added_versions' in log.details %}
, versions added [{{ log.details.added_versions|join:', ' }}].
, versions {% if log.details.soft %}soft-{% else %}hard-{% endif %}blocked [{{ log.details.added_versions|join:', ' }}].
{% elif 'removed_versions' in log.details %}
, versions removed [{{ log.details.removed_versions|join:', ' }}].
, versions unblocked [{{ log.details.removed_versions|join:', ' }}].
{% else %}.
{% endif %}
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ <h3>{{ blocks|length|intcomma }} Add-on GUIDs with {{ total_adu|intcomma }} user
name="changed_version_ids"
value="{{ version.id }}"
{% if version.id in widget.value %}checked{% endif %}
> {% if is_delete %}Unblock{% else %}Block{% endif %} {{ version.version }}</label>
> {% if is_add_change %}Block{% else %}Unblock{% endif %} {{ version.version }} {% if version.blockversion %}({{ version.blockversion.get_soft_display }}){% endif %}</label>
{% else %}
<span title="{% if version.is_blocked %}Blocked{% else %}Not blocked{% endif %}">
<!-- Red Hexagonal stop sign for Blocked; Green cirle for not blocked -->
{% if version.is_blocked %}&#x1F6D1;{% else %}&#x1F7E2;{% endif %}{{ version.version }}
<span>
{{ version.version }} ({% if version.is_blocked %}{{ version.blockversion.get_soft_display }}{% else %}&#x1F7E2; Not Blocked{% endif %})
{% if version.blocklist_submission_id %}
[<a href="{% url 'admin:blocklist_blocklistsubmission_change' version.blocklist_submission_id %}">Edit Submission</a>]
{% endif %}
Expand Down
Loading

0 comments on commit 97fe21c

Please sign in to comment.