Better API i hope
This commit is contained in:
@@ -18,6 +18,7 @@ Version: 0.1.0
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -59,7 +60,7 @@ class TranslatableFormField(forms.CharField):
|
||||
|
||||
if row and row[0]:
|
||||
raw_json = row[0]
|
||||
print(f"Form field get_bound_field - got raw JSON for {field_name}: {repr(raw_json)}")
|
||||
logging.debug(f"Form field get_bound_field - got raw JSON for {field_name}: {repr(raw_json)}")
|
||||
|
||||
# Store this for prepare_value to use
|
||||
self._raw_db_value = raw_json
|
||||
@@ -71,11 +72,11 @@ class TranslatableFormField(forms.CharField):
|
||||
'raw_json': raw_json
|
||||
}
|
||||
else:
|
||||
print(f"Form field get_bound_field - no raw data found for {field_name}")
|
||||
logging.debug(f"Form field get_bound_field - no raw data found for {field_name}")
|
||||
self._raw_db_value = None
|
||||
|
||||
except Exception as e:
|
||||
print(f"Form field get_bound_field - DB error: {e}")
|
||||
logging.debug(f"Form field get_bound_field - DB error: {e}")
|
||||
self._raw_db_value = None
|
||||
|
||||
return bound_field
|
||||
@@ -132,17 +133,17 @@ class TranslatableFormField(forms.CharField):
|
||||
|
||||
def prepare_value(self, value):
|
||||
"""Prepare value for widget display - use raw JSON if available"""
|
||||
print(f"Form field prepare_value - received: {repr(value)}, type: {type(value)}")
|
||||
logging.debug(f"Form field prepare_value - received: {repr(value)}, type: {type(value)}")
|
||||
|
||||
# Use raw DB value if we have it
|
||||
if hasattr(self, '_raw_db_value') and self._raw_db_value:
|
||||
try:
|
||||
parsed = json.loads(self._raw_db_value)
|
||||
if isinstance(parsed, dict):
|
||||
print(f"Form field prepare_value - using raw DB dict: {parsed}")
|
||||
logging.debug(f"Form field prepare_value - using raw DB dict: {parsed}")
|
||||
return parsed
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
print(f"Form field prepare_value - raw DB value parse failed")
|
||||
logging.debug(f"Form field prepare_value - raw DB value parse failed")
|
||||
|
||||
# Fallback to original logic
|
||||
if not value:
|
||||
@@ -152,20 +153,20 @@ class TranslatableFormField(forms.CharField):
|
||||
try:
|
||||
parsed = json.loads(value)
|
||||
if isinstance(parsed, dict):
|
||||
print(f"Form field prepare_value - returning parsed dict: {parsed}")
|
||||
logging.debug(f"Form field prepare_value - returning parsed dict: {parsed}")
|
||||
return parsed
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
pass
|
||||
result = {'en': value}
|
||||
print(f"Form field prepare_value - returning english dict: {result}")
|
||||
logging.debug(f"Form field prepare_value - returning english dict: {result}")
|
||||
return result
|
||||
|
||||
if isinstance(value, dict):
|
||||
print(f"Form field prepare_value - returning dict as-is: {value}")
|
||||
logging.debug(f"Form field prepare_value - returning dict as-is: {value}")
|
||||
return value
|
||||
|
||||
result = {'en': str(value)}
|
||||
print(f"Form field prepare_value - returning converted dict: {result}")
|
||||
logging.debug(f"Form field prepare_value - returning converted dict: {result}")
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user