Add CinderPolicy model and sync_cinder_policies management command (#21773)

* CinderPolicy model: add parent and make uuid unique

* Add sync_cinder_policies management command + task
This commit is contained in:
Kevin Meinhardt 2024-02-01 13:26:29 +01:00 коммит произвёл GitHub
Родитель c1cc3b512d
Коммит fb8a38ed45
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 92 добавлений и 1 удалений

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

@ -0,0 +1,13 @@
from django.core.management.base import BaseCommand
import olympia.core.logger
from olympia.abuse.tasks import sync_cinder_policies
class Command(BaseCommand):
log = olympia.core.logger.getLogger('z.abuse')
def handle(self, *args, **options):
sync_cinder_policies.apply_async()
self.log.info('Triggered policy sync task.')

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

@ -0,0 +1,24 @@
# Generated by Django 4.2.9 on 2024-02-01 09:00
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('abuse', '0023_cinderjob_decision_notes_alter_cinderjob_appeal_job_and_more'),
]
operations = [
migrations.AddField(
model_name='cinderpolicy',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='abuse.cinderpolicy'),
),
migrations.AlterField(
model_name='cinderpolicy',
name='uuid',
field=models.CharField(max_length=36, unique=True),
),
]

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

@ -816,6 +816,16 @@ class CantBeAppealed(Exception):
class CinderPolicy(ModelBase):
uuid = models.CharField(max_length=36)
uuid = models.CharField(max_length=36, unique=True)
name = models.CharField(max_length=50)
text = models.TextField()
parent = models.ForeignKey(
'self',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='children',
)
def __str__(self):
return self.name

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

@ -5,7 +5,9 @@ from unittest import mock
from django.conf import settings
from django.core import mail
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.db.utils import IntegrityError
import pytest
import responses
from olympia.amo.tests import TestCase, addon_factory, collection_factory, user_factory
@ -1067,3 +1069,45 @@ class TestCinderJobCanBeAppealed(TestCase):
)
self.initial_job.update(appeal_job=appeal_job)
assert appeal_job.can_be_appealed(is_reporter=False)
class TestCinderPolicy(TestCase):
def test_create_cinder_policy_with_required_fields(self):
policy = CinderPolicy.objects.create(
name='Test Policy',
text='Test Policy Description',
uuid='test-uuid',
)
self.assertEqual(policy.name, 'Test Policy')
self.assertEqual(policy.text, 'Test Policy Description')
self.assertEqual(policy.uuid, 'test-uuid')
def test_create_cinder_policy_with_child_policy(self):
parent_policy = CinderPolicy.objects.create(
name='Parent Policy',
text='Parent Policy Description',
uuid='parent-uuid',
)
child_policy = CinderPolicy.objects.create(
name='Child Policy',
text='Child Policy Description',
uuid='child-uuid',
parent=parent_policy,
)
self.assertEqual(child_policy.name, 'Child Policy')
self.assertEqual(child_policy.text, 'Child Policy Description')
self.assertEqual(child_policy.uuid, 'child-uuid')
self.assertEqual(child_policy.parent, parent_policy)
def test_create_cinder_policy_with_duplicate_uuid(self):
existing_policy = CinderPolicy.objects.create(
name='Policy 1',
text='Policy 1 Description',
uuid='duplicate-uuid',
)
with pytest.raises(IntegrityError):
CinderPolicy.objects.create(
name='Policy 2',
text='Policy 2 Description',
uuid=existing_policy.uuid,
)