diff --git a/component/migrations/0007_remove_ifcomponent_component_ptr_and_more.py b/component/migrations/0007_remove_ifcomponent_component_ptr_and_more.py new file mode 100644 index 0000000..6c13ce3 --- /dev/null +++ b/component/migrations/0007_remove_ifcomponent_component_ptr_and_more.py @@ -0,0 +1,48 @@ +# Generated by Django 5.1.7 on 2025-06-08 10:34 + +import django.contrib.postgres.fields +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("component", "0006_alter_component_component_type"), + ] + + operations = [ + migrations.RemoveField( + model_name="ifcomponent", + name="component_ptr", + ), + migrations.AddField( + model_name="switchcomponent", + name="next_components", + field=django.contrib.postgres.fields.ArrayField( + base_field=models.IntegerField(), + default=[], + size=None, + ), + preserve_default=False, + ), + migrations.AlterField( + model_name="markup", + name="parent_component", + field=models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + related_name="markup", + to="component.component", + ), + ), + migrations.AddConstraint( + model_name="switchcomponent", + constraint=models.CheckConstraint( + condition=models.Q(False), + name="check_next_components_length", + ), + ), + migrations.DeleteModel( + name="IfComponent", + ), + ] diff --git a/component/models.py b/component/models.py index 8752ef3..ba84529 100644 --- a/component/models.py +++ b/component/models.py @@ -3,28 +3,6 @@ from component.telegram.models import * -class IfComponent(Component): - """Use this method to create a conditional component. Returns True on success.""" - - def __init__(self, *args: Any, **kwargs: Any) -> None: - super().__init__(*args, **kwargs) - self.type = Component.ComponentType.CONDITIONAL - - class Condition(models.TextChoices): - Equal = "Equal" - Contains = "Contains" - Greater = "Greater" - GreaterEqual = "GreaterEqual" - - expression = models.CharField(max_length=1024, help_text="Expression to evaluate") - condition = models.CharField( - max_length=40, - choices=Condition.choices, - help_text="Condition to evaluate", - ) - is_reverse = models.BooleanField(default=False, help_text="Is reverse?") - - class SwitchComponent(Component): """Use this method to create a switch component. Returns True on success.""" @@ -37,6 +15,34 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: models.CharField(max_length=1024), help_text="Values to evaluate", ) + next_components = ArrayField(models.IntegerField()) + + class Meta: + constraints = [ + models.CheckConstraint( + check=models.Q( + models.functions.Length("next_components") + == models.functions.Length("values"), + ), + name="check_next_components_length", + ), + ] + + def generate_code(self) -> str: + dict_key = "" + code = [ + f"async def {self.code_function_name}(message: Message, **kwargs):", + f" value = message{expression}", + f" match value:", + ] + for i in range(len(self.values)): + value = self.values[i] + next_component = self.next_components[i] + code += [ + f" case {value}:", + f" await {next_component.code_function_name}(message, **kwargs)", + ] + return code class CodeComponent(Component): diff --git a/component/serializers.py b/component/serializers.py index 7fff56a..9ddd3df 100644 --- a/component/serializers.py +++ b/component/serializers.py @@ -5,13 +5,6 @@ from component.telegram.serializers import ModelSerializerCustom -class IfComponentSerializer(ModelSerializerCustom): - class Meta: - model = IfComponent - exclude = ["bot"] - read_only_fields = ["component_type"] - - class SwitchComponentSerializer(ModelSerializerCustom): class Meta: model = SwitchComponent diff --git a/component/urls.py b/component/urls.py index 9168d15..a539bc9 100644 --- a/component/urls.py +++ b/component/urls.py @@ -6,12 +6,11 @@ from component.views import ( CodeComponentSet, ContentTypeListView, - IfComponentSet, MarkupSet, OnCallbackQuerySet, OnMessageSet, - SetStateSet, SchemaListView, + SetStateSet, SwitchComponentSet, ) @@ -19,7 +18,6 @@ router.register(r"on-message", OnMessageSet, basename="on-message") router.register(r"set-state", SetStateSet, basename="set-state") router.register(r"on-callback-query", OnCallbackQuerySet, basename="on-callback-query") -router.register(r"if-component", IfComponentSet, basename="if-component") router.register(r"switch-component", SwitchComponentSet, basename="switch-component") router.register(r"code-component", CodeComponentSet, basename="code-component") router.register(r"markup", MarkupSet, basename="markup") diff --git a/component/views.py b/component/views.py index 6d68dc3..ac593c2 100644 --- a/component/views.py +++ b/component/views.py @@ -7,12 +7,6 @@ from iam.permissions import IsLoginedPermission -class IfComponentSet(ModelViewSetCustom): - permission_classes = [IsLoginedPermission, IsBotOwner] - serializer_class = IfComponentSerializer - queryset = IfComponent.objects.all() - - class SwitchComponentSet(ModelViewSetCustom): permission_classes = [IsLoginedPermission, IsBotOwner] serializer_class = SwitchComponentSerializer