def render_html(self, value): renderer = self._renderers['html'] title = "report : %s" % self.context._report_type fields = self.context.default_fields if isinstance(value, Report): # an iterable self.request.response.charset = renderer[1] self.request.response.content_type = renderer[0][0] return dict(body=render( 'reports/templates/_report.jinja2', dict(report=value, fields=self.fields, table_id=self._entity_name, title=title, wrap=self.wrap, js_links="datatable/js/jquery.dataTables.min.js", fields_json=Markup( json.dumps([dict(mData=f) for f in self.fields]))), self.request), ) else: return render_to_response( "reports/templates/_report_list.jinja2", { "title": "Reports", "report_types": value #self.request.db_session.query(ReportType) }, self.request) raise Exception()
def render_html(self, value): renderer = self._renderers['html'] title = 'api : events' fields = self.context.default_fields # a model instance if isinstance(value, Event): #hasattr(value, '__iter__'): value = (value, ) title = u"Event: %s" % Event.id template = 'events/templates/_event.jinja2' else: template = 'events/templates/_events_table.jinja2' fieldsdict = [dict(mData=f) for f in fields] # an iterable return dict(body=render( template, dict(value=value, fields=fields, table_id=self._entity_name_plural, title=title, wrap=self.wrap, js_links=( "workhours:static/datatable/js/jquery.dataTables.min.js", ), fields_json=Markup(json.dumps(fieldsdict))), self.request), charset=renderer[1], content_type=renderer[0][0])
def render_html(self, value): renderer = self._renderers['html'] title = 'api : places' fields = self.fields # a model instance if isinstance(value, Place): #not hasattr(value, '__iter__'): value = [value] title = u"Place: %s" % self.request.matchdict['id'] template = 'places/templates/_place.jinja2' else: template = 'places/templates/_places_table.jinja2' # an iterable return dict(body=render( template, dict(value=value, fields=fields, table_id=self._entity_name_plural, title=title, wrap=self.wrap, js_links=( "workhours:static/datatable/js/jquery.dataTables.min.js", ), fields_json=(Markup( json.dumps([dict(mDataProp=f) for f in fields])))), self.request), charset=renderer[1], content_type=renderer[0][0])
def jsonify(eval_ctx, value): result = None if value: result = dumps(value) if eval_ctx.autoescape: result = Markup(result) return result
def get_member_key_as_string(self, member): """Return the proper string representation of the member""" key = member[self.index_key] if isinstance(key, (int, basestring)): return key else: return json.dumps(key, cls=self.json_encoder)
def jsonify_indent(eval_ctx, value): result = None if value: result = dumps( {k:v for k,v in value.iteritems() if not k.startswith('_')}, indent=2) if eval_ctx.autoescape: result = Markup(result) return result
def to_json(self, value, fields=None, wrap=True): """Convert instance or sequence of instances to JSON. ``value`` is a single ORM instance or an iterable that yields instances. ``fields`` is a list of fields to include for each instance. ``wrap`` indicates whether or not the result should be wrapped or returned as-is. """ #obj = self.get_json_obj(value, fields, wrap) return json.dumps(value, cls=self.json_encoder)
def write(self, obj): print(json.dumps(obj._asdict(), indent=2), end=',\n', file=self._output)
auto_reload=True, # TODO autoescape=False) template = env.get_template(template_name) return template.render(context) if __name__ == "__main__": import sys _output = sys.stdout debug = False if sys.argv[1:]: _output_filename = sys.argv[1].strip() if _output_filename: _output = open(_output_filename, 'w') try: context = get_local_context() if debug: import workhours.models.json as json print(json.dumps(context, indent=2), file=sys.stdout) print(render(TEMPLATE_NAME, context), file=_output) finally: if _output is not sys.stdout: _output.close
test_es_url = 'http://localhost:9200/' import logging log = logging.getLogger('workhours.models.es.tests.TestESSession') import workhours.models.json as json dumps = lambda obj: json.dumps(obj, indent=2) import unittest import workhours.models.es from workhours.models.es import ESSession class TestESSession(unittest.TestCase): def test_es_session(self): log.debug("ESSession: %s" % test_es_url) s = ESSession(test_es_url) log.debug("get_indexes: %s" % s.get_indexes()) # TODO index = 'test-index' obj_type = 'test-type' obj = {'test': True, 'one': 'two'} result = s.put(obj, index=index, type=obj_type) log.debug('result: %r' % result) self.assertTrue(result['ok']) self.assertEqual(result['_type'], obj_type) self.assertTrue(result.get('_id')) self.assertEqual(result['_index'], index)