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
1 change: 0 additions & 1 deletion bot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
)
from bot.services import generate_code
from bot.tasks import deploy_bot
from component.models import Component, InlineKeyboardMarkup
from iam.permissions import IsLoginedPermission

logger = logging.getLogger(__name__)
Expand Down
14 changes: 0 additions & 14 deletions component/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@

from component.models import (
CodeComponent,
ForceReply,
InlineKeyboardButton,
InlineKeyboardMarkup,
Keyboard,
KeyboardButton,
Markup,
OnMessage,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
SendDocument,
SendMessage,
SendPhoto,
Expand All @@ -29,13 +22,6 @@
admin.site.register(SendPhoto)
admin.site.register(SendVideo)
admin.site.register(SendDocument)
admin.site.register(Keyboard)
admin.site.register(KeyboardButton)
admin.site.register(InlineKeyboardButton)
admin.site.register(InlineKeyboardMarkup)
admin.site.register(ReplyKeyboardMarkup)
admin.site.register(ReplyKeyboardRemove)
admin.site.register(ForceReply)
admin.site.register(AddStickerToSet)
admin.site.register(CodeComponent)
admin.site.register(Markup)
6 changes: 5 additions & 1 deletion component/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ def validate(self) -> None:
for row in buttons:
assert isinstance(row, list)
for button in row:
assert isinstance(button, str)
assert isinstance(button, dict)
assert "value" in button
assert "next_component" in button
assert isinstance(button["value"], str)
assert isinstance(button["next_component"], int)

def get_callback_data(self, cell: str) -> str:
return f"{self.prent_component.id}-{cell}"
Expand Down
50 changes: 49 additions & 1 deletion component/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.utils.text import slugify
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from component.models import *
from component.telegram.serializers import ModelSerializerCustom
Expand Down Expand Up @@ -79,6 +80,7 @@ class ContentTypeSerializer(serializers.ModelSerializer):
path = serializers.SerializerMethodField()
component_type = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
reply_markup_supported = serializers.SerializerMethodField()

def get_schema(self, obj: ContentType) -> dict:
model_class = obj.model_class()
Expand Down Expand Up @@ -120,6 +122,12 @@ def get_schema(self, obj: ContentType) -> dict:
}
return schema

def get_reply_markup_supported(self, obj: ContentType) -> bool:
model_class = obj.model_class()
if hasattr(model_class, "reply_markup_supported"):
return model_class().reply_markup_supported
return False

def get_path(self, obj: ContentType) -> str:
request = self.context.get("request") # Get the request from context
base_url = request.build_absolute_uri("/")[:-1] if request else "" # Get host
Expand All @@ -139,11 +147,51 @@ def get_component_type(self, obj: ContentType) -> str:

class Meta:
model = ContentType
fields = ["id", "name", "description", "path", "schema", "component_type"]
fields = [
"id",
"name",
"description",
"path",
"schema",
"component_type",
"reply_markup_supported",
]


class MarkupSerializer(ModelSerializerCustom):
class Meta:
model = Markup
fields = "__all__"
read_only_fields = ["component_type"]

def validate_buttons(self, value):
"""Validate that buttons follow the correct structure"""
if not isinstance(value, list):
raise ValidationError("Buttons must be a list")

for row_index, row in enumerate(value):
if not isinstance(row, list):
raise ValidationError(f"Row {row_index} must be a list")

for button_index, button in enumerate(row):
if not isinstance(button, dict):
raise ValidationError(
f"Button at row {row_index}, column {button_index} must be a dictionary",
)

if "value" not in button:
raise ValidationError(
f"Button at row {row_index}, column {button_index} must have a 'value' field",
)

if not isinstance(button["value"], str):
raise ValidationError(
f"Button 'value' at row {row_index}, column {button_index} must be a string",
)

if not isinstance(button["next_component"], int):
raise ValidationError(
f"Button 'next_component' at row {row_index}, column {button_index} must be an integer",
)

return value
Loading