예제 #1
0
파일: cli.py 프로젝트: tijmengit/pycopia
 def owner(self, argv):
     """owner [<name>]
 Set the owner of this container to <name>, of possible.
 The special name "delete" will remove ownership."""
     name = argv[1] if len(argv) > 1 else None
     if not _user.is_superuser:
         self._ui.warning("Only superuser accounts can change ownership")
         return
     if name is None:
         current = self._obj.user
         choices = models.get_choices(_session, models.Config, "user", "username")
         choices.insert(0, (0, "Nobody"))
         chosen_id = self._ui.choose_key(dict(choices), current.id if current is not None else 0, "%Iuser%N")
         if chosen_id == 0:
             config.Container(_session, self._obj, user=_user).set_owner(None)
         else:
             relmodel = models.Config.user.property.mapper.class_
             related = _session.query(relmodel).get(chosen_id)
             config.Container(_session, self._obj, user=_user).set_owner(related)
     else:
         if name.startswith("delete"):
             config.Container(_session, self._obj, user=_user).set_owner(None)
         else:
             user = models.User.get_by_username(_session, name)
             if user:
                 config.Container(_session, self._obj, user=_user).set_owner(user)
             else:
                 self._ui.error("No such user.")
예제 #2
0
def new_relation_input(node, modelclass, metadata, order_by=None):
    choices = models.get_choices(dbsession, modelclass, metadata.colname, order_by)
    if not choices:
        return node.new_para("%s has no choices." % metadata.colname)
    if metadata.nullable:
        choices.insert(0, (0, "----"))
    elid = "id_" + metadata.colname
    node.add_label(metadata.colname, elid)
    return node.add_select(choices, name=metadata.colname, multiple=metadata.uselist, id=elid)
예제 #3
0
def new_relation_input(node, modelclass, metadata, order_by=None):
    choices = models.get_choices(dbsession, modelclass, metadata.colname,
                                 order_by)
    if not choices:
        return node.new_para("%s has no choices." % metadata.colname)
    if metadata.nullable:
        choices.insert(0, (0, "----"))
    elid = "id_" + metadata.colname
    node.add_label(metadata.colname, elid)
    return node.add_select(choices,
                           name=metadata.colname,
                           multiple=metadata.uselist,
                           id=elid)
예제 #4
0
파일: cli.py 프로젝트: tijmengit/pycopia
def edit_relation_input(ui, modelclass, metadata, dbrow):
    choices = models.get_choices(_session, modelclass, metadata.colname, None)
    if not choices:
        ui.Print("%s has no choices." % metadata.colname)
        if metadata.uselist:
            if metadata.collection == "MappedCollection":
                setattr(dbrow, metadata.colname, {})
            else:
                setattr(dbrow, metadata.colname, [])
        else:
            setattr(dbrow, metadata.colname, None)
        return
    choices = dict(choices)
    current = getattr(dbrow, metadata.colname)
    relmodel = getattr(modelclass, metadata.colname).property.mapper.class_
    if metadata.uselist:
        if metadata.collection == "MappedCollection":
            chosen = dict((crow.id, str(crow)) for crow in current.values())
        else:
            chosen = dict((crow.id, str(crow)) for crow in current)
        for chosenone in chosen:
            try:
                del choices[chosenone]
            except KeyError: # choice may have gone away.
                pass
        chosen = ui.choose_multiple_from_map(choices, chosen, "%%I%s%%N" % metadata.colname)
        if chosen:
            t = _session.query(relmodel).filter( relmodel.id.in_(chosen.keys())).all()
        else:
            t = []
        if not t and metadata.nullable:
            t = None
        if metadata.collection == "MappedCollection" and t is not None:
            col = getattr(dbrow, metadata.colname)
            for val in t:
                col.set(val)
        else:
            setattr(dbrow, metadata.colname, t)
    else:
        if metadata.nullable:
            choices[0] = "Nothing"
        chosen_id = ui.choose_key(choices, current.id if current is not None else 0,
                "%%I%s%%N" % metadata.colname)
        if chosen_id == 0: # indicates nullable
            setattr(dbrow, metadata.colname, None)
        else:
            related = _session.query(relmodel).get(chosen_id)
            setattr(dbrow, metadata.colname, related)
예제 #5
0
파일: cli.py 프로젝트: tijmengit/pycopia
def new_relation_input(ui, modelclass, metadata):
    choices = models.get_choices(_session, modelclass, metadata.colname, None)
    if not choices:
        ui.Print("%s has no choices." % metadata.colname)
        if metadata.uselist:
            return []
        else:
            return None
    if metadata.uselist:
        return ui.choose_multiple_from_map(dict(choices), None, "%%I%s%%N" % metadata.colname).keys()
    else:
        if metadata.nullable:
            choices.insert(0, (0, "Nothing"))
            default = 0
        else:
            default = choices[0][0]
        return ui.choose_key(dict(choices), default, "%%I%s%%N" % metadata.colname)
예제 #6
0
def get_choices(modelname, attribute, order_by=None):
    modelclass = get_model(modelname)
    return models.get_choices(webhelpers.dbsession, modelclass, attribute, order_by)