@@ -922,8 +922,9 @@ class ReadOnlyMessageTemplateSerializer(AbilitiesModelSerializer):
922
922
923
923
kind = IntegerChoicesField (choices_class = models .MessageTemplateKindChoices )
924
924
is_default = serializers .SerializerMethodField ()
925
+ raw_blob = serializers .SerializerMethodField ()
925
926
926
- def get_is_default (self , obj ):
927
+ def get_is_default (self , obj ) -> bool :
927
928
"""Get is_default information."""
928
929
maildomain_id = self .context .get ("maildomain_id" )
929
930
mailbox_id = self .context .get ("mailbox_id" )
@@ -936,6 +937,10 @@ def get_is_default(self, obj):
936
937
)
937
938
return template .is_default if template else False
938
939
940
+ def get_raw_blob (self , obj ) -> str | None :
941
+ """Get raw blob."""
942
+ return obj .raw_blob .get_content ().decode ("utf-8" ) if obj .raw_blob else None
943
+
939
944
class Meta :
940
945
model = models .MessageTemplate
941
946
fields = [
@@ -951,7 +956,7 @@ class Meta:
951
956
"created_at" ,
952
957
"updated_at" ,
953
958
]
954
- read_only_fields = ["id" , "created_at" , "updated_at" ]
959
+ read_only_fields = ["id" , "created_at" , "updated_at" , "raw_blob" ]
955
960
956
961
957
962
class MessageTemplateSerializer (AbilitiesModelSerializer ):
@@ -969,6 +974,7 @@ class MessageTemplateSerializer(AbilitiesModelSerializer):
969
974
is_default = serializers .BooleanField (
970
975
required = False , default = False , help_text = "Set as default template"
971
976
)
977
+ raw_blob = serializers .SerializerMethodField ()
972
978
973
979
class Meta :
974
980
model = models .MessageTemplate
@@ -987,7 +993,11 @@ class Meta:
987
993
"maildomain_id" ,
988
994
"is_default" ,
989
995
]
990
- read_only_fields = ["id" , "created_at" , "updated_at" ]
996
+ read_only_fields = ["id" , "created_at" , "updated_at" , "raw_blob" ]
997
+
998
+ def get_raw_blob (self , obj ) -> str | None :
999
+ """Get raw blob."""
1000
+ return obj .raw_blob .get_content ().decode ("utf-8" ) if obj .raw_blob else None
991
1001
992
1002
def validate (self , attrs ):
993
1003
"""Validate template data."""
@@ -1021,8 +1031,17 @@ def create(self, validated_data):
1021
1031
maildomain_id = validated_data .pop ("maildomain_id" , None )
1022
1032
is_default = validated_data .pop ("is_default" , False )
1023
1033
1034
+ # Create raw_blob relationship
1035
+ if self .initial_data .get ("raw_blob" ):
1036
+ blob = models .Blob .objects .create_blob (
1037
+ content = self .initial_data .get ("raw_blob" , "" ).encode ("utf-8" ),
1038
+ content_type = "application/json" ,
1039
+ )
1040
+ validated_data ["raw_blob" ] = blob
1041
+
1024
1042
template = super ().create (validated_data )
1025
1043
1044
+
1026
1045
# Create mailbox relationship
1027
1046
if mailbox_id :
1028
1047
mailbox = models .Mailbox .objects .get (id = mailbox_id )
@@ -1058,8 +1077,21 @@ def update(self, instance, validated_data):
1058
1077
maildomain_id = validated_data .pop ("maildomain_id" , None )
1059
1078
is_default = validated_data .pop ("is_default" , None )
1060
1079
1080
+ if self .initial_data .get ("raw_blob" ):
1081
+ try :
1082
+ if instance .raw_blob :
1083
+ instance .raw_blob .delete ()
1084
+ except models .Blob .DoesNotExist :
1085
+ pass
1086
+ blob = models .Blob .objects .create_blob (
1087
+ content = self .initial_data .get ("raw_blob" , "" ).encode ("utf-8" ),
1088
+ content_type = "application/json" ,
1089
+ )
1090
+ validated_data ["raw_blob" ] = blob
1091
+
1061
1092
template = super ().update (instance , validated_data )
1062
1093
1094
+
1063
1095
# Update relationships if provided
1064
1096
if mailbox_id is not None or maildomain_id is not None :
1065
1097
# Handle mailbox relationship
0 commit comments