|
1 | 1 | # Third party imports |
2 | 2 | from rest_framework import serializers |
| 3 | +import base64 |
3 | 4 |
|
4 | 5 | # Module imports |
5 | 6 | from .base import BaseSerializer |
| 7 | +from plane.utils.content_validator import ( |
| 8 | + validate_binary_data, |
| 9 | + validate_html_content, |
| 10 | + validate_json_content, |
| 11 | +) |
6 | 12 | from plane.db.models import ( |
7 | 13 | Page, |
8 | 14 | PageLog, |
@@ -186,3 +192,71 @@ class Meta: |
186 | 192 | "updated_by", |
187 | 193 | ] |
188 | 194 | read_only_fields = ["workspace", "page"] |
| 195 | + |
| 196 | + |
| 197 | +class PageBinaryUpdateSerializer(serializers.Serializer): |
| 198 | + """Serializer for updating page binary description with validation""" |
| 199 | + |
| 200 | + description_binary = serializers.CharField(required=False, allow_blank=True) |
| 201 | + description_html = serializers.CharField(required=False, allow_blank=True) |
| 202 | + description = serializers.JSONField(required=False, allow_null=True) |
| 203 | + |
| 204 | + def validate_description_binary(self, value): |
| 205 | + """Validate the base64-encoded binary data""" |
| 206 | + if not value: |
| 207 | + return value |
| 208 | + |
| 209 | + try: |
| 210 | + # Decode the base64 data |
| 211 | + binary_data = base64.b64decode(value) |
| 212 | + |
| 213 | + # Validate the binary data |
| 214 | + is_valid, error_message = validate_binary_data(binary_data) |
| 215 | + if not is_valid: |
| 216 | + raise serializers.ValidationError( |
| 217 | + f"Invalid binary data: {error_message}" |
| 218 | + ) |
| 219 | + |
| 220 | + return binary_data |
| 221 | + except Exception as e: |
| 222 | + if isinstance(e, serializers.ValidationError): |
| 223 | + raise |
| 224 | + raise serializers.ValidationError("Failed to decode base64 data") |
| 225 | + |
| 226 | + def validate_description_html(self, value): |
| 227 | + """Validate the HTML content""" |
| 228 | + if not value: |
| 229 | + return value |
| 230 | + |
| 231 | + # Use the validation function from utils |
| 232 | + is_valid, error_message = validate_html_content(value) |
| 233 | + if not is_valid: |
| 234 | + raise serializers.ValidationError(error_message) |
| 235 | + |
| 236 | + return value |
| 237 | + |
| 238 | + def validate_description(self, value): |
| 239 | + """Validate the JSON description""" |
| 240 | + if not value: |
| 241 | + return value |
| 242 | + |
| 243 | + # Use the validation function from utils |
| 244 | + is_valid, error_message = validate_json_content(value) |
| 245 | + if not is_valid: |
| 246 | + raise serializers.ValidationError(error_message) |
| 247 | + |
| 248 | + return value |
| 249 | + |
| 250 | + def update(self, instance, validated_data): |
| 251 | + """Update the page instance with validated data""" |
| 252 | + if "description_binary" in validated_data: |
| 253 | + instance.description_binary = validated_data.get("description_binary") |
| 254 | + |
| 255 | + if "description_html" in validated_data: |
| 256 | + instance.description_html = validated_data.get("description_html") |
| 257 | + |
| 258 | + if "description" in validated_data: |
| 259 | + instance.description = validated_data.get("description") |
| 260 | + |
| 261 | + instance.save() |
| 262 | + return instance |
0 commit comments