def run(self, sql, args=None, no_cursor=False): self.open() if self.engine is None or self.conn is None or self.conn.closed: return None rows = [] with self.conn.begin() as trans: try: if args: cursor = self.conn.execute(sql, args) else: cursor = self.conn.execute(sql) if not no_cursor: rows = [row for row in cursor if cursor] trans.commit() except: try: trans.rollback() except: pass print_to(None, 'NO SQL EXEC: %s' % sql) if IsPrintExceptions: print_exception() self.engine_error = True rows = None self.close() return rows
def index(): debug, kw = init_response('WebPerso Order State Page') kw['product_version'] = product_version command = get_request_item('command') if IsDebug: print('--> command:%s, order_id:%s, event_id:%s' % ( command, kw.get('order_id'), kw.get('event_id') )) refresh() errors = [] if command and command.startswith('admin'): pass kw['errors'] = '<br>'.join(errors) kw['OK'] = '' try: kw = _make_page_default(kw) if IsTrace: print_to(errorlog, '--> orderstate:%s %s %s' % (command, current_user.login, str(kw.get('current_file')),), request=request) except: print_exception() kw['vsc'] = (IsDebug or IsIE or IsForceRefresh) and ('?%s' % str(int(random.random()*10**12))) or '' if command and command.startswith('admin'): pass elif command == 'export': columns = kw['config']['orders']['export'] rows = [] for data in kw['orders']: row = [] for column in columns: if column == 'FinalMessage': continue v = data[column] if 'Date' in column: v = re.sub(r'\s+', ' ', re.sub(r'<.*?>', ' ', v)) row.append(v) rows.append(row) rows.insert(0, columns) xls = makeXLSContent(rows, 'Журнал заказов (OrderState)', True) response = make_response(xls) response.headers["Content-Disposition"] = "attachment; filename=orders.xls" return response return make_response(render_template('orderstate.html', debug=debug, **kw))
def _init_state(self, **kw): if IsDeepDebug: print_to(None, 'FileDecoder.inistate') self._original = kw.get('original') or '' self._request_id = current_request_id() self._tmp = '%s/%s%s' % (self.tmp_folder, str(self._request_id), '%s')
def __init__(self): if IsDeepDebug: print_to(None, 'FileDecoder.init') self._original = '' self._tmp = '%s' self._request_id = None
def register_action(self, operator, client, order, batch, action): """ Register BatchTime Operator Action. Arguments: operator -- dict, operator {login, name} Keyword arguments: bp_client_id -- int: BP_ClientID bp_file_id -- int: BP_FileID bp_batch_id -- int: BP_BatchID client -- string: client name fname -- string: file name fqty -- int: cards number register_date -- string: file register datetime batchname -- string: batch name batchtype_id -- int: batchtype id batchno -- int: batch number pers_tz -- int: TZ element_qty -- int: items number add -- int: 1|0 add action delete -- int: 1|0 delete action Returns: Stored procedure response. If OK `ActionID`, `Status` """ if not action or not operator: return self.action_id, self.status = 0, '' cursor = engine.runProcedure('persostation-register-action', login=operator['login'], fullname=operator['name'], bp_client_id=client.get('ClientID'), bp_file_id=order.get('FileID'), bp_batch_id=batch.get('TID'), client=client.get('BankName'), fname=order.get('FName'), fqty=order.get('FQty'), register_date=getDate(order.get('RegisterDate'), format=UTC_FULL_TIMESTAMP), batchname=batch.get('BatchType'), batchtype_id=batch.get('BatchTypeID'), batchno=batch.get('BatchNo'), pers_tz=batch.get('TZ'), element_qty=batch.get('ElementQty'), add=action == 'add' and 1 or 0, delete=action == 'delete' and 1 or 0, ) if cursor: self.action_id = cursor[0][0] self.status = cursor[0][1] else: if IsDebug: print_to(None, '!!! register_action, no cursor: %s' % str(operator))
def _decompress(self, xml, body): try: self.file_setter(xml, zlib.decompress(body)) except: if IsTrace: print_to( errorlog, 'FileImageDecoder._decompress Error:%s' % self._file_id) if IsPrintExceptions: print_exception()
def walk(logs, checker, root, **kw): client = kw.get('client') options = kw.get('options') or '' aliases = kw.get('aliases') or None files = kw.get('files') obs = os.listdir(root) for name in obs: folder = normpath(os.path.join(root, name)) # # Check Logs limit # if logs and len(logs) > MAX_LOGS_LEN: break if not os.path.exists(folder): continue if name in config.get('suspend'): continue # # Check folder name # elif os.path.isdir(folder): # and not os.path.islink(folder): if not valid_name('dir', name): continue if '*' in options: pass elif 'with_aliases' in options and aliases is not None: if not check_aliases(folder, aliases): continue if IsLogTrace: print_to(None, '--> folder: %s' % folder) walk(logs, checker, folder, **kw) # # Check file name & start log-checker # else: if not valid_name('file', name): continue filename = normpath(os.path.join(root, name)) if not is_today_file(name, dates=kw.get('dates'), filemask=kw.get('filemask'), filename=filename, format=kw.get('fmt')): continue if 'pointers' in kw: if files is not None: files[filename] = os.path.getsize(filename) elif checker is None: continue else: checker(logs, filename, **kw)
def send_message(user, params): subject = params.get('subject') or gettext('Announcement') message = params.get('message') is_with_greeting = params.get('with_greeting') and True or False is_with_signature = params.get('with_signature') and True or False done = 1 html = _EMAIL_HTML % { 'Subject': subject, 'Message': re.sub(r'\n', r'<br>', message), 'Greeting': _GREETING, 'Signature': _SIGNATURE % (current_user.post, current_user.full_name()), 'hidden_g': params.get('with_greeting') != 1 and 'hidden' or '', 'hidden_s': params.get('with_signature') != 1 and 'hidden' or '', } try: users = map(User.get_by_id, params.get('ids') or []) default_email = current_user.email addr_to = list(filter( None, [x.email for x in users if x.confirmed ])) if params.get('to_everybody') else [user.email] addr_cc = [default_email] if default_email not in addr_to else None if not message or not addr_to: return 0 addr_to = ';'.join(addr_to) addr_cc = addr_cc and ';'.join(addr_cc) or '' timestamp = getDate(getToday(), format=UTC_FULL_TIMESTAMP) if not IsNoEmail: done = send_simple_mail(subject, html, addr_to, addr_cc=addr_cc) if IsTrace: print_to( None, '>>> mail sent %s, login:%s, to:%s, cc:%s, subject: [%s], done:%s' % (timestamp, current_user.login, addr_to, addr_cc, subject, done)) except: if IsPrintExceptions: print_exception() done = 0 return done
def loader_ext1(): exchange_error = '' exchange_message = '' action = get_request_item('action') or default_action selected_menu_action = get_request_item('selected_menu_action') or action != default_action and action or default_log_action response = {} params = get_request_item('params') or None refresh() if IsDebug: print('--> action:%s' % action) if IsTrace: print_to(errorlog, '--> loader:%s %s %s' % ( action, current_user.login, params and ' params:[%s]' % params or '', )) data = [] errors = None try: if action == default_action: data = calculate(params) if not action: pass except: print_exception() response.update({ 'action' : action, # -------------- # Service Errors # -------------- 'exchange_error' : exchange_error, 'exchange_message' : exchange_message, # -------------------------- # Results (Log page content) # -------------------------- 'total' : len(data), 'data' : data, 'errors' : errors, }) return jsonify(response)
def loader(): exchange_error = '' exchange_message = '' refresh() action = get_request_item('action') or default_action selected_menu_action = get_request_item( 'selected_menu_action') or action != default_action and action or '901' response = {} template = get_request_item('template') or default_template lid = get_request_item('lid') if IsDebug: print('--> action:%s %s lid:%s' % (action, template, lid)) if IsSemaphoreTrace: print_to( errorlog, '--> loader:%s [%s:%s:%s] %s' % (action, template, lid, selected_menu_action, getTime(UTC_FULL_TIMESTAMP))) state = {} try: if action == default_action: action = selected_menu_action if not action: pass elif action == '901': state = getCurrentState(template, lid) except: print_exception() response.update({ \ 'action' : action, # -------------- # Service Errors # -------------- 'exchange_error' : exchange_error, 'exchange_message' : exchange_message, # ------------------------- # Results (Semaphore state) # ------------------------- 'state' : state, }) return jsonify(response)
def run(**kw): global _processed global _found ctype = config['ctype'].lower() emitter = config.get('emitter') or False limit = config.get('limit') or 0 try: if not ctype: app = AbstractSource(config, logger) elif ctype == 'bankperso': app = Bankperso(config, logger) elif ctype == 'sdc': app = SDC(config, logger) elif ctype == 'exchange': app = Exchange(config, logger) else: app = Bankperso(config, logger) app._init_state(**kw) print_to(None, '>>> Logger Started[%s], date_from: %s, root: %s' % ( \ config['ctype'], kw.get('date_from'), config['root'], )) if app.is_ready(): if emitter: _processed, _found = app.emitter(limit=limit) else: _processed, _found = app(limit=limit) if not IsDisableOutput: _pout('>>> New messages found: %d' % (sum([_found[x] for x in _found]) or 0)) _pout('>>> Total processed: %d orders' % _processed) _pout('>>> Unresolved: %d lines' % app._unresolved_lines()) start_observer(app, **kw) app._term() print_to(None, '%s>>> Logger Finished[%s]%s' % ( \ cr, config['ctype'], cr, )) except: print_exception()
def __init__(self, parser, mode, no_test=False): self._parser = parser self._mode = mode self._stopped = False self._item = None self._index = -1 if not no_test: self.inner_valid() if IsTrace: print_to(errorlog, 'RecordsParser[%s]' % self._mode)
def index(): debug, kw = init_response('WebPerso Calculator Page') kw['product_version'] = product_version is_admin = current_user.is_administrator() command = get_request_item('command') refresh() IsMakePageDefault = True info = '' errors = [] if command.startswith('admin'): command = command.split(DEFAULT_HTML_SPLITTER)[1] if get_request_item('OK') != 'run': command = '' if IsDebug: print('--> %s' % info) if IsTrace: print_to(errorlog, '--> command:%s %s [%s]' % (command, current_user.login, info)) kw['errors'] = '<br>'.join(errors) kw['OK'] = '' try: if IsMakePageDefault: kw = _make_page_default(kw) if IsTrace: print_to(errorlog, '--> calculator:%s %s [%s] %s %s' % ( \ command, current_user.login, request.remote_addr, str(kw.get('current_file')), info,), request=request) except: print_exception() kw['vsc'] = vsc() if command: if not command.strip(): pass elif command == 'export': return _make_xls_content(_make_export(kw), 'Себестоимость ...') return make_response(render_template('calculator/%s.html' % default_locator, debug=debug, **kw))
def getExchangeLogInfo(**kw): global config logs = [] encoding, root, filemask, options = kw.get('config') or getExchangeConfig( kw.get('client')) if root is None: return logs set_globals(kw.get('globals')) config = exchange_log_config root = normpath(os.path.join(root, config['root'])) kw['filemask'] = filemask kw['options'] = options if IsTrace: keys = _extract_keys(kw.get('keys')) or [] print_to( None, '\n==> CHECK_EXCHANGE_LOG: %s STARTED [%s:%s:%s]' % ( datetime.datetime.now().strftime(UTC_FULL_TIMESTAMP), len(keys) > 0 and keys[0] or '', kw.get('client'), kw.get('dates'), )) try: if IsCheckFolders: logger = Logger('./folders.txt', encoding=default_encoding) check_path(".", logger) logger.close() walk(logs, check_exchange_log, root, encoding=encoding, **kw) except Exception as e: _register_error(logs, e, **kw) if IsPrintExceptions: print_exception() logs = sorted(logs, key=itemgetter('Date')) if IsTrace: print_to( None, '==> CHECK_EXCHANGE_LOG: %s FINISHED' % datetime.datetime.now().strftime(UTC_FULL_TIMESTAMP)) return logs
def openfile(filename, mode='r', encoding=default_encoding, use_codecs=False): is_opened = False forced_encoding = encoding default_mode = 'r' fo = None line = '' try: if 'b' in mode: fo = open(filename, mode) elif use_codecs: fo = codecs.open(filename, mode, encoding=forced_encoding) else: fo = open(filename, mode, encoding=forced_encoding) # # Check content valid encoding # line = fo.readline() fo.seek(0) is_opened = True line = None except: if IsLogTrace: print_to( None, '>>> openfile error: [%s] mode:%s encoding:%s' % (filename, mode, forced_encoding)) forced_encoding = get_opposite_encoding(forced_encoding) if fo is not None and not fo.closed: fo.close() # # Try to reopen file in opposite encoding # try: if use_codecs: fo = codecs.open(filename, default_mode, encoding=forced_encoding) else: fo = open(filename, default_mode, encoding=forced_encoding) is_opened = True except: print_to(None, '!!! openfile error: cannot open file') fo = None return fo or [], forced_encoding, is_opened
def _get_page_args(): args = {} try: args.update({ 'subdivision': [ 'subdivision_id', int(get_request_item('subdivision_id') or '-1') ], 'app_role': ['app_role_id', int(get_request_item('app_role_id') or '-1')], 'role': ['role_id', int(get_request_item('role_id') or '-1')], 'confirmed': [ 'confirmed', int(get_request_item('confirmed', check_int=True) or '-1') ], 'enabled': [ 'enabled', int(get_request_item('enabled', check_int=True) or '-1') ], 'app_privilege': ['app_privilege', int(get_request_item('app_privilege') or '-1')], 'id': ['id', get_request_item('_id', check_int=True)], }) except Exception as ex: args.update({ 'subdivision': ['subdivision_id', -1], 'app_role': ['app_role_id', -1], 'role': ['role_id', -1], 'confirmed': ['confirmed', -1], 'enabled': ['enabled', -1], 'app_privilege': ['app_privilege', -1], 'id': ['id', None], }) print_to(errorlog, '!!! admin._get_page_args:%s Exception: %s' % (current_user.login, str(ex)), request=request) flash('Please, update the page by Ctrl-F5!') return args
def _clean(self): if not IsTmpClean: return if not (self._tmp and self.tmp_folder in self._tmp): return try: del_file(self.tmp_body) del_file(self.tmp_image) del_file(self.tmp_xml) except Exception as ex: print_to( None, 'FileImageDecoder.clean Error: [%s] %s' % (self.tmp_folder, str(ex))) if IsPrintExceptions: print_exception()
def receive(action, request, session, **kw): """ Receive XML from **kw (DB) or requested WEB-form, parse DOM and XML-Items and return its to the caller. From **kw(data) or requested WEB-form XML is ready. Arguments: action - 203|204|205|206 for Internal, 303 for Log request - form.data (XML, Internal) session - current session kw - data (XML, DB). Returns: dom - DOM object data - Data Dictionary (XML-Items). """ error = None dom = None print_action(action, 'Exchange.Receive') form = request.form _print_form(form) data = kw.get('data') or form.get('data') or '' # unicode -> utf8 if isinstance(data, unicode): data = data.encode(default_unicode, 'ignore') data = data.strip() if data and data.startswith(CDATA): data = data[9:-3] try: dom = parseString(data) except Exception, error: msg = '--> Invalid XML content!' # --------------- # Unvalid XML DOM # --------------- print_to(errorlog, [msg, data], request=request) #traceback.print_exc(file=open(errorlog, 'a')) raise
def _commit(check_session=True): if IsError: if IsDebug: print '>>> Error' return if check_session: if not (db.session.new or db.session.dirty or db.session.deleted): if IsDebug: print '>>> No data to commit: new[%s], dirty[%s], deleted[%s]' % ( \ len(db.session.new), len(db.session.dirty), len(db.session.deleted)) return try: db.session.commit() except Exception, error: db.session.rollback() if IsDebug: print '>>> Commit Error: %s' % error print_to(errorlog, str(error))
def _commit(check_session=True): if check_session: if not (db.session.new or db.session.dirty or db.session.deleted): if IsDebug: print('>>> No data to commit: new[%s], dirty[%s], deleted[%s]' % ( \ len(db.session.new), len(db.session.dirty), len(db.session.deleted)) ) return try: db.session.commit() except Exception as error: db.session.rollback() if IsDebug: print('>>> Commit Error: %s' % error) print_to(errorlog, str(error)) if IsDebug: print('>>> OK')
def index(): debug, kw = init_response('WebPerso Configurator Page') kw['product_version'] = product_version command = get_request_item('command') if IsDebug: print('--> command:%s, file_id:%s, batch_id:%s' % (command, kw.get('file_id'), kw.get('batch_id'))) refresh() errors = [] if command and command.startswith('admin'): pass kw['errors'] = '<br>'.join(errors) kw['OK'] = '' try: kw = _make_page_default(kw) if IsTrace: print_to(errorlog, '--> profile:%s %s %s' % ( command, current_user.login, str(kw.get('current_file')), ), request=request) except: print_exception() kw['vsc'] = (IsDebug or IsIE or IsForceRefresh) and ( '?%s' % str(int(random.random() * 10**12))) or '' if command and command.startswith('admin'): pass return make_response(render_template('profile.html', debug=debug, **kw))
def execute(self, sql): self.open() if self.engine is None: return None res = None try: res = self.engine.execute(sql) except: print_to(None, 'NO SQL EXEC: %s' % sql) if IsPrintExceptions: print_exception() self.engine_error = True self.close() return res
def run(self, app): emitter = self._config.get('emitter') or False limit = self._config.get('limit') or 0 print_to( None, '>>> Logger Started[%s], root: %s' % ( self._config['ctype'], self._config['root'], )) base = BaseEmitter(args=( app, emitter, limit, self._logger, )) try: base.start() while not (self.stop_requested or base.is_finished()): time.sleep(5) if self.stop_requested: base.should_be_stop() self._processed, self._found = base.stop() except: print_exception() self._out('run exception (look at traceback.log)', is_error=True) finally: base.join() if not IsDisableOutput: self._out('>>> New messages found: %d' % (sum([self._found[x] for x in self._found]) or 0)) self._out('>>> Total processed: %d orders' % self._processed) self._out('>>> Unresolved: %d lines' % app._unresolved_lines())
def decoder(data, encodings, info='', is_trace=False, limit=None, level=0, **kw): image = '' encoding = None if data is None and level == 0 and 'source' in kw: with open(kw['source'], 'rb') as fi: data = fi.read() if data is not None and encodings: n = -1 is_error = False while len(data) > 0: if limit and len(image) > limit: break n = n < len(encodings) - 1 and n + 1 or 0 encoding = encodings[n] errors = encoding == default_iso and 'surrogateescape' or 'strict' try: image += data.decode(encoding, errors=errors) break except UnicodeError as ex: if is_trace: print_to(None, '>>> decoder error:%s encoding:%s [%s:%s] reason:%s [%s]' % ( info, encoding, ex.start, ex.end, ex.reason, data[ex.start and ex.start-1 or 0:ex.end]), ) if is_error and n == len(encodings) - 1: break i, e = decoder(data[ex.start:ex.end], encodings[n+1:], info=info, is_trace=is_trace, limit=limit, level=level+1) if i: image += data[:ex.start].decode(encoding) image += i data = data[ex.end:] n -= 1 else: is_error = True p = 'continuation' in ex.reason and ex.start > 0 and ex.start - 1 or ex.start image += data[:p].decode(encoding) data = data[p:] if is_trace: print_to(None, '>>> decoder done %serrors: [%s]' % (is_error and 'with ' or 'without ', encoding)) if level == 0: data = None del data if is_trace: print_to(None, '>>> decoder image:%s' % len(image)) return image, encoding
def checkfile(filename, mode, encoding, logs, keys, getter, msg, **kw): """ Checks Log-file lines, decodes their and generates Logs-items. Function reads given Log-file and generates Log-items collection matched with the given keys. If passed `lines`, function doesn't make logs and returns lines content only. Arguments: filename -- string: full path to Log-file mode -- string: file open mode (r|rb ...) encoding -- string: preffered encoding to decode messages logs -- list: logged items collection, [output] keys -- iterable: searching keys set getter -- callable: log-item fabric msg -- string: text to output in trace Keyword arguments: forced -- bool: add a new item to logs forced unique -- bool: log unique items only with_count -- bool: count number of unique messages case_insensitive -- bool: if True, use case-insensitive keys check no_span -- bool: if True, don't insert <span> tag inside the log context decoder_trace -- bool: lines decoder trace files -- dict: processed Logs-files seek pointers, [output] lines -- list: obtained Log-lines only, [output] Returns [output] by ref. """ forced = kw.get('forced') or False unique = kw.get('unique') or False with_count = kw.get('with_count') or False case_insensitive = kw.get('case_insensitive') or False no_span = kw.get('no_span') or False decoder_trace = kw.get('decoder_trace') or False files = kw.get('files') or None lines = kw.get('lines') set_globals(kw.get('globals')) line = '' fin, forced_encoding, is_opened = openfile(filename, mode, encoding) is_bytes = 'b' in mode # # Check if keys prepared as a `booleval` Token expression # IsToken = isinstance(keys, Token) if IsToken: token = keys keys = token.get_keys() else: token = None # # Prepare insensitive search # if case_insensitive: if IsToken: pass else: keys = [key.lower() for key in keys] # # Set opposite encoding for decoder-tricks # encodings = ( encoding, get_opposite_encoding(encoding), ) # # Get FSO-pointer for a given Log-file # spointer = files is not None and filename in files and files[ filename] or None # # Check lines-mode only # IsLinesOnly = lines is not None and True or False num_logged = 0 num_line = 0 pointer = 0 try: # # Start reading the file from the last seek-position # if is_opened and spointer is not None: fin.seek(spointer, 0) while is_opened: size = 0 pointer = fin.tell() if pointer == os.path.getsize(filename): break try: line = fin.readline() size = len(line) num_line += 1 if not is_valid_line(line, is_bytes): continue info = '%d:%d:%d' % (num_line, size, pointer) line_save = line # # Decode bytes as string with more preffered encoding # if is_bytes: line, encoding = decoder(line, encodings, info=info, is_trace=decoder_trace) # # Check end of the stream # if not line: if size > 0: if IsDeepDebug and IsLogTrace: print_to( None, '!!! NO LINE DECODED[%s]: %s %s' % (filename, info, line_save)) continue if IsLinesOnly: lines.append(( filename, line, )) num_logged += 1 continue if forced: logs.append(getter(line)) num_logged += 1 continue # # Search keys and add a new item to the logs-collection # logged = checkline(line, logs, keys, getter, token=token, unique=unique, with_count=with_count, case_insensitive=case_insensitive, no_span=no_span) if logged > 0: num_logged += logged except (ValueError, UnicodeError): if IsLogTrace: print_to( None, '>>> INVALID %s LINE[%s]: %s %s' % (msg, filename, info, line)) if IsPrintExceptions: print_exception() if size > 0 and fin.tell() - pointer > size: fin.seek(pointer + size, 0) except: if IsDeepDebug and IsLogTrace: print_to( None, '!!! CHECKFILE ERROR[%s]: %s %s' % (filename, info, line)) if IsPrintExceptions: print_exception() raise except EOFError: pass closefile(fin) if files is not None and pointer > 0: # and num_logged > 0 files[filename] = pointer if IsLogTrace: print_to(None, '--> file: %s %s %s %s [%d]: %d' % ( \ mdate(filename), filename, forced_encoding, is_opened, num_logged, pointer ))
def index(): debug, kw = init_response('WebPerso Persostation Page') kw['product_version'] = product_version is_admin = current_user.is_administrator() is_operator = current_user.is_operator() command = get_request_item('command') order_id = int(get_request_item('order_id') or '0') batch_id = int(get_request_item('batch_id') or '0') if IsDebug: print('--> command:%s, order_id:%s, batch_id:%s' % (command, order_id, batch_id)) refresh(order_id=order_id) IsMakePageDefault = True logsearch = '' tagsearch = '' info = '' errors = [] if command.startswith('admin'): command = command.split(DEFAULT_HTML_SPLITTER)[1] if get_request_item('OK') != 'run': command = '' elif not is_operator: flash('You have not permission to run this action!') command = '' if IsDebug: print('--> %s' % info) if IsTrace: print_to(errorlog, '--> command:%s %s [%s]' % (command, current_user.login, info)) kw['errors'] = '<br>'.join(errors) kw['OK'] = '' try: if IsMakePageDefault: kw = _make_page_default(kw) if IsTrace: print_to(errorlog, '--> persostation:%s %s [%s:%s] %s %s' % ( command, current_user.login, request.remote_addr, kw.get('browser_info'), str(kw.get('current_file')), info,), request=request) except: print_exception() kw['vsc'] = vsc() if command: if not command.strip(): pass elif command == 'export': return _make_xls_content(_make_export(kw), 'Журнал инкассации') return make_response(render_template('persostation.html', debug=debug, **kw))
def respond_external(action, **kw): exchange_error = 0 exchange_message = '' error_code = '' error_description = '' errors = '' response = {} dom = None data = '' total = '' price = '' currency = '' document = '' order_number = '' order_date = '' print_action(action, 'Respond.External') locale = request.form.get('currentUsedLocalization') or '' wizard = request.form.get('wizardID') or '' try: # ------------------------------------- # Get DOM and XML response from Service # ------------------------------------- exchange_error, exchange_message, dom, data = send(action, request, session, **kw) if demo(): exchange_error, exchange_message, total, currency = _demo_price(action) order_number, order_date = _demo_order(action) elif exchange_error: total = 0.0 elif dom is not None: total = float(getDOMItemValue(dom, 'total') or '0') currency = CURRENCIES.get(getDOMItemValue(dom, 'currency'), gettext('undefined')) error_code = getDOMItemValue(dom, 'errorCode').strip() error_description = getDOMItemValue(dom, 'errorDescription') if action == '207': order = _order(dom) order_number = order.get('number', '') order_date = order.get('date', '') elif data: x = request.form.get('price') total = x and float(x.split()[0])*1.288726 or 0 except: msg = '--> Send error!' print_to(errorlog, [msg, data], request=request) # ---------------------- # Service Exchange Error # ---------------------- if IsDeepDebug: print msg raise #print_to(errorlog, ['>>> Data:', data]) IsValid = data and True or False if exchange_message and exchange_message == exchange_error: exchange_message = '' if error_description and error_description == error_code: error_description = '' # ----------------- # Response is valid # ----------------- if IsValid: errors = getDOMErrors(dom) or '' if IsDeepDebug: print errors if currency in ('undefined', n_a, '') or not total: if not exchange_message: if action == '203' and error_code in ('', '0',): pass else: exchange_message = gettext('Calculation is not performed.') IsValid = False total = IsValid and '%.2f' % total or '' price = IsValid and '%s %s' % (total, currency) or '' if IsDebug: print '--> Total: %s' % price document = order_number and ('# %s %s %s' % (order_number, gettext('at'), order_date)) or '' # ------------------------------------------- # Make parameters and Register response in DB # ------------------------------------------- attrs = { \ 'locale' : locale, 'selected_item' : kw.get('id') or request.form.get('selected_item'), 'title' : request.form.get('title') or '', 'document' : document or action, 'total' : total, 'currency' : currency, 'countryID' : getCountryID(getDOMItemValue(dom, 'countryID'), locale), 'regionID' : getRegionID(getDOMItemValue(dom, 'regionID'), locale), 'userID' : getUserID(getDOMItemValue(dom, 'userID'), locale), 'userName' : getClient(getDOMItemValue(dom, 'userName'), locale), 'wizardID' : wizard, 'wizardName' : request.form.get('wizardName') or '', 'custom_code' : request.form.get('custom_code') or '', 'option_update' : request.form.get('option_update') or '', 'option_cost' : request.form.get('option_cost') or '', 'data' : data, #getDOMTagStrippedValue(dom, 'parameters'), } #print_to(errorlog, ['>>> Total:', total]) if IsValid and action in ('203','204','205','207',): # and dom response = register(action, dom, attrs) if IsDeepDebug: print '>>> DB Response:%s' % response order = action == '205' and response.get('custom_code') or document return { \ 'action' : action, 'op' : '', # -------------- # Service Errors # -------------- 'exchange_error' : exchange_error, 'exchange_message' : exchange_message, 'error_code' : error_code, 'error_description' : error_description, 'errors' : errors, # --- # IDs # --- 'countryID' : getDOMItemValue(dom, 'countryID') or '', 'regionID' : getDOMItemValue(dom, 'regionID') or '', 'userID' : attrs['userID'], # -------------- # Client Details # -------------- 'country_name' : getDOMItemValue(dom, 'countryName') or getCountry(getDOMItemValue(dom, 'countryID'), locale), 'region_name' : getDOMItemValue(dom, 'regionName') or getRegion(getDOMItemValue(dom, 'regionID'), locale), 'client_name' : attrs['userName'], # ---------- # Order Info # ---------- 'document_number' : order_number, 'document_date' : order_date, 'order' : order, # ------- # DB Data # ------- 'total_log_rows' : getLogTotal({'userID' : attrs['userID'], 'wizardID' : wizard}), 'custom_code' : response.get('custom_code', ''), 'next_custom_code' : response.get('next_custom_code', ''), 'option_update' : response.get('option_update', ''), 'option_cost' : response.get('option_cost', ''), 'title' : response.get('title', ''), # ------------------------------ # Results (Price & XML-Response) # ------------------------------ 'price' : price, 'data' : data, }
def respond_log(action): exchange_error = 0 exchange_message = '' page = None current_page, pages, per_page, has_prev, has_next, total, id = (0, 0, 0, False, False, 0, None) iter_pages = [] order = None data = None dom = None items = None title = '' print_action(action, 'Respond.Log') locale = request.form.get('currentUsedLocalization') or '' try: # ---------------- # Get Data from DB # ---------------- if not action: pass elif action == '301': exchange_error, exchange_message, page, data = getLogPage(action, request, session) elif action == '305': exchange_error, exchange_message, id, total, data = getStatictics(action, request, session) elif action == '308': exchange_error, exchange_message, page, data = removeLogItem(action, request, session) else: #if action in ('302','303','304','307',): exchange_error, exchange_message, id, order = getLogItem(action, request, session) except: msg = '--> Database error!' # ------------- # DB Log failed # -------------- print_to(errorlog, msg, request=request) raise if not (action and action in valid_action_types): pass elif action in ('301','308',): # -------------------- # Get Log-page content # -------------------- if page: current_page, pages, per_page, has_prev, has_next, total = page.get_page_params() iter_pages = page.iter_pages() if IsDebug: print '--> LogPage[%s]: items:[%s] %s-%s-%s-%s iter:%s' % ( \ current_page, len(data), pages, per_page, has_prev, has_next, iter_pages) elif action in ('302',): # ------------------------------ # DOM and XML-Items (Dictionary) # ------------------------------ dom, data = receive(action, request, session, data=order.data) #, products_sorted=True if IsDebug: print '--> LogPageItem[%s]: Data %s, Products %s, Parameters %s' % ( \ id, len(order.data), len(data['products']), len(data['parameters'])) elif action in ('303','304','307',): # --------- # Clean XML # --------- data = order.data #_data(coding='encode') # ------------------------------ # DOM and XML-Items (Dictionary) # ------------------------------ dom, items = receive(action, request, session, data=data) title = order.title if IsDebug: print '--> Loaded LogPageItem[%s]: Data %s, Products %s, Parameters %s' % ( \ id, len(order.data), len(items['products']), len(items['parameters'])) elif action in ('305',): if IsDebug: print '--> LogStatictics[%s]: Data %s' % ( \ id, data and len(data), ) if exchange_error: if IsDebug: print '--> ExchangeError[%s]: %s' % (exchange_error, exchange_message) return { \ 'action' : action, 'op' : '', # -------------- # Service Errors # -------------- 'exchange_error' : exchange_error, 'exchange_message' : exchange_message, # ----------------------------- # Results (Log page parameters) # ----------------------------- 'id' : id, 'rows_on_page' : len(data), 'total' : total, 'page' : current_page, 'pages' : pages, 'per_page' : per_page, 'has_prev' : has_prev, 'has_next' : has_next, 'iter_pages' : iter_pages, # -------------------------- # Results (Log page content) # -------------------------- 'custom_code' : order and order.code or '', 'title' : title, 'price' : '', 'data' : data, 'dom' : dom, 'items' : items, }
def check_infoexchange_log(logs, filename, encoding=default_encoding, **kw): keys = kw.get('keys') split_by = kw.get('split_by') or '\t' columns = kw.get('columns') fmt = kw.get('fmt') date_format = kw.get('date_format') or DEFAULT_DATETIME_INLINE_FORMAT case_insensitive = kw.get('case_insensitive') no_span = kw.get('no_span') forced = kw.get('forced') and True or False original_logger = kw.get('original_logger') and True or False def _get_log_item(line): if not line: return None values = line.split(split_by) if len(values) != len(columns): return None ob = {'filename': filename} try: for n, column in enumerate(columns): ob[column] = values[n].strip() except: if IsDebug and IsPrintExceptions: print_exception() try: x = datetime.datetime.strptime('%s' % ob['Date'], fmt[1]) ob['Date'] = cdate(x, date_format) except: if IsDebug and IsPrintExceptions: print_exception() if 'Code' in ob: ob['Code'] = re.sub(r'[\[\]]', '', ob['Code'].upper()) return ob if IsLogTrace: print_to(None, '--> file: %s %s' % (mdate(filename), filename)) # --------------------------------------------- # Output Log from native view (Original Logger) # --------------------------------------------- if original_logger: fin, forced_encoding, is_opened = openfile(filename, 'r', encoding=encoding) IsOutput = IsFound = False lines = [] for line in fin: try: if forced: logs.append(_get_log_item(line)) continue if not IsOutput and 'Version' in line: IsOutput = True for key in keys: line, is_found = _findkey( line, key, case_insensitive=case_insensitive, no_span=no_span) if is_found: IsFound = True if IsOutput: lines.append(_get_log_item(line)) if 'Выход' in line: if IsFound: logs += list(filter(None, lines)) IsOutput = IsFound = False lines = [] except: if IsLogTrace: print_to( None, '>>> INVALID INFOEXCHAGE LINE[%s]: %s' % (filename, line)) print_exception() if lines and IsFound: logs += list(filter(None, lines)) closefile(fin) return # ----------------- # Used in LogSearch # ----------------- checkfile( filename, 'rb', encoding, logs, keys, getter=_get_log_item, msg='INFOEXCHANGELOG', forced=forced, case_insensitive=case_insensitive, no_span=no_span, files=kw.get('files'), )
def checkline(line, logs, keys, getter, **kw): """ Checks the Log-file line and makes a new logs-item """ token = kw.get('token') or None unique = kw.get('unique') or False with_count = kw.get('with_count') or False case_insensitive = kw.get('case_insensitive') or False no_span = kw.get('no_span') or False mcre = re.compile(r'\[(\d+)\]') logged = 0 def _has_unique(ob): # # Call it if the log-item should be unique # for log in logs: if log.get('filename') == ob.get('filename'): if log.get('Date') == ob.get('Date') and log.get( 'Message') == ob.get('Message'): s = 'Module' if with_count and s in log: module = log[s] or '' m = mcre.search(module) if m: cnt = int(m.group(1) or '1') else: module += '[1]' cnt = 1 log[s] = re.sub(r'\d+', str(cnt + 1), module) return True return False def _is_ignore_line(line): for x in config.get('ignore', []): if x and x in line: return True return False if line: # # Check if line should be ignored # if _is_ignore_line(line): return -1 IsFound = False # # Search given keys # if token is not None: for key in keys: line, is_found = _findkey(line, key['value'], case_insensitive=case_insensitive, no_span=no_span) key['res'] = is_found token.set_values(keys) IsFound = token() else: for key in keys: line, is_found = _findkey(line, key, case_insensitive=case_insensitive, no_span=no_span) if is_found: IsFound = True break # # Add a new item to logs # if IsFound: item = getter(line) if item is not None and not (unique and _has_unique(item)): logs.append(item) logged = 1 else: if IsLogTrace: print_to(None, '!!! empty line[%s]' % line) return logged
def render(self, **kw): """ Render Seller data by the given ID """ orders = [] items = {} self._started = getToday() if IsTrace: print_to(None, '>>> seller started') columns = ( 'np', #'TID', 'Article', 'Qty', 'Subdivision', 'Price', 'Currency', 'Total', #'Condition', #'RD', ) seller = self._get_seller(self.id) where = 'SellerID=%s' % self.id total = 0 sum_price = Decimal(0.0) n = 0 for order in self._get_orders(where=where): order_id = order['TID'] """ if not order['Price']: continue """ n += 1 order['np'] = n info = self._info(order_id=order_id, no_extra=True, **order) items[order_id] = info[1] euro = re.sub(r'\s', '', info[1].get('EUR') or '') total += 1 sum_price += euro and Decimal(euro) or 0 if not order['Price']: order['Price'] = '0' order['Total'] = '0.00' if not order['Currency']: order['Currency'] = 'RUR' orders.append(order) if IsTrace: print_to(None, '>>> seller %02d: %s' % (n, getToday())) keys = ( 'num', 'equipment_title', 'purpose', 'author', 'EUR', ) headers = { 'num': 'Номер заявки', 'equipment_title': 'Описание', 'purpose': 'Обоснование', 'author': 'Автор', 'EUR': 'Цена в ЕВРО', } data = { 'seller': seller, 'columns': columns, 'headers': headers, 'keys': keys, 'orders': orders, 'items': items, 'total': total, 'sum_price': self._get_money(str(sum_price)), } self._finished = getToday() if IsTrace: print_to( None, '>>> seller finished: %s sec' % spent_time(self._started, self._finished)) return data
def login(): if IsDeepDebug: print('--> current_user.is_active:%s' % current_user.is_active) form = LoginForm() login = form.login.data if login and form.validate_on_submit(): user = User.query.filter_by(login=login).first() else: user = None if user is not None: accessed_link = get_default_url(user) is_valid_password = user.verify_password(form.password.data) is_admin = user.is_administrator() is_confirmed = user.confirmed is_enabled = user.enabled if IsDeepDebug: print( '--> user:%s valid:%s enabled:%s is_admin:%s' % (user and user.login, is_valid_password, is_enabled, is_admin)) IsEnabled = False if not is_enabled: flash('Access to the service is denied!') elif not is_valid_password: flash('Password is incorrect!') elif not is_confirmed: flash('You should change you password!') accessed_link = url_for('auth.change_password') IsEnabled = True elif 'admin' in accessed_link and not is_admin: flash('You cannot access this page!') else: IsEnabled = True if IsDeepDebug: print('--> link:%s enabled:%s' % (accessed_link, IsEnabled)) if IsTrace: print_to(errorlog, '\n==> login:%s %s enabled:%s' % (user.login, request.remote_addr, is_valid_password), request=request) if IsEnabled: try: login_user(user, remember=form.remember_me.data) except Exception as ex: print_to(errorlog, '!!! auth.login error: %s %s' % (login, str(ex))) if IsPrintExceptions: print_exception() if accessed_link in ('default', '/'): return menu() return redirect(accessed_link) elif login: if IsTrace: print_to( errorlog, '\n==> login:%s is invalid!!! %s' % ( login, request.remote_addr, )) flash('Invalid username or password.') kw = make_platform(mode='auth') kw.update({ 'title': gettext('WebPerso Login'), 'page_title': gettext('WebPerso Auth'), 'header_class': 'middle-header', 'show_flash': True, 'semaphore': { 'state': '' }, 'sidebar': { 'state': 0, 'title': '' }, }) kw['vsc'] = vsc() link = 'auth/login%s.html' % (IsEdge() and '_default' or '') return render_template(link, form=form, **kw)
def loader(): exchange_error = '' exchange_message = '' refresh() action = get_request_item('action') or default_action selected_menu_action = get_request_item('selected_menu_action') or action != default_action and action or '501' response = {} order_id = int(get_request_item('order_id') or '0') event_id = int(get_request_item('event_id') or '0') filter = [ engine.getReferenceID('orderstate-clients', key='TID', value=get_request_item('filter-client'), tid='Name'), engine.getReferenceID('orderstate-actions', key='TID', value=get_request_item('filter-action'), tid='Name'), engine.getReferenceID('orderstate-configs', key='TID', value=get_request_item('filter-config'), tid='Name'), get_request_item('filter-type'), get_request_item('filter-search-context'), ] x = filter[-1] if x and len(x) > 1 and x[1] == ':': filter[2] = ''.join(x.split(':')[1:]) if IsDebug: print('--> action:%s order_id:%s event_id:%s filter:%s-%s-%s-%s-%s' % ( action, order_id, event_id, filter[0], filter[1], filter[2], filter[3], filter[4] )) if IsTrace: print_to(errorlog, '--> loader:%s %s [%s:%s:%s]' % (action, current_user.login, order_id, event_id, selected_menu_action)) currentfile = None events = [] config = None data = '' number = '' columns = [] props = None try: if action == default_action: order = _get_order(order_id) events, event_id = _get_events(order_id) currentfile = [order_id, order['PackageName'], event_id] config = _get_view_columns(database_config['orderstate-events']) action = selected_menu_action if not action: pass elif action == '501': data, props = getTabEventInfo(event_id) elif action == '502': columns = _get_view_columns(database_config['orderstate-files']) data = getTabFiles(order_id, filter=filter) elif action == '503': columns = _get_view_columns(database_config['orderstate-errors']) data = getTabErrors(order_id, filter=filter) elif action == '504': columns = _get_view_columns(database_config['orderstate-certificates']) data = getTabCertificates(order_id, filter=filter) elif action == '505': columns = _get_view_columns(database_config['orderstate-aliases']) data = getTabAliases(order_id, filter=filter) elif action == '506': columns = _get_view_columns(database_config['orderstate-log']) data = getTabInfoExchangeLog(database_config['orderstate-log']['columns'], order_id=order_id, filter=filter) except: print_exception() response.update({ 'action' : action, # -------------- # Service Errors # -------------- 'exchange_error' : exchange_error, 'exchange_message' : exchange_message, # ----------------------------- # Results (Log page parameters) # ----------------------------- 'order_id' : order_id, 'event_id' : event_id, # --------------------------------------------- # Default Lines List (sublines equal as events) # --------------------------------------------- 'currentfile' : currentfile, 'sublines' : events, 'config' : config, # -------------------------- # Results (Log page content) # -------------------------- 'total' : len(data), 'data' : data, 'props' : props, 'columns' : columns, }) return jsonify(response)
def lines_emitter(filename, mode, encoding, msg, **kw): """ Emitter of Log-file lines, decodes their and generates Logs-items. Arguments: filename -- string: full path to Log-file mode -- string: file open mode (r|rb ...) encoding -- string: preffered encoding to decode messages msg -- string: text to output in trace Keyword arguments: decoder_trace -- bool: lines decoder trace files -- dict: processed Logs-files seek pointers, [output] """ decoder_trace = kw.get('decoder_trace') or False files = kw.get('files') or None set_globals(kw.get('globals')) line = '' fin, forced_encoding, is_opened = openfile(filename, mode, encoding) is_bytes = 'b' in mode # # Set opposite encoding for decoder-tricks # encodings = ( encoding, get_opposite_encoding(encoding), ) # # Get FSO-pointer for a given Log-file # spointer = files is not None and filename in files and files[ filename] or None num_line = 0 pointer = 0 try: # # Start reading the file from the last seek-position # if is_opened and spointer is not None: fin.seek(spointer, 0) while is_opened: size = 0 pointer = fin.tell() if pointer == os.path.getsize(filename): break try: line = fin.readline() size = len(line) num_line += 1 if not is_valid_line(line, is_bytes): continue info = '%d:%d:%d' % (num_line, size, pointer) line_save = line # # Decode bytes as string with more preffered encoding # if is_bytes: line, encoding = decoder(line, encodings, info=info, is_trace=decoder_trace) # # Check end of the stream # if not line: if size > 0: if IsDebug or IsLogTrace: print_to( None, '!!! NO LINE DECODED[%s]: %s\n%s' % (filename, info, line_save)) continue # # Generate a new line output # yield line.strip() except (ValueError, UnicodeError): if IsLogTrace: print_to( None, '>>> INVALID %s LINE[%s]: %s\n%s' % (msg, filename, info, line)) if IsPrintExceptions: print_exception() if size > 0 and fin.tell() - pointer > size: fin.seek(pointer + size, 0) except: if IsLogTrace: print_to( None, '!!! EMITTER ERROR[%s]: %s\n%s' % (filename, info, line)) if IsPrintExceptions: print_exception(1) break else: raise except EOFError: pass closefile(fin) if files is not None and pointer > 0: files[filename] = pointer if IsLogTrace: print_to(None, '--> file: %s %s %s %s [%d]: %d' % ( \ mdate(filename), filename, forced_encoding, is_opened, num_line, pointer ))