Exemplo n.º 1
0
 def to_json(self, encode=True):
     """Return data prepared for JSON. By default, it returns a JSON encoded
     string, but disabling the encoding to prevent double encoding with
     embedded documents.
     """
     fun = lambda f, v: f.for_json(v)
     data = self._to_fields(fun)
     if encode:
         return json.dumps(data)
     else:
         return data
Exemplo n.º 2
0
    def make_json_ownersafe(cls, doc_dict_or_dicts):
        """This function removes internal fields and handles any steps
        required for making the data stucture (list, dict or Document)
        safe for transmission to the owner of the data.

        It attempts to handle multiple inputs types to avoid as many
        translation steps as possible.
        """
        internal_fields = cls._get_internal_fields()

        def handle_doc(doc_dict):
            # internal_fields is a blacklist
            for f in internal_fields:
                if doc_dict.has_key(f):
                    del doc_dict[f]
            return doc_dict

        trimmed = cls._safe_data_from_input(handle_doc, doc_dict_or_dicts)
        return json.dumps(trimmed)
Exemplo n.º 3
0
    def make_json_publicsafe(cls, doc_dict_or_dicts):
        """This funciton ensures found_data only contains the keys as
        listed in cls._public_fields.

        This function can be safely called without calling make_json_ownersafe
        first because it treats cls._public_fields as a whitelist and
        removes anything not listed.
        """
        if cls._public_fields is None:
            raise DictPunch('make_json_publicsafe called cls with no _public_fields')
        
        def handle_doc(doc_dict):
            # public_fields is a whitelist
            for f in doc_dict.keys():
                if f not in cls._public_fields:
                    del doc_dict[f]
            return doc_dict
        
        trimmed = cls._safe_data_from_input(handle_doc, doc_dict_or_dicts)
        return json.dumps(trimmed)
Exemplo n.º 4
0
 def build_post_policy(self, expiration_time):
     policy = {'expiration': expiration_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
               'conditions': self.conditions,}
     return json.dumps(policy)
Exemplo n.º 5
0
 def make_json_publicsafe(cls, doc_dict_or_dicts):
     """Trims the object using make_publicsafe and dumps to JSON
     """
     trimmed = cls.make_publicsafe(doc_dict_or_dicts)
     return json.dumps(trimmed)
Exemplo n.º 6
0
 def build_post_policy(self, expiration_time):
     policy = {"expiration": expiration_time.strftime("%Y-%m-%dT%H:%M:%SZ"), "conditions": self.conditions}
     return json.dumps(policy)
Exemplo n.º 7
0
 def build_post_policy(self, policy):
     return json.dumps(policy)
Exemplo n.º 8
0
 def to_jsonschema(cls):
     return json.dumps(cls.for_jsonschema())