Пример #1
0
 def _get_query(self, argv):
     mapper = models.class_mapper(self._obj)
     args, kwargs = _query_args(argv[1:], self._environ)
     q = _session.query(self._obj)
     if args:
         grps, left = divmod(len(args), 3)
         if grps:
             for name, op, val in _by_three(args[:grps*3]):
                 col = getattr(self._obj, name)
                 opm = {"=": col.__eq__,
                         ">": col.__gt__,
                         "<": col.__lt__,
                         "match": col.match,
                         "contains": col.contains,
                         "in": col.in_,
                         "like": col.like}.get(op)
                 if opm:
                     if op == "like":
                         val = val.replace("*", "%")
                         val = val.replace(".", "_")
                         if "%" not in val:
                             val = "%" + val + "%"
                     if op == "in":
                         val = val.split(",")
                     q = q.filter(opm(val))
         for name in args[grps*3:]:
             if name.startswith("="):
                 q = q.order_by(name[1:])
     if kwargs:
         for name, value in kwargs.items():
             col = getattr(self._obj, name)
             value = CLI.clieval(value)
             q = q.filter(col.__eq__(value))
     return q
Пример #2
0
 def count(self, argv):
     """count [<criteria>]
 Show the count of row selected by criteria.
 """
     mapper = models.class_mapper(self._obj)
     pkname = str(mapper.primary_key[0].name)
     q = self._get_query(argv)
     self._print(q.count())
Пример #3
0
 def ls(self, argv):
     """ls [<criteria>]
 List all current entries, or entries filtered by criteria.
 Criteria can be a simple expression of groups of name, operator, and
 value. a name prefixed by "=" means order by that name.
 """
     mapper = models.class_mapper(self._obj)
     pkname = str(mapper.primary_key[0].name)
     q = self._get_query(argv)
     if q._statement is None:
         q = q.order_by(pkname)
     heading = models.get_rowdisplay(self._obj)
     fmtl = []
     hfmtl = []
     rows, cols = self._ui.get_winsize()
     tw = 0
     cols -= 8
     for colname in heading:
         md = models.get_column_metadata(self._obj, colname)
         fmt, length = _FORMATS.get(md.coltype, ("{!s:10.10}", 10)) # default width 10
         cols -= length
         if cols <= 0:
             break
         fmtl.append(fmt)
         side = ">" if ">" in fmt else "<"
         hfmtl.append("{{:{side}{fw}.{fw}}}".format(side=side, fw=length))
     fmt = "{!s:6.6s}: " + " ".join(fmtl)
     hfmt = "{!s:6.6s}: " + " ".join(hfmtl)
     ln = 0 ; rows -= 3
     try:
         for item in q.all():
             if ln % rows == 0:
                 self._ui.printf("%B" + hfmt.format(pkname, *heading) + "%N")
             try:
                 self._print(fmt.format(getattr(item, pkname), *[getattr(item, hn) for hn in heading]))
             except ValueError:
                 self._ui.printf("%R{!s:6.6s}%N: {}".format(getattr(item, pkname), str(item)))
             ln += 1
     except:
         _session.rollback()
         raise
Пример #4
0
 def get_prompt(cls, dbrow):
     cls._metadata = sorted(models.get_metadata(dbrow.__class__))
     mapper = models.class_mapper(dbrow.__class__)
     pkname = str(mapper.primary_key[0].name)
     return "%%I%s%%N:%s> " % (getattr(dbrow, pkname), dbrow)
Пример #5
0
    Returns the new items primary key values.
    """
    klass = get_model(modelname)
    dbrow = klass()
    try:
        webhelpers.update_row_from_data(data, klass, dbrow)
    except types.ValidationError, err:
        webhelpers.dbsession.rollback()
        raise
    webhelpers.dbsession.add(dbrow)
    try:
        webhelpers.dbsession.commit()
    except (DataError, IntegrityError), err:
        webhelpers.dbsession.rollback()
        raise
    mapper = models.class_mapper(klass)
    return [getattr(dbrow, col.name) for col in mapper.primary_key]


def related_add(modelname, entry_id, colname, relmodelname, rel_id):
    klass = get_model(modelname)
    relklass = get_model(relmodelname)
    metadata = models.get_column_metadata(klass, colname)
    # fetch parent and related objects
    dbrow = webhelpers.get_row(klass, entry_id)
    reldbrow = webhelpers.get_row(relklass, rel_id)
    # now add using appropriate semantics
    if metadata.uselist:
        col = getattr(dbrow, colname)
        col.append(reldbrow)
    else: