def home(): try: persons = get_model_by_tablename('persons') orgs = get_model_by_tablename('organizations') persons_count = len(session.query(persons).all()) orgs_count = len(session.query(orgs).all()) context = { 'persons_count': persons_count, 'orgs_count': orgs_count, 'last_update': updater.last_update } return render_template('home.html', **context) except Exception as e: logger.error('Error occurred. Details: {}'.format(e)) abort(500)
def put(table): try: model = get_model_by_tablename(table) data = request.get_json() idx = data['index'] obj = session.query(model).get(idx) # неизмененное состояние строки note = '' flag = False for k in data.keys(): if data[k] != obj.__dict__[k] and data[k] != 'None': flag = True changed_row = '<{}>: "{}" -> "{}".'.format(k, obj.__dict__[k], data[k]) note += changed_row if flag: session.query(model).filter_by(index=idx).update(data) last_idx = len(session.query(History).all()) hist = History(index=last_idx, table=table, obj_id=int(idx), note=note) session.add(hist) res = make_response(jsonify({"message": "OK"}), 200) logger.info('Data changed in "{}" table. Object by index: {}'.format(table, idx)) return res except Exception as e: logger.error('Error occurred. Details: {}'.format(e)) abort(500)
def add(table): model = get_model_by_tablename(table) columns = get_columns(model) context = { 'table': table, 'columns': columns[1:], } return render_template('add.html', **context)
def edit(table, idx): try: model = get_model_by_tablename(table) columns = get_columns(model) obj = session.query(model).get(idx) history = session.query(History).filter(and_(History.table == table, History.obj_id == idx)).all() context = { 'columns': columns, 'obj': obj, 'history': history } return render_template('edit.html', **context) except Exception as e: logger.error('Error occurred. Details: {}'.format(e)) abort(500)
def delete(table): try: model = get_model_by_tablename(table) idxs = request.get_json() for idx in idxs: if idx != 'selectall': obj = session.query(model).get(int(idx)) session.delete(obj) flash('Записи удалены', 'delete') res = make_response(jsonify({"message": "OK"}), 200) logger.info('Deleted {} rows from "{}" table'.format(len(idxs), table)) return res except Exception as e: logger.error('Error occurred. Details: {}'.format(e)) abort(500)
def send(table): try: model = get_model_by_tablename(table) last_index = len(session.query(model).all()) objs = [] data = request.get_json() for i in range(len(data)): data[i]['index'] = last_index + i objs.append(model(**data[i])) session.bulk_save_objects(objs) flash('Записи добавлены', 'insert') res = make_response(jsonify({"message": "OK"}), 200) logger.info('Added {} rows to "{}" table'.format(len(data), table)) return res except Exception as e: logger.error('Error occurred. Details: {}'.format(e)) abort(500)
def search(): url = request.full_path table = request.args.get('category') model = get_model_by_tablename(table) columns = get_columns(model) status = request.args.get('status') if request.args.get('page'): p = int(request.args.get('page')) url = request.full_path.replace('&page={}'.format(p), '') else: p = 1 q = '%{}%'.format(request.args['q']) variables = { 'model': model, 'q': q } expression = (eval('model.{}.ilike(q)'.format(column), variables) for column in columns[1:]) # eval - unsafe func if status == 'all': results = session.query(model).filter(or_(expression)) else: results = session.query(model).filter(or_(expression)).filter(model.status == status) total_count = results.count() pagination = Pagination(page=p, total=total_count, per_page=records_num, bs_version='4', href=url + '&page={}', alignment='center') if p == 1: data = results.limit(records_num) elif 1 < p < pagination.total_pages + 1: data = results.limit(records_num).offset(records_num * (p - 1)) context = { 'table': table, 'data': data, 'columns': columns, 'pagination': pagination } return render_template('result.html', **context)
def page(table, p): try: model = get_model_by_tablename(table) columns = get_columns(model) # pagination total_count = len(session.query(model).all()) pagination = Pagination(page=p, total=total_count, per_page=records_num, bs_version='4', href='{}', alignment='center') if p == 1: data = session.query(model).order_by(model.index).limit(records_num) elif 1 < p < pagination.total_pages + 1: data = session.query(model).order_by(model.index).limit(records_num).offset(records_num * (p - 1)) else: return redirect(url_for('page', table=table, p=1)) context = { 'table': table, 'data': data, 'columns': columns, 'pagination': pagination } return render_template('table.html', **context) except Exception as e: logger.error('Error occurred. Details: {}'.format(e)) abort(500)