aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db/extratypes.py
diff options
context:
space:
mode:
authorRodney Ewing <ewing.rj@gmail.com>2013-08-27 11:40:35 -0700
committerRodney Ewing <ewing.rj@gmail.com>2013-09-19 08:12:09 -0700
commit7f9d3ca7c9e4934c3ee487a0f1d10e1b6525db71 (patch)
tree88d2fbf569bbf78fbbb54cfcef554b5a9d7b7ac7 /mediagoblin/db/extratypes.py
parent755b6a86b6784ca5b1e8d427ce6e07442d16c974 (diff)
downloadmediagoblin-7f9d3ca7c9e4934c3ee487a0f1d10e1b6525db71.tar.lz
mediagoblin-7f9d3ca7c9e4934c3ee487a0f1d10e1b6525db71.tar.xz
mediagoblin-7f9d3ca7c9e4934c3ee487a0f1d10e1b6525db71.zip
need to use mutation tracking to detect changes in JSONEncoded types
Diffstat (limited to 'mediagoblin/db/extratypes.py')
-rw-r--r--mediagoblin/db/extratypes.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/mediagoblin/db/extratypes.py b/mediagoblin/db/extratypes.py
index f2304af0..8e04d58d 100644
--- a/mediagoblin/db/extratypes.py
+++ b/mediagoblin/db/extratypes.py
@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from sqlalchemy.ext.mutable import Mutable
from sqlalchemy.types import TypeDecorator, Unicode, TEXT
import json
@@ -38,7 +39,7 @@ class PathTupleWithSlashes(TypeDecorator):
return value
-# The following class and only this one class is in very
+# The following two classes and only these two classes is in very
# large parts based on example code from sqlalchemy.
#
# The original copyright notice and license follows:
@@ -61,3 +62,30 @@ class JSONEncoded(TypeDecorator):
if value is not None:
value = json.loads(value)
return value
+
+
+class MutationDict(Mutable, dict):
+ @classmethod
+ def coerce(cls, key, value):
+ "Convert plain dictionaries to MutationDict."
+
+ if not isinstance(value, MutationDict):
+ if isinstance(value, dict):
+ return MutationDict(value)
+
+ # this call will raise ValueError
+ return Mutable.coerce(key, value)
+ else:
+ return value
+
+ def __setitem__(self, key, value):
+ "Detect dictionary set events and emit change events."
+
+ dict.__setitem__(self, key, value)
+ self.changed()
+
+ def __delitem__(self, key):
+ "Detect dictionary del events and emit change events."
+
+ dict.__delitem__(self, key)
+ self.changed()