From f78894711c477aa7d5af05f63afa92faf8c44c0e Mon Sep 17 00:00:00 2001 From: elitonzky Date: Tue, 7 Jan 2025 15:13:46 -0300 Subject: [PATCH] feat: add dynamic url to message template button --- .../migrations/0008_templatebutton_example.py | 17 ++++++++++++++++ marketplace/wpp_templates/models.py | 1 + marketplace/wpp_templates/serializers.py | 20 +++++++++++++------ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 marketplace/wpp_templates/migrations/0008_templatebutton_example.py diff --git a/marketplace/wpp_templates/migrations/0008_templatebutton_example.py b/marketplace/wpp_templates/migrations/0008_templatebutton_example.py new file mode 100644 index 00000000..ea74da8c --- /dev/null +++ b/marketplace/wpp_templates/migrations/0008_templatebutton_example.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.4 on 2025-01-07 17:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("wpp_templates", "0007_alter_templatetranslation_message_template_id"), + ] + + operations = [ + migrations.AddField( + model_name="templatebutton", + name="example", + field=models.CharField(default=None, max_length=2000, null=True), + ), + ] diff --git a/marketplace/wpp_templates/models.py b/marketplace/wpp_templates/models.py index baf9e426..a87fe24a 100644 --- a/marketplace/wpp_templates/models.py +++ b/marketplace/wpp_templates/models.py @@ -129,6 +129,7 @@ class TemplateButton(models.Model): country_code = models.IntegerField(null=True) phone_number = models.CharField(max_length=20, null=True) url = models.CharField(max_length=2000, null=True) + example = models.CharField(max_length=2000, default=None, null=True) class TemplateHeader(models.Model): diff --git a/marketplace/wpp_templates/serializers.py b/marketplace/wpp_templates/serializers.py index 48da2993..108eee95 100644 --- a/marketplace/wpp_templates/serializers.py +++ b/marketplace/wpp_templates/serializers.py @@ -40,10 +40,18 @@ class ButtonSerializer(serializers.ModelSerializer): country_code = serializers.CharField(required=False) phone_number = serializers.CharField(required=False) url = serializers.CharField(required=False) + example = serializers.ListField(required=False) class Meta: model = TemplateButton - fields = ["button_type", "text", "country_code", "phone_number", "url"] + fields = [ + "button_type", + "text", + "country_code", + "phone_number", + "url", + "example", + ] class TemplateTranslationSerializer(serializers.Serializer): @@ -81,17 +89,15 @@ def create(self, validated_data: dict) -> None: components = [validated_data.get("body", {})] header = validated_data.get("header") + # Process Header if header: header = dict(header) header["type"] = "HEADER" header["format"] = header.get("header_type", "TEXT") header.pop("header_type") - if ( - header.get("format") == "IMAGE" - or header.get("format") == "DOCUMENT" - or header.get("format") == "VIDEO" - ): + # Handle Media Uploads + if header.get("format") in ["IMAGE", "DOCUMENT", "VIDEO"]: photo_api_request = PhotoAPIService(client=FacebookClient(access_token)) photo = header.get("example") file_type = re.search("(?<=data:)(.*)(?=;base64)", photo).group(0) @@ -113,6 +119,7 @@ def create(self, validated_data: dict) -> None: components = self.append_to_components(components, validated_data.get("footer")) buttons = validated_data.get("buttons", {}) + # Process Buttons buttons_component = { "type": "BUTTONS", "buttons": [], @@ -121,6 +128,7 @@ def create(self, validated_data: dict) -> None: for button in buttons: button = dict(button) button["type"] = button.get("button_type") + if button.get("phone_number"): button[ "phone_number"