Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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",
),
]
50 changes: 28 additions & 22 deletions component/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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):
Expand Down
7 changes: 0 additions & 7 deletions component/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions component/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
from component.views import (
CodeComponentSet,
ContentTypeListView,
IfComponentSet,
MarkupSet,
OnCallbackQuerySet,
OnMessageSet,
SetStateSet,
SchemaListView,
SetStateSet,
SwitchComponentSet,
)

router = DefaultRouter()
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")
Expand Down
6 changes: 0 additions & 6 deletions component/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down