def test_indent(self): h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', {'nifty': 87}, {'field': 'yes', 'morefield': False} ] expect = textwrap.dedent("""\ [ [ "blorpie" ], [ "whoops" ], [], "d-shtaeou", "d-nthiouh", "i-vhbjkhnth", { "nifty": 87 }, { "field": "yes", "morefield": false } ]""") d1 = json.dumps(h) d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) h1 = json.loads(d1) h2 = json.loads(d2) self.assertEquals(h1, h) self.assertEquals(h2, h) self.assertEquals(d2, expect)
def validation_errors(self, REQUEST): """check submitted values """ errors = self.validateInputs(REQUEST) if len(errors) > 0: return self.errors_json(errors=json.dumps({ 'success': False, 'errors': errors })) else: return self.errors_json(errors=json.dumps({'success': True}))
def validation_errors(self, REQUEST): """check submitted values """ db = self.getParentDatabase() form = db.getForm(REQUEST.get('Form')) errors=form.validateInputs(REQUEST, doc=self) if len(errors)>0: return self.errors_json(errors=json.dumps({'success': False,'errors':errors})) else: return self.errors_json(errors=json.dumps({'success': True}))
def validation_errors(self, REQUEST): """ Check submitted values. """ db = self.getParentDatabase() form = db.getForm(REQUEST.get('Form')) errors = form.validateInputs(REQUEST, doc=self) if errors: return self.errors_json( errors=json.dumps({'success': False, 'errors': errors})) else: return self.errors_json( errors=json.dumps({'success': True}))
def tojson(self, REQUEST=None, item=None, formid=None, rendered=False): """return item value as JSON (return all items if item=None) """ if not self.isReader(): raise Unauthorized, "You cannot read this content" datatables_format = False if REQUEST: REQUEST.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') item = REQUEST.get('item', item) formid = REQUEST.get('formid', formid) rendered_str = REQUEST.get('rendered', None) if rendered_str: rendered = True datatables_format_str = REQUEST.get('datatables', None) if datatables_format_str: datatables_format = True if not item: return json.dumps(self.items.data) if not formid: form = self.getForm() else: form = self.getParentDatabase().getForm(formid) if form: field = form.getFormField(item) if field: if field.getFieldType() == 'DATAGRID': adapt = field.getSettings() fieldvalue = adapt.getFieldValue(form, self, False, False, REQUEST) fieldvalue = adapt.rows(fieldvalue, rendered=rendered) if datatables_format: fieldvalue = { 'iTotalRecords': len(fieldvalue), 'aaData': fieldvalue } else: if rendered: fieldvalue = self.getRenderedItem(item, form) else: adapt = field.getSettings() fieldvalue = adapt.getFieldValue( form, self, False, False, REQUEST) else: fieldvalue = self.getItem(item) else: fieldvalue = self.getItem(item) return json.dumps(fieldvalue)
def test_dictrecursion(self): x = {} x["test"] = x try: json.dumps(x) except ValueError: pass else: self.fail("didn't raise ValueError on dict recursion") x = {} y = {"a": x, "b": x} # ensure that the marker is cleared json.dumps(x)
def getRenderedFields(self, editmode=True, creation=False, request={}): """ Return an array of rows rendered using the associated form fields """ if not self.field_mapping: return [] db = self.context.getParentDatabase() mapped_fields = [f.strip() for f in self.field_mapping.split(',')] #get associated form id child_form_id = self.associated_form if not child_form_id: return mapped_fields # get associated form object child_form = db.getForm(child_form_id) if not child_form: return mapped_fields target = TemporaryDocument(db, child_form, request, validation_mode=False).__of__(db) # return rendered field for each mapped field if this one exists in the child form child_form_fields = [ f.getFieldRender(child_form, target, editmode=editmode, creation=creation, request=request) for f in [child_form.getFormField(f) for f in mapped_fields] if f ] return json.dumps(child_form_fields)
def tojson(self, selectionlist): """Return a JSON table storing documents to be displayed """ if self.sourceview: sourceview = self.context.getParentDatabase().getView( self.sourceview) brains = sourceview.getAllDocuments(getObject=False) columns = [ col for col in sourceview.getColumns() if not (col.getHiddenColumn()) ] column_ids = [col.id for col in columns] datatable = [] for b in brains: row = [b.id] for col in column_ids: v = getattr(b, sourceview.getIndexKey(col)) if not isinstance(v, str): v = unicode(v).encode('utf-8').replace('\r', '') row.append(v or ' ') datatable.append(row) else: datatable = [v.split('|')[::-1] for v in selectionlist] return json.dumps(datatable)
def childDocument(self, doc): """ """ parent_form = self.getParentDatabase().getForm(doc.Plomino_Parent_Form) parent_field = parent_form.getFormField(doc.Plomino_Parent_Field) fields = parent_field.getSettings().field_mapping.split(',') raw_values = [] for f in fields: v = doc.getItem(f) if hasattr(v, 'strftime'): raw_values.append( DateToString(doc.getItem(f), self.getParentDatabase().getDateTimeFormat())) else: raw_values.append(v) html = """<div id="raw_values">%s</div>""" % json.dumps(raw_values) html = html + """<div id="parent_field">%s</div>""" % doc.Plomino_Parent_Field for f in fields: html = html + """<span id="%s" class="plominochildfield">%s</span>""" % ( f, doc.getRenderedItem(f, form=self)) return html
def tojson(self, REQUEST=None, item=None): """return field value as JSON, return all fields values if item=None (Note: we use 'item' instead of 'field' to match the PlominoDocument.tojson method signature) """ if REQUEST: REQUEST.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') item = REQUEST.get('item', item) result = None if not item: fields = self.getFormFields() result = {} for field in fields: adapt = field.getSettings() fieldvalue = adapt.getFieldValue(self, None, False, False, REQUEST) result[field.id] = fieldvalue else: field = self.getFormField(item) if field: adapt = field.getSettings() result = adapt.getFieldValue(self, None, False, False, REQUEST) return json.dumps(result)
def request_items_aoData(self, request): """ Return a string representing REQUEST.items as aoData.push calls. """ aoData_templ = "aoData.push(%s); " aoDatas = [] for k, v in request.form.items(): j = json.dumps({'name': k, 'value': v}) aoDatas.append(aoData_templ % j) return '\n'.join(aoDatas)
def getFilteredNames(self, filter): """ Return a JSON list of users, filtered by id or name. """ names_ids = self._getNamesIds() if filter: names_ids = [ (username, userid) for (username, userid) in names_ids if filter in username or filter in userid] return json.dumps(names_ids)
def tojson(self, value, rendered=False): """ """ if not value: return "[]" if type(value) is dict: if rendered and 'rendered' in value: value = value['rendered'] if not(rendered) and 'rawdata' in value: value = value['rawdata'] return json.dumps(value)
def test_dont_Recordify_results(self): """ _request() is required to return the exact string that the HTTP server sent to it -- no transforming it, such as by json-decoding and then constructing a Record. """ EXAMPLE_RECORD_JSONSTR=json.dumps({ 'geometry' : { 'type' : 'Point', 'coordinates' : [D('10.0'), D('11.0')] }, 'id' : 'my_id', 'type' : 'Feature', 'properties' : { 'key' : 'value' , 'type' : 'object' } }) mockhttp = mock.Mock() mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, EXAMPLE_RECORD_JSONSTR) self.client.http = mockhttp res = self.client._request("http://thing", 'POST') self.failUnlessEqual(res, EXAMPLE_RECORD_JSONSTR)
def getFilteredNames(self, filter): """ Return a JSON list of users, filtered by id or name. """ names_ids = self._getNamesIds() if filter: names_ids = [ {'id': userid, 'text': username} for (username, userid) in names_ids[:20] if filter.lower() in username.lower() or filter.lower() in userid.lower()] return json.dumps( {'results': names_ids, 'total': len(names_ids)})
def runAgent(self, *args, **kwargs): """ Execute the agent formula. """ plominoContext = self plominoReturnURL = self.getParentDatabase().absolute_url() request = getattr(self, 'REQUEST', None) if request: alsoProvides(request, IDisableCSRFProtection) try: if self.run_as == "OWNER": # Remember the current user member = self.getCurrentMember() if member.__class__.__name__ == "SpecialUser": user = member else: user = member.getUser() # Switch to the agent's owner owner = self.getOwner() newSecurityManager(None, owner) result = self.runFormulaScript( SCRIPT_ID_DELIMITER.join(["agent", self.id]), plominoContext, self.content, True, *args ) # Switch back to the original user if self.run_as == "OWNER": newSecurityManager(None, user) if request: if request.get('REDIRECT', False): # result is supposed to be an URL plominoReturnURL = result or plominoReturnURL request.RESPONSE.redirect(plominoReturnURL) if "application/json" in request.getHeader('Accept', ''): # result will be serialized in JSON return json.dumps(result) except PlominoScriptException, e: # Exception logged already in runFormulaScript if request and request.get('RESPONSE'): request.RESPONSE.setHeader( 'content-type', 'text/plain; charset=utf-8') return e.message
def addField(self): # specific field settings are managed as instance behaviors (using # collective.instancebehavior), but it is not supported by # plone.restapi, so we implement our own endpoint to create fields. self.request.RESPONSE.setHeader('content-type', 'text/plain; charset=utf-8') if self.request.method == "POST": alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection) data = json.loads(self.request.BODY) newfield = api.content.create( container=self.context, type="PlominoField", title=data['title'], ) return json.dumps({'created': newfield.id})
def addField(self): # specific field settings are managed as instance behaviors (using # collective.instancebehavior), but it is not supported by # plone.restapi, so we implement our own endpoint to create fields. self.request.RESPONSE.setHeader( 'content-type', 'text/plain; charset=utf-8') if self.request.method == "POST": alsoProvides( self.request, plone.protect.interfaces.IDisableCSRFProtection) data = json.loads(self.request.BODY) newfield = api.content.create( container=self.context, type="PlominoField", title=data['title'], ) return json.dumps({'created': newfield.id})
def getJQueryColumns(self): """Returns a JSON representation of columns headers, designed for JQuery DataTables """ if self.sourceview is not None: sourceview = self.context.getParentDatabase().getView( self.sourceview) columns = [ col for col in sourceview.getColumns() if not (col.getHiddenColumn()) ] column_labels = [col.Title() for col in columns] else: column_labels = [""] column_dicts = [{"sTitle": col} for col in column_labels] column_dicts.insert(0, {"bVisible": False, "bSearchable": False}) return json.dumps(column_dicts)
def getJQueryColumns(self): """ Returns a JSON representation of columns headers. Designed for JQuery DataTables. """ if self.sourceview is not None: sourceview = self.context.getParentDatabase().getView( self.sourceview) columns = [col for col in sourceview.getColumns() if not(col.getHiddenColumn())] column_labels = [col.Title() for col in columns] else: column_labels = [""] column_dicts = [{"sTitle": col} for col in column_labels] column_dicts.insert( 0, {"bVisible": False, "bSearchable": False}) return json.dumps(column_dicts)
def getFilteredNames(self, filter): """Return a JSON list of users, filtered by id or name. """ if filter: if self.restricttogroup and self.restricttogroup != '': group = self.context.portal_groups.getGroupById(self.restricttogroup) if group is not None: all = [(m.getProperty('id'), m.getProperty("fullname")) for m in group.getGroupMembers()] else: all = [] elif not(self.context.getParentDatabase().getDoNotListUsers()): all = [(m.getId(), m.getProperty("fullname")) for m in self.context.getPortalMembers()] else: all = [] else: all = [] result = [l for l in all if filter in l[0] or filter in l[1]] result.sort() return json.dumps(result)
def tojson(self, selectionlist): """Return a JSON table storing documents to be displayed """ if self.context.sourceview: sourceview = self.context.getParentDatabase().getView( self.sourceview) brains = sourceview.getAllDocuments(getObject=False) columns = [col for col in sourceview.getColumns() if not col.hidden_column] column_ids = [col.id for col in columns] datatable = [] for b in brains: row = [b.id] for col in column_ids: v = getattr(b, sourceview.getIndexKey(col)) if not isinstance(v, str): v = unicode(v).encode('utf-8').replace('\r', '') row.append(v or ' ') datatable.append(row) else: datatable = [v.split('|')[::-1] for v in selectionlist] return json.dumps(datatable)
def getRenderedFields(self, editmode=True, creation=False, request={}): """ Return an array of rows rendered using the associated form fields """ if not self.context.field_mapping: return [] db = self.context.getParentDatabase() mapped_fields = [f.strip() for f in self.context.field_mapping.split(',')] # get associated form id child_form_id = self.context.associated_form if not child_form_id: return mapped_fields # get associated form object child_form = db.getForm(child_form_id) if not child_form: return mapped_fields target = TemporaryDocument( db, child_form, request, validation_mode=False).__of__(db) # return rendered field for each mapped field if this one exists in the # child form child_form_fields = [f.getFieldRender( child_form, target, editmode=editmode, creation=creation, request=request ) for f in [child_form.getFormField(f) for f in mapped_fields] if f] return json.dumps(child_form_fields)
def test_listrecursion(self): x = [] x.append(x) try: json.dumps(x) except ValueError: pass else: self.fail("didn't raise ValueError on list recursion") x = [] y = [x] x.append(y) try: json.dumps(x) except ValueError: pass else: self.fail("didn't raise ValueError on alternating list recursion") y = [] x = [y, y] # ensure that the marker is cleared json.dumps(x)
def tojson(self, REQUEST=None, item=None, formid=None, rendered=False, lastmodified=None): """ Return item value as JSON. Return all items if `item=None`. Values on the REQUEST overrides parameters. If the requested item corresponds to a field on the found form, the field value is returned. If not, it falls back to a plain item lookup on the document. `formid="None"` specifies plain item lookup. """ # TODO: Don't always return the entire dataset: allow batching. if not self.isReader(): raise Unauthorized, "You cannot read this content" datatables_format = False if REQUEST: REQUEST.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') item = REQUEST.get('item', item) formid = REQUEST.get('formid', formid) lastmodified = REQUEST.get('lastmodified', lastmodified) rendered_str = REQUEST.get('rendered', None) if rendered_str: rendered = True datatables_format_str = REQUEST.get('datatables', None) if datatables_format_str: datatables_format = True if item: if formid == "None": form = None elif formid: form = self.getParentDatabase().getForm(formid) else: form = self.getForm() if form: field = form.getFormField(item) if field: if field.getFieldType() == 'DATAGRID': adapt = field.getSettings() fieldvalue = adapt.getFieldValue(form, doc=self, request=REQUEST) fieldvalue = adapt.rows(fieldvalue, rendered=rendered) else: if rendered: fieldvalue = self.getRenderedItem(item, form) else: adapt = field.getSettings() fieldvalue = adapt.getFieldValue(form, doc=self, request=REQUEST) else: _logger.info("Failed to find %s on %s, " "fallback to getItem." % (item, form.id)) fieldvalue = self.getItem(item) else: fieldvalue = self.getItem(item) data = fieldvalue else: data = self.items.data if datatables_format: data = { 'iTotalRecords': len(data), 'iTotalDisplayRecords': len(data), 'aaData': data } if lastmodified: data = {'lastmodified': self.getLastModified(), 'data': data} return json.dumps(data)
def test_encoding2(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' s = u.encode('utf-8') ju = json.dumps(u, encoding='utf-8') js = json.dumps(s, encoding='utf-8') self.assertEquals(ju, js)
def tojson(self, value, rendered=False): """ """ rows = self.rows(value, rendered) return json.dumps(rows)
def tojson(self, REQUEST=None): """ Returns a JSON representation of view data """ data = [] categorized = self.getCategorized() start = 1 limit = -1 search = None sort_index = None reverse = None if REQUEST: start = int(REQUEST.get('iDisplayStart', 1)) iDisplayLength = REQUEST.get('iDisplayLength', None) if iDisplayLength: limit = int(iDisplayLength) search = REQUEST.get('sSearch', '').lower() if search: search = ' '.join([term + '*' for term in search.split(' ')]) sort_column = REQUEST.get('iSortCol_0') if sort_column: sort_index = self.getIndexKey( self.getColumns()[int(sort_column) - 1].id) reverse = REQUEST.get('sSortDir_0', None) if reverse == 'desc': reverse = 0 if reverse == 'asc': reverse = 1 if limit < 1: limit = None results = self.getAllDocuments(start=start, limit=limit, getObject=False, fulltext_query=search, sortindex=sort_index, reverse=reverse) total = display_total = len(results) columnids = [ col.id for col in self.getColumns() if not getattr(col, 'HiddenColumn', False) ] for b in results: row = [b.getPath().split('/')[-1]] for colid in columnids: v = getattr(b, self.getIndexKey(colid), '') if isinstance(v, list): v = [ asUnicode(e).encode('utf-8').replace('\r', '') for e in v ] else: v = asUnicode(v).encode('utf-8').replace('\r', '') row.append(v or ' ') if categorized: for cat in asList(row[1]): entry = [c for c in row] entry[1] = cat data.append(entry) else: data.append(row) return json.dumps({ 'iTotalRecords': total, 'iTotalDisplayRecords': display_total, 'aaData': data })
def test_big_unicode_encode(self): u = u'\U0001d120' self.assertEquals(json.dumps(u), '"\\ud834\\udd20"') self.assertEquals(json.dumps(u, ensure_ascii=False), u'"\U0001d120"')
def tojson(self, selection): """Return a JSON table storing documents to be displayed """ return json.dumps([v.split('|')[::-1] for v in selection])
def json(self): """ Returns a JSON representation of view data """ data = [] categorized = self.context.categorized pagenumber = int(self.request.get('pagenumber', '1')) pagesize = self.request.get('pagesize', None) if pagesize: pagesize = int(pagesize) search = self.request.get('search', '').lower() if search: search = ' '.join([term + '*' for term in search.split(' ')]) sort_column = self.request.get('sorton') if sort_column: sort_index = self.context.getIndexKey(sort_column) else: sort_index = None reverse = int(self.request.get('reverse', '0')) if 'request_query' in self.request: # query parameter in self.request is supposed to be a json object request_query = self.context.__query_loads__( self.request['request_query']) else: request_query = None results = self.context.getAllDocuments(pagenumber=pagenumber, pagesize=pagesize, getObject=False, fulltext_query=search, sortindex=sort_index, reverse=reverse, request_query=request_query) total = len(results) if pagesize: display_total = results.items_on_page else: display_total = total columns = [ column for column in self.context.getColumns() if not column.hidden_column ] for brain in results: row = [ brain.id, ] for column in columns: column_value = getattr(brain, self.context.getIndexKey(column.id), '') rendered = column.getColumnRender(column_value) if isinstance(rendered, list): rendered = [ asUnicode(e).encode('utf-8').replace('\r', '') for e in rendered ] else: rendered = asUnicode(rendered).encode('utf-8').replace( '\r', '') row.append(rendered or ' ') if categorized: for cat in asList(row[1]): entry = [c for c in row] entry[1] = cat data.append(entry) else: data.append(row) self.request.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') return json.dumps({ 'total': total, 'displayed': display_total, 'rows': data })
def tree(self): database = self.context.getParentDatabase() # Create form tree forms = [] for form in database.getForms(): fields = [] for field in form.getFormFields(): fields.append({ "label": field.title, "url": field.absolute_url(), "type" : 'PlominoField' }) plomino_form = [] plomino_form.append({ "label" : "Fields", "folder" : True, "children" : fields, "type" : "PlominoField", "url" : form.absolute_url() }) actions = [] for action in form.getFormActions(): actions.append({ "label": action.title, 'type' : 'PlominoAction', "url" : action.absolute_url() }) plomino_form.append({ "label": "Actions", "folder": True, "children": actions, "type" : "PlominoAction", "url" : form.absolute_url() }) hide_whens = [] for hide_when in form.getHidewhenFormulas(): hide_whens.append({ "label": hide_when.title, 'type' : 'PlominoHidewhen', "url" : hide_when.absolute_url() }) plomino_form.append({ "label": "Hide Whens", "folder": True, "children": hide_whens, "type" : "PlominoHidewhen", "url" : form.absolute_url() }) forms.append({ "label": form.title, "folder": True, "children": plomino_form, "type" : "PlominoForm", "url" : form.absolute_url(), }) # Create Views Tree views = [] for view in database.getViews(): plomino_view = [] actions = [] for action in view.getActions(): # view.getActions() returns tuples actions.append({ "label": action[0].title, "type": 'PlominoAction', "url": action[0].absolute_url() }) plomino_view.append({ "label": "Actions", "folder": True, "children": actions, "type": "PlominoAction", "url": view.absolute_url() }) columns = [] for column in view.getColumns(): columns.append({ "label": column.title, "type": "PlominoColumn", "url": column.absolute_url() }) plomino_view.append({ "label": "Columns", "folder": True, "children": columns, "type": "PlominoColumn", "url": view.absolute_url() }) views.append({ "label": view.title, "type": "PlominoView", "children": plomino_view, "url": view.absolute_url(), }) # Create Agents View agents = [] for agent in database.getAgents(): agents.append({ "label" : agent.title, "type" : "PlominoAgent", "url" : agent.absolute_url() }) # Build the final element tree elements = [ { "label": "Forms", "folder": True, "children": forms, "type" : 'PlominoForm', "url" : database.absolute_url() }, { "label": "Views", "folder": True, "children": views, "type" : 'PlominoView', "url" : database.absolute_url() }, { "label": "Agents", "folder": True, "children": agents , "type" : 'PlominoAgent', "url" : database.absolute_url() } ] self.request.RESPONSE.setHeader( 'content-type', 'application/json; charset=utf-8') return json.dumps(elements)
def json_dumps(data): return json.dumps(data)
def tojson(self, REQUEST=None): """ Returns a JSON representation of view data """ data = [] categorized = self.getCategorized() start = 1 limit = -1 search = None sort_index = None reverse = None if REQUEST: REQUEST.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') start = int(REQUEST.get('iDisplayStart', 1)) iDisplayLength = REQUEST.get('iDisplayLength', None) if iDisplayLength: limit = int(iDisplayLength) search = REQUEST.get('sSearch', '').lower() if search: search = ' '.join([term + '*' for term in search.split(' ')]) sort_column = REQUEST.get('iSortCol_0') if sort_column: sort_index = self.getIndexKey( self.getColumns()[int(sort_column) - 1].id) reverse = REQUEST.get('sSortDir_0', None) if reverse == 'desc': reverse = 0 if reverse == 'asc': reverse = 1 if limit < 1: limit = None if not REQUEST is None and 'request_query' in REQUEST: # query parameter in REQUEST is supposed to be a json object request_query = self.__query_loads__(REQUEST['request_query']) else: request_query = None results = self.getAllDocuments(start=start, limit=limit, getObject=False, fulltext_query=search, sortindex=sort_index, reverse=reverse, request_query=request_query) total = display_total = len(results) columns = [ column for column in self.getColumns() if not getattr(column, 'HiddenColumn', False) ] for brain in results: row = [brain.getPath().split('/')[-1]] for column in columns: column_value = getattr(brain, self.getIndexKey(column.id), '') rendered = column.getColumnRender(column_value) if isinstance(rendered, list): rendered = [ asUnicode(e).encode('utf-8').replace('\r', '') for e in rendered ] else: rendered = asUnicode(rendered).encode('utf-8').replace( '\r', '') row.append(rendered or ' ') if categorized: for cat in asList(row[1]): entry = [c for c in row] entry[1] = cat data.append(entry) else: data.append(row) return json.dumps({ 'iTotalRecords': total, 'iTotalDisplayRecords': display_total, 'aaData': data })
def _extended_json_encoding(obj): if isinstance(obj, DateTime): return {'<datetime>': True, 'datetime': obj.ISO()} return json.dumps(obj)
def test_floats(self): for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100]: self.assertEquals(float(json.dumps(num)), num)
def test_encoding4(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' j = json.dumps([u]) self.assertEquals(j, '["\\u03b1\\u03a9"]')
if getattr(hidewhen, 'isDynamicHidewhen', False): try: isHidden = self.runFormulaScript( "hidewhen_" + self.id + "_" + hidewhen.id + "_formula", target, hidewhen.Formula) except PlominoScriptException, e: e.reportError('%s hide-when formula failed' % hidewhen.id) #if error, we hide anyway isHidden = True result[hidewhen.id] = isHidden for subformname in self.getSubforms(): form = self.getParentDatabase().getForm(subformname) form_hidewhens = json.loads(form.getHidewhenAsJSON(REQUEST)) result.update(form_hidewhens) return json.dumps(result) security.declareProtected(READ_PERMISSION, 'applyCache') def applyCache(self, html_content, doc=None): """ Evaluate cache formula and return resulting layout """ to_be_cached = {} for cacheformula in self.getCacheFormulas(): cacheid = cacheformula.id try: if doc is None: target = self else: target = doc
def code(self): if self.request.method == "GET": self.request.RESPONSE.setHeader('content-type', 'text/plain; charset=utf-8') type = self.request.form.keys()[0] if type not in [ "Form", "FormField", "FormAction", "FormHidewhen", "View", "ViewAction", "ViewColumn", "Agent" ]: return "Parameter error" element = self.getElementByType(type, self.request.form[type]) if not element: return "Name error" if type == "Agent": return json.dumps({"code": element.content, "methods": []}) if type == "FormHidewhen": return json.dumps({"code": element.formula, "methods": []}) methods = self.getMethods(type) code = "" for method in self.getMethodsId(type): formula = getattr(element, method, None) if formula: code += "## START " + method + " {\n" code += formula code += "\n## END " + method + " }\n\r" elements = {"code": code, "methods": methods} return json.dumps(elements) if self.request.method == "POST": alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection) self.request.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') response = json.loads(self.request.BODY) type = response["Type"] id = response["Id"] code = response["Code"] if type == "Agent": self.context.getAgent(id).content = code return json.dumps({"type": "OK"}) if type == "FormHidewhen": id = id.split('/') self.context.getForm(id[0]).getHidewhen(id[1]).formula = code return json.dumps({"type": "OK"}) methodList = self.getMethodsId(type) content = "" contents = [] inside = False for lineNumber, line in enumerate(code.split('\n')): start_reg = re.match(r'^##\s*START\s+(.*){$', line) end_reg = re.match(r'^##\s*END\s+(.*)}$', line) if start_reg and not inside: if start_reg.group(1).strip() in methodList: methodName = start_reg.group(1).strip() inside = True else: return json.dumps({ "type": "Error", "error": "Method \"" + start_reg.group(1).strip() + "\" doesn't exists", "line": lineNumber + 1 }) elif end_reg and inside: if end_reg.group(1).strip() != methodName: return json.dumps({ "type": "Error", "error": "END tag doesn't match START tag", "line": lineNumber + 1 }) contents.append({"name": methodName, "code": content}) inside = False content = '' elif not start_reg and not end_reg and inside: content += line + "\n" elif end_reg and not inside: return json.dumps({ "type": "Error", "error": "Unexpected END tag", "line": lineNumber + 1 }) elif start_reg and inside: return json.dumps({ "type": "Error", "error": "Unexpected START tag", "line": lineNumber + 1 }) element = self.getElementByType(type, id) for formula in methodList: setattr(element, formula, '') for formula in contents: setattr(element, formula['name'], formula['code'].rstrip()) return json.dumps({"type": "OK"})
def test_encode(self): self.failUnlessEqual(jsonutil.dumps(zero_point_one), "0.1")
def tojson( self, REQUEST=None, item=None, formid=None, rendered=False, lastmodified=None ): """ Return item value as JSON. Return all items if `item=None`. Values on the REQUEST overrides parameters. If the requested item corresponds to a field on the found form, the field value is returned. If not, it falls back to a plain item lookup on the document. `formid="None"` specifies plain item lookup. """ # TODO: Don't always return the entire dataset: allow batching. if not self.isReader(): raise Unauthorized("You cannot read this content") datatables_format = False if REQUEST: REQUEST.RESPONSE.setHeader( 'content-type', 'application/json; charset=utf-8') item = REQUEST.get('item', item) formid = REQUEST.get('formid', formid) lastmodified = REQUEST.get('lastmodified', lastmodified) rendered_str = REQUEST.get('rendered', None) if rendered_str: rendered = True datatables_format_str = REQUEST.get('datatables', None) if datatables_format_str: datatables_format = True if item: if formid == "None": form = None elif formid: form = self.getParentDatabase().getForm(formid) else: form = self.getForm() if form: field = form.getFormField(item) if field: if field.field_type == 'DATAGRID': adapt = field.getSettings() fieldvalue = adapt.getFieldValue( form, doc=self, request=REQUEST) fieldvalue = adapt.rows( fieldvalue, rendered=rendered) else: if rendered: fieldvalue = self.getRenderedItem(item, form) else: adapt = field.getSettings() fieldvalue = adapt.getFieldValue( form, doc=self, request=REQUEST) else: fieldvalue = self.getItem(item) else: fieldvalue = self.getItem(item) data = fieldvalue else: data = self.items.data if datatables_format: data = {'iTotalRecords': len(data), 'iTotalDisplayRecords': len(data), 'aaData': data} if lastmodified: data = {'lastmodified': self.getLastModified(), 'data': data} return json.dumps(data)
def test_parse(self): # test in/out equivalence and parsing res = json.loads(JSON) out = json.dumps(res) self.assertEquals(res, json.loads(out))
def tojson(self, selection): """ Return a JSON table storing documents to be displayed """ return json.dumps([v.split('|')[::-1] for v in selection])
def tojson(self, REQUEST=None): """ Returns a JSON representation of view data """ data = [] categorized = self.getCategorized() start = 1 limit = -1 search = None sort_index = None reverse = None if REQUEST: REQUEST.RESPONSE.setHeader( 'content-type', 'application/json; charset=utf-8') start = int(REQUEST.get('iDisplayStart', 1)) iDisplayLength = REQUEST.get('iDisplayLength', None) if iDisplayLength: limit = int(iDisplayLength) search = REQUEST.get('sSearch', '').lower() if search: search = ' '.join([term+'*' for term in search.split(' ')]) sort_column = REQUEST.get('iSortCol_0') if sort_column: sort_index = self.getIndexKey( self.getColumns()[int(sort_column)-1].id) reverse = REQUEST.get('sSortDir_0', None) if reverse == 'desc': reverse = 0 if reverse == 'asc': reverse = 1 if limit < 1: limit = None if not REQUEST is None and 'request_query' in REQUEST: # query parameter in REQUEST is supposed to be a json object request_query = self.__query_loads__(REQUEST['request_query']) else: request_query = None results = self.getAllDocuments( start=start, limit=limit, getObject=False, fulltext_query=search, sortindex=sort_index, reverse=reverse, request_query=request_query) total = display_total = len(results) columns = [column for column in self.getColumns() if not getattr(column, 'HiddenColumn', False)] for brain in results: row = [brain.getPath().split('/')[-1]] for column in columns: column_value = getattr(brain, self.getIndexKey(column.id), '') rendered = column.getColumnRender(column_value) if isinstance(rendered, list): rendered = [asUnicode(e).encode('utf-8').replace('\r', '') for e in rendered] else: rendered = asUnicode(rendered).encode('utf-8').replace('\r', '') row.append(rendered or ' ') if categorized: for cat in asList(row[1]): entry = [c for c in row] entry[1] = cat data.append(entry) else: data.append(row) return json.dumps( {'iTotalRecords': total, 'iTotalDisplayRecords': display_total, 'aaData': data })
def code(self): if self.request.method == "GET": self.request.RESPONSE.setHeader( 'content-type', 'text/plain; charset=utf-8') type = self.request.form.keys()[0] if type not in ["Form", "FormField", "FormAction", "FormHidewhen", "View", "ViewAction", "ViewColumn", "Agent"]: return "Parameter error" element = self.getElementByType(type, self.request.form[type]) if not element: return "Name error" if type == "Agent": return json.dumps({"code" : element.content, "methods" : []}) if type == "FormHidewhen": return json.dumps({"code" : element.formula, "methods" : []}) methods = self.getMethods(type) code = "" for method in self.getMethodsId(type): formula = getattr(element, method, None) if formula: code+= "## START "+method+" {\n" code+= formula code+= "\n## END "+method+" }\n\r" elements = {"code" : code, "methods" : methods} return json.dumps(elements) if self.request.method == "POST": alsoProvides(self.request,plone.protect.interfaces.IDisableCSRFProtection) self.request.RESPONSE.setHeader( 'content-type', 'application/json; charset=utf-8') response = json.loads(self.request.BODY) type = response["Type"] id = response["Id"] code = response["Code"] if type == "Agent": self.context.getAgent(id).content = code return json.dumps({ "type": "OK" }) if type == "FormHidewhen": id = id.split('/') self.context.getForm(id[0]).getHidewhen(id[1]).formula = code return json.dumps({ "type": "OK" }) methodList = self.getMethodsId(type) content = "" contents = [] inside = False for lineNumber, line in enumerate(code.split('\n')): start_reg = re.match(r'^##\s*START\s+(.*){$', line) end_reg = re.match(r'^##\s*END\s+(.*)}$',line) if start_reg and not inside: if start_reg.group(1).strip() in methodList: methodName = start_reg.group(1).strip() inside = True else: return json.dumps({ "type": "Error", "error": "Method \""+start_reg.group(1).strip()+"\" doesn't exists", "line": lineNumber+1 }) elif end_reg and inside: if end_reg.group(1).strip() != methodName: return json.dumps({ "type": "Error", "error": "END tag doesn't match START tag", "line": lineNumber+1 }) contents.append({ "name": methodName, "code": content }) inside = False content = '' elif not start_reg and not end_reg and inside: content+= line+"\n" elif end_reg and not inside: return json.dumps({ "type": "Error", "error": "Unexpected END tag", "line": lineNumber+1 }) elif start_reg and inside: return json.dumps({ "type": "Error", "error": "Unexpected START tag", "line": lineNumber+1 }) element = self.getElementByType(type,id) for formula in methodList: setattr(element,formula,'') for formula in contents: setattr(element,formula['name'],formula['code'].rstrip()) return json.dumps({ "type": "OK" })
def json_dumps(obj): return json.dumps(obj)
def tree(self): database = self.context.getParentDatabase() # Create form tree forms = [] for form in database.getForms(): fields = [] for field in form.getFormFields(): fields.append({ "label": field.title, "url": field.absolute_url(), "type": 'PlominoField' }) plomino_form = [] plomino_form.append({ "label": "Fields", "folder": True, "children": fields, "type": "PlominoField", "url": form.absolute_url() }) actions = [] for action in form.getFormActions(): actions.append({ "label": action.title, 'type': 'PlominoAction', "url": action.absolute_url() }) plomino_form.append({ "label": "Actions", "folder": True, "children": actions, "type": "PlominoAction", "url": form.absolute_url() }) hide_whens = [] for hide_when in form.getHidewhenFormulas(): hide_whens.append({ "label": hide_when.title, 'type': 'PlominoHidewhen', "url": hide_when.absolute_url() }) plomino_form.append({ "label": "Hide Whens", "folder": True, "children": hide_whens, "type": "PlominoHidewhen", "url": form.absolute_url() }) forms.append({ "label": form.title, "folder": True, "children": plomino_form, "type": "PlominoForm", "url": form.absolute_url(), }) # Create Views Tree views = [] for view in database.getViews(): plomino_view = [] actions = [] for action in view.getActions(): # view.getActions() returns tuples actions.append({ "label": action[0].title, "type": 'PlominoAction', "url": action[0].absolute_url() }) plomino_view.append({ "label": "Actions", "folder": True, "children": actions, "type": "PlominoAction", "url": view.absolute_url() }) columns = [] for column in view.getColumns(): columns.append({ "label": column.title, "type": "PlominoColumn", "url": column.absolute_url() }) plomino_view.append({ "label": "Columns", "folder": True, "children": columns, "type": "PlominoColumn", "url": view.absolute_url() }) views.append({ "label": view.title, "type": "PlominoView", "children": plomino_view, "url": view.absolute_url(), }) # Create Agents View agents = [] for agent in database.getAgents(): agents.append({ "label": agent.title, "type": "PlominoAgent", "url": agent.absolute_url() }) # Build the final element tree elements = [{ "label": "Forms", "folder": True, "children": forms, "type": 'PlominoForm', "url": database.absolute_url() }, { "label": "Views", "folder": True, "children": views, "type": 'PlominoView', "url": database.absolute_url() }, { "label": "Agents", "folder": True, "children": agents, "type": 'PlominoAgent', "url": database.absolute_url() }] self.request.RESPONSE.setHeader('content-type', 'application/json; charset=utf-8') return json.dumps(elements)
def test_dumps(self): self.assertEquals(json.dumps({}), '{}')
def json_dumps(data): def json_date_handler(obj): if hasattr(obj, 'ISO'): obj = obj.ISO() return obj return json.dumps(data, default=json_date_handler)
def tree(self): database = self.context.getParentDatabase() # Create form tree forms = [] for form in database.getForms(): fields = [] for field in form.getFormFields(): fields.append({ "label": field.id, "url": field.absolute_url(), "type" : 'field' }) plomino_form = [] plomino_form.append({ "label" : "Fields", "folder" : True, "children" : fields, "type" : "fields", }) actions = [] for action in form.getFormActions(): actions.append({ "label": action.id, 'type' : 'action', "url" : action.absolute_url() }) plomino_form.append({ "label": "Actions", "folder": True, "children": actions, "type" : "actions", }) forms.append({ "label": form.id, "folder": True, "children": plomino_form, "type" : "form", "url" : form.absolute_url(), }) # Create Views Tree views = [] for view in database.getViews(): plomino_view = [] actions = [] for action in view.getActions(): # view.getActions() returns tuples actions.append({ "label": action[0].id, "type": 'action', "url": action[0].absolute_url() }) plomino_view.append({ "label": "Actions", "folder": True, "children": actions, "type": "actions", }) columns = [] for column in view.getColumns(): columns.append({ "label": column.id, "type": 'column', "url": column.absolute_url() }) plomino_view.append({ "label": "Columns", "folder": True, "children": columns, "type": "columns", }) views.append({ "label": view.id, "type": "view", "children": plomino_view, "url": view.absolute_url(), }) # Create Agents View agents = [] for agent in database.getAgents(): agents.append({ "label" : agent.id, "type" : "agent", "url" : agent.absolute_url() }) # Build the final element tree elements = [ { "label": "Forms", "folder": True, "children": forms, "type" : 'database' }, { "label": "Views", "folder": True, "children": views, "type" : 'views' }, { "label": "Agents", "folder": True, "children": agents , "type" : 'agents' } ] self.request.RESPONSE.setHeader( 'content-type', 'application/json; charset=utf-8') return json.dumps(elements)
def test_encoding6(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' j = json.dumps([u], ensure_ascii=False) self.assertEquals(j, u'["%s"]' % (u,))