def _listen(cls, database): Database = backend.get('Database') db = Database(database) if not db.has_channel(): raise NotImplemented logger.info("listening on channel '%s'", cls._channel) conn = db.get_connection() try: cursor = conn.cursor() cursor.execute('LISTEN "%s"' % cls._channel) conn.commit() cls._messages[database] = messages = _MessageQueue(_cache_timeout) now = time.time() while cls._queues[database]['timeout'] > now: readable, _, _ = select.select([conn], [], [], _select_timeout) if not readable: continue conn.poll() while conn.notifies: notification = conn.notifies.pop() payload = json.loads(notification.payload, object_hook=JSONDecoder()) channel = payload['channel'] message = payload['message'] messages.append(channel, message) with cls._queues_lock: events = \ cls._queues[database]['events'][channel].copy() cls._queues[database]['events'][channel].clear() for event in events: event.set() now = time.time() except Exception: logger.error('bus listener on "%s" crashed', database, exc_info=True) with cls._queues_lock: del cls._queues[database] raise finally: db.put_connection(conn) with cls._queues_lock: if cls._queues[database]['timeout'] <= now: del cls._queues[database] else: # A query arrived between the end of the while and here listener = threading.Thread(target=cls._listen, args=(database, ), daemon=True) cls._queues[database]['listener'] = listener listener.start()
def _listen(cls, database): db = backend.Database(database) if not db.has_channel(): raise NotImplementedException logger.info("listening on channel '%s'", cls._channel) conn = db.get_connection(autocommit=True) pid = os.getpid() selector = selectors.DefaultSelector() try: cursor = conn.cursor() cursor.execute('LISTEN "%s"' % cls._channel) cls._messages[database] = messages = _MessageQueue(_cache_timeout) now = time.time() selector.register(conn, selectors.EVENT_READ) while cls._queues[pid, database]['timeout'] > now: selector.select(timeout=_select_timeout) conn.poll() while conn.notifies: notification = conn.notifies.pop() payload = json.loads( notification.payload, object_hook=JSONDecoder()) channel = payload['channel'] message = payload['message'] messages.append(channel, message) with cls._queues_lock[pid]: events = cls._queues[pid, database][ 'events'][channel].copy() cls._queues[pid, database]['events'][channel].clear() for event in events: event.set() now = time.time() except Exception: logger.error('bus listener on "%s" crashed', database, exc_info=True) with cls._queues_lock[pid]: del cls._queues[pid, database] raise finally: selector.close() db.put_connection(conn) with cls._queues_lock[pid]: if cls._queues[pid, database]['timeout'] <= now: del cls._queues[pid, database] else: # A query arrived between the end of the while and here listener = threading.Thread( target=cls._listen, args=(database,), daemon=True) cls._queues[pid, database]['listener'] = listener listener.start()
def get(self, ids, model, name, values=None): dicts = dict((id, None) for id in ids) for value in values or []: data = value[name] if data: # If stored as JSON conversion is done on backend if isinstance(data, str): data = json.loads(data, object_hook=JSONDecoder()) dicts[value['id']] = data return dicts
def __init__(self, session_id): pool = Pool() Session = pool.get('ir.session.wizard') self._session_id = session_id session = Session(session_id) data = json.loads(session.data, object_hook=JSONDecoder()) for state_name, state in self.states.items(): if isinstance(state, StateView): Target = pool.get(state.model_name) data.setdefault(state_name, {}) setattr(self, state_name, Target(**data[state_name]))
def get(self, ids, model, name, values=None): dicts = dict((id, None) for id in ids) for value in values or []: data = value[name] if data: # If stored as JSON conversion is done on backend if isinstance(data, str): data = json.loads(data, object_hook=JSONDecoder()) for key, val in data.items(): if isinstance(val, list): data[key] = tuple(val) dicts[value['id']] = ImmutableDict(data) return dicts
def format_filter(cls, execution): pool = Pool() Lang = pool.get('ir.lang') if not execution.filter_values: return [] locale = Transaction().context.get( 'report_lang', Transaction().language).split('_')[0] lang, = Lang.search([ ('code', '=', locale or 'en'), ]) filter_data = json.loads(execution.filter_values, object_hook=JSONDecoder()) parameters = dict( (p.id, p) for p in execution.report.filter.parameters) res = [] for key in sorted(filter_data.keys()): parameter = parameters[int(key.split('_')[-1:][0])] value = filter_data[key] if parameter.ttype == 'date': value = lang.strftime(value) elif parameter.ttype in ('float', 'numeric'): value = lang.format('%.2f', value, grouping=True) elif parameter.ttype == 'integer': value = lang.format('%d', value, grouping=True) elif parameter.ttype == 'boolean': value = (gettext('babi.msg_true') if value else gettext('babi.msg_false')) elif parameter.ttype == 'many2many': Model = pool.get(parameter.related_model.model) record = [] if value: record.append(Model(value[0]).rec_name) if len(value) > 2: record.append('...') if len(value) > 1: record.append(Model(value[-1]).rec_name) value = ', '.join(record) elif parameter.ttype == 'many2one': Model = pool.get(parameter.related_model.model) value = Model(value).rec_name res.append('%s: %s' % (parameter.name, value)) return res
def data(request, pool, model): User = pool.get('res.user') Lang = pool.get('ir.lang') try: Model = pool.get(model) except KeyError: abort(HTTPStatus.NOT_FOUND) transaction = Transaction() context = User(transaction.user).get_preferences(context_only=True) language = request.args.get('l') if language: context['language'] = language try: domain = json.loads(request.args.get('d', '[]'), object_hook=JSONDecoder()) except json.JSONDecodeError: abort(HTTPStatus.BAD_REQUEST) try: ctx = json.loads(request.args.get('c', '{}'), object_hook=JSONDecoder()) except json.JSONDecoder: abort(HTTPStatus.BAD_REQUEST) for key in list(ctx.keys()): if key.startswith('_') and key != '_datetime': del ctx[key] context.update(ctx) limit = None offset = 0 if 's' in request.args: try: limit = int(request.args.get('s')) if 'p' in request.args: offset = int(request.args.get('p')) * limit except ValueError: abort(HTTPStatus.BAD_REQUEST) if 'o' in request.args: order = [(o.split(',', 1) + [''])[:2] for o in request.args.getlist('o')] else: order = None fields_names = request.args.getlist('f') encoding = request.args.get('enc', 'UTF-8') delimiter = request.args.get('dl', ',') quotechar = request.args.get('qc', '"') try: header = bool(int(request.args.get('h', True))) locale_format = bool(int(request.args.get('loc', False))) except ValueError: abort(HTTPStatus.BAD_REQUEST) with transaction.set_context(**context): lang = Lang.get(transaction.language) def format_(row): for i, value in enumerate(row): if locale_format: if isinstance(value, Number): value = lang.format('%.12g', value) elif isinstance(value, (dt.date, dt.datetime)): value = lang.strftime(value) elif isinstance(value, bool): value = int(value) row[i] = value return row try: if domain and isinstance(domain[0], (int, float)): rows = Model.export_data(domain, fields_names) else: rows = Model.export_data_domain(domain, fields_names, limit=limit, offset=offset, order=order) except (ValueError, KeyError): abort(HTTPStatus.BAD_REQUEST) data = io.StringIO(newline='') writer = csv.writer(data, delimiter=delimiter, quotechar=quotechar) if header: writer.writerow(fields_names) for row in rows: writer.writerow(format_(row)) data = data.getvalue().encode(encoding) filename = slugify(Model.__names__()['model']) + '.csv' filename = filename.encode('latin-1', 'ignore') response = Response(data, mimetype='text/csv; charset=' + encoding) response.headers.add('Content-Disposition', 'attachment', filename=filename) response.headers.add('Content-Length', len(data)) return response
def convert_json(value): return json.loads(value, object_hook=JSONDecoder())
def convert_json(value): from trytond.protocols.jsonrpc import JSONDecoder return json.loads(value, object_hook=JSONDecoder())
def dumps_loads(self, value): self.assertEqual( json.loads(json.dumps(value, cls=JSONEncoder), object_hook=JSONDecoder()), value)
import json import requests from functools import partial from trytond.protocols.jsonrpc import JSONDecoder, JSONEncoder dumps = partial(json.dumps, cls=JSONEncoder) loads = partial(json.loads, object_hook=JSONDecoder()) class Client(object): def __init__(self, subdomain, api_key): self.subdomain = subdomain self.api_key = api_key self.base_url = 'https://%s.fulfil.io/api/v1' % self.subdomain self.session = requests.Session() self.session.headers.update({'x-api-key': api_key}) def model(self, name): return Model(self, name) class Model(object): _local_methods = ['create', 'search', 'get'] def __init__(self, client, model_name): self._client = client self._model_name = model_name @property