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(self, row): self._print("Change attribute values...") kwargs = {} for aname, typ, default in database.get_columns(row.__class__): default = getattr(row, aname) ok = self._get_input(aname, typ, default, kwargs) if not ok: return row.set(**kwargs)
def _create(self, klass): self._print("Enter following values for a %s object..." % (klass.__name__,)) kwargs = {} for aname, typ, default in database.get_columns(klass): if default is database.NoDefault: ok = self._get_input(aname, typ, None, kwargs) else: ok = self._get_input(aname, typ, default, kwargs) if ok: inst = klass(**kwargs) return inst else: return None
def set(self, argv): """set <col> <value> Set the column to the given value.""" colname = argv[1] value = tuple(argv[2:]) for aname, typ, default in database.get_columns(self._obj.__class__): if aname == colname: try: value = typ(*value) except ValueError, err: self._print(err) else: setattr(self._obj, colname, value) return
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 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)
def _showrow(self, row): s = [] for aname, typ, default in database.get_columns(row.__class__): s.append("%20.20s (%10.10s) : %r" % (aname, typ.__name__, getattr(row, aname))) self._print("\n".join(s))
def _setup(self, obj, name, id): self._obj = obj self._colinfo = database.get_columns(self._obj.__class__) self._environ["PS1"] = "%s(%%I%s%%N)> " % (name, id) self._reset_scopes()
def get_columns(self, table): return database.get_columns(self.conn, table)