def to_representation(self, obj): request = self.context.get('request') if not isinstance(obj, XForm): return super(DataListSerializer, self).to_representation(obj) query_params = (request and request.query_params) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.user.username, obj.id_string) } try: query.update(json.loads(query_params.get('query', '{}'))) except ValueError: raise ParseError(_("Invalid query: %(query)s" % {'query': query_params.get('query')})) query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) return list(cursor)
def to_native(self, obj): request = self.context.get('request') if obj is None: return super(DataListSerializer, self).to_native(obj) query_params = (request and request.QUERY_PARAMS) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.user.username, obj.id_string) } try: query.update(json.loads(query_params.get('query', '{}'))) except ValueError: raise ParseError( _("Invalid query: %(query)s" % {'query': query_params.get('query')})) query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) records = list(record for record in cursor) return records
def to_native(self, obj): request = self.context.get('request') if obj is None: return super(DataListSerializer, self).to_native(obj) query_params = (request and request.QUERY_PARAMS) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.user.username, obj.id_string) } try: query.update(json.loads(query_params.get('query', '{}'))) except ValueError: raise ParseError(_("Invalid query: %(query)s" % {'query': query_params.get('query')})) query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) records = list(record for record in cursor) return records
def to_representation(self, obj): if not hasattr(obj, 'xform'): return super(DataInstanceSerializer, self).to_representation(obj) request = self.context.get('request') query_params = (request and request.query_params) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.xform.user.username, obj.xform.id_string), u'_id': obj.pk } query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) records = list(record for record in cursor) returned_dict = {} if (len(records)): returned_dict = records[0] #break code fix # returned_dict = (len(records) and records[0]) or records return MongoHelper.to_readable_dict(returned_dict)
def _get_form_data(self, xform, **kwargs): query = kwargs.get('query', {}) query = query if query is not None else {} if xform: query[ParsedInstance.USERFORM_ID] =\ u'%s_%s' % (xform.user.username, xform.id_string) query = json.dumps(query) if isinstance(query, dict) else query margs = { 'query': query, 'fields': kwargs.get('fields', None), 'sort': kwargs.get('sort', None) } cursor = ParsedInstance.query_mongo_minimal(**margs) records = list(record for record in cursor) return records
def _get_form_data(self, xform, **kwargs): query = kwargs.get('query', {}) query = query if query is not None else {} if xform: query[ParsedInstance.USERFORM_ID] =\ u'%s_%s' % (xform.user.username, xform.id_string) query = json.dumps(query) if isinstance(query, dict) else query margs = { 'query': query, 'fields': kwargs.get('fields', None), 'sort': kwargs.get('sort', None) } cursor = ParsedInstance.query_mongo_minimal(**margs) records = list(record for record in cursor) return records
def to_native(self, obj): request = self.context.get('request') query_params = (request and request.QUERY_PARAMS) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.xform.user.username, obj.xform.id_string), u'_id': obj.pk } query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) records = list(record for record in cursor) return (len(records) and records[0]) or records
def to_representation(self, obj): request = self.context.get('request') if not isinstance(obj, XForm): return super(DataListSerializer, self).to_representation(obj) query_params = (request and request.query_params) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.user.username, obj.id_string) } limit = query_params.get('limit', False) start = query_params.get('start', False) count = query_params.get('count', False) try: query.update(json.loads(query_params.get('query', '{}'))) except ValueError: raise ParseError( _("Invalid query: %(query)s" % {'query': query_params.get('query')})) query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } # if we want the count, we don't kwow to paginate the records. # start and limit are useless then. if count: query_kwargs['count'] = True else: if limit: query_kwargs['limit'] = int(limit) if start: query_kwargs['start'] = int(start) cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) # if we want the count, we only need the first index of the list. if count: return cursor[0] else: return [MongoHelper.to_readable_dict(record) for record in cursor]
def to_representation(self, obj): request = self.context.get('request') if not isinstance(obj, XForm): return super(DataListSerializer, self).to_representation(obj) query_params = (request and request.query_params) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.user.username, obj.id_string) } limit = query_params.get('limit', False) start = query_params.get('start', False) count = query_params.get('count', False) try: query.update(json.loads(query_params.get('query', '{}'))) except ValueError: raise ParseError(_("Invalid query: %(query)s" % {'query': query_params.get('query')})) query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } # if we want the count, we don't kwow to paginate the records. # start and limit are useless then. if count: query_kwargs['count'] = True else: if limit: query_kwargs['limit'] = int(limit) if start: query_kwargs['start'] = int(start) cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) # if we want the count, we only need the first index of the list. if count: return cursor[0] else: return [MongoHelper.to_readable_dict(record) for record in cursor]
def to_representation(self, obj): if not hasattr(obj, 'xform'): return super(DataInstanceSerializer, self).to_representation(obj) request = self.context.get('request') query_params = (request and request.query_params) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.xform.user.username, obj.xform.id_string), u'_id': obj.pk } query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) records = list(record for record in cursor) return (len(records) and records[0]) or records
def to_native(self, obj): if obj is None: return super(DataInstanceSerializer, self).to_native(obj) request = self.context.get('request') query_params = (request and request.QUERY_PARAMS) or {} query = { ParsedInstance.USERFORM_ID: u'%s_%s' % (obj.xform.user.username, obj.xform.id_string), u'_id': obj.pk } query_kwargs = { 'query': json.dumps(query), 'fields': query_params.get('fields'), 'sort': query_params.get('sort') } cursor = ParsedInstance.query_mongo_minimal(**query_kwargs) records = list(record for record in cursor) return (len(records) and records[0]) or records