diff --git a/src/chains/admin.py b/src/chains/admin.py index 82bd9fa4..66b14619 100644 --- a/src/chains/admin.py +++ b/src/chains/admin.py @@ -1,5 +1,8 @@ +from typing import List, Optional + from django.contrib import admin from django.db.models import Model +from django.http import HttpRequest from .models import Chain, Feature, GasPrice, Wallet @@ -38,6 +41,18 @@ class ChainAdmin(admin.ModelAdmin[Chain]): ) inlines = [FeatureInline, GasPriceInline, WalletInline] + def get_readonly_fields( + self, request: HttpRequest, obj: Optional[Chain] = None + ) -> List[str]: + if ( + request.user.has_perm("chains.change_only_warning") + and not request.user.is_superuser + ): + readonly_fields = [f.name for f in self.model._meta.fields] + readonly_fields.remove("warning") + return readonly_fields + return [] + @admin.register(GasPrice) class GasPriceAdmin(admin.ModelAdmin[GasPrice]): diff --git a/src/chains/migrations/0037_chain_warning.py b/src/chains/migrations/0037_chain_warning.py new file mode 100644 index 00000000..287611d2 --- /dev/null +++ b/src/chains/migrations/0037_chain_warning.py @@ -0,0 +1,46 @@ +# Generated by Django 4.0.5 on 2022-10-24 14:29 + +from django.apps.registry import Apps +from django.contrib.auth.models import Group, Permission +from django.contrib.contenttypes.models import ContentType +from django.db import migrations, models +from django.db.backends.base.schema import BaseDatabaseSchemaEditor + +from ..models import Chain + + +def create_support_group(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None: + support_group, created = Group.objects.get_or_create(name="support") + + ct = ContentType.objects.get_for_model(Chain) + warning_perm, created = Permission.objects.get_or_create( + codename="change_only_warning", name="Can change only warning", content_type=ct + ) + chain_perm, created = Permission.objects.get_or_create( + codename="change_chain", name="Can change chain", content_type=ct + ) + + support_group.permissions.add(chain_perm) + support_group.permissions.add(warning_perm.id) + + +class Migration(migrations.Migration): + + dependencies = [ + ("chains", "0036_alter_chain_transaction_service_uri_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="chain", + name="warning", + field=models.TextField(blank=True, max_length=511, null=True), + ), + migrations.AlterModelOptions( + name="chain", + options={ + "permissions": [("change_only_warning", "Can change only warning")] + }, + ), + migrations.RunPython(create_support_group, migrations.RunPython.noop), + ] diff --git a/src/chains/models.py b/src/chains/models.py index e1e2df00..670e143a 100644 --- a/src/chains/models.py +++ b/src/chains/models.py @@ -119,6 +119,7 @@ class RpcAuthentication(models.TextChoices): recommended_master_copy_version = models.CharField( max_length=255, validators=[sem_ver_validator] ) + warning = models.TextField(max_length=511, blank=True, null=True) def get_disabled_wallets(self) -> QuerySet["Wallet"]: all_wallets = Wallet.objects.all() @@ -129,6 +130,9 @@ def get_disabled_wallets(self) -> QuerySet["Wallet"]: def __str__(self) -> str: return f"{self.name} | chain_id={self.id}" + class Meta: + permissions = [("change_only_warning", "Can change only warning")] + class GasPrice(models.Model): chain = models.ForeignKey(Chain, on_delete=models.CASCADE) diff --git a/src/chains/serializers.py b/src/chains/serializers.py index 82743d34..12950d03 100644 --- a/src/chains/serializers.py +++ b/src/chains/serializers.py @@ -118,6 +118,7 @@ class ChainSerializer(serializers.ModelSerializer[Chain]): ens_registry_address = EthereumAddressField() disabled_wallets = serializers.SerializerMethodField() features = serializers.SerializerMethodField() + warning = serializers.CharField() class Meta: model = Chain @@ -140,6 +141,7 @@ class Meta: "recommended_master_copy_version", "disabled_wallets", "features", + "warning", ] @staticmethod diff --git a/src/chains/tests/factories.py b/src/chains/tests/factories.py index c5a4d41e..effed5dd 100644 --- a/src/chains/tests/factories.py +++ b/src/chains/tests/factories.py @@ -43,6 +43,7 @@ class Meta: ens_registry_address = factory.LazyAttribute( lambda o: web3.Account.create().address ) + warning = factory.Faker("pystr", max_chars=511) recommended_master_copy_version = "1.3.0" diff --git a/src/chains/tests/test_views.py b/src/chains/tests/test_views.py index f0a8d7e9..bc739650 100644 --- a/src/chains/tests/test_views.py +++ b/src/chains/tests/test_views.py @@ -80,6 +80,7 @@ def test_json_payload_format(self) -> None: "recommendedMasterCopyVersion": chain.recommended_master_copy_version, "disabledWallets": [], "features": [], + "warning": chain.warning, } ], } @@ -195,6 +196,7 @@ def test_json_payload_format(self) -> None: "recommendedMasterCopyVersion": chain.recommended_master_copy_version, "disabledWallets": [], "features": [], + "warning": chain.warning, } response = self.client.get(path=url, data=None, format="json")