This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
118 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,7 @@ class index(MetadataDictDirective): | |
'binary', | ||
'object', | ||
'float', | ||
'nested', | ||
'path' | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- encoding: utf-8 -*- | ||
from zope.schema._bootstrapfields import Field | ||
from jsonschema import validate, ValidationError | ||
from zope.schema.interfaces import WrongType | ||
from zope.schema.interfaces import WrongContainedType | ||
from zope.event import notify | ||
from zope.interface import implementer | ||
from plone.server.interfaces import IJSONField | ||
|
||
import json | ||
|
||
|
||
@implementer(IJSONField) | ||
class JSONField(Field): | ||
|
||
def __init__(self, schema, **kw): | ||
if not isinstance(schema, str): | ||
raise WrongType | ||
|
||
try: | ||
self.schema = json.loads(schema) | ||
except ValueError: | ||
raise WrongType | ||
super(JSONField, self).__init__(**kw) | ||
|
||
def _validate(self, value): | ||
super(JSONField, self)._validate(value) | ||
|
||
try: | ||
validate(value, self.schema) | ||
except ValidationError as e: | ||
raise WrongContainedType(e.message, self.__name__) | ||
|
||
def set(self, object, value): | ||
# Announce that we're going to assign the value to the object. | ||
# Motivation: Widgets typically like to take care of policy-specific | ||
# actions, like establishing location. | ||
event = BeforeJSONAssignedEvent(value, self.__name__, object) | ||
notify(event) | ||
# The event subscribers are allowed to replace the object, thus we need | ||
# to replace our previous value. | ||
value = event.object | ||
super(JSONField, self).set(object, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters