Esempio n. 1
0
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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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
Esempio n. 4
0
 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
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
 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))
Esempio n. 9
0
 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()
Esempio n. 10
0
 def get_columns(self, table):
     return database.get_columns(self.conn, table)