Beispiel #1
0
def process_args(args, cls):
    mapper = sqlainspect(cls)
    for key, value in args.iteritems():
        column = mapper.c.get(key)
        if column is not None:
            if isinstance(column.type, DeclEnumType):
                yield (key, column.type.enum.from_string(value))
            elif column.type.python_type == int:
                yield (key, int(value))
            elif column.type.python_type == float:
                yield (key, float(value))
            elif column.type.python_type == bool:
                yield (key, asbool(value))
            else:
                yield (key, value)
            continue
        reln = mapper.relationships.get(key)
        if (reln is not None and reln.direction.name == 'MANYTOONE'
                and isinstance(value, (str, unicode))):
            assert (len(reln.local_columns) == 1)
            key = next(reln.local_columns.__iter__()).key
            yield (key, reln.mapper.class_.get_database_id(value))
            continue
        attribute = getattr(cls, key, None)
        if isinstance(attribute, property) and attribute.fset is not None:
            yield (key, value)
            continue
Beispiel #2
0
 def create_object(self, typename=None, json=None, user_id=None, **kwargs):
     cls = self.get_collection_class(typename)
     permissions = get_permissions(user_id, self.get_discussion_id())
     permissions.extend(self.ctx_permissions(permissions))
     with self.parent_instance.db.no_autoflush:
         try:
             if json is None:
                 mapper = sqlainspect(cls)
                 for prop in ('creator_id', 'user_id'):
                     if prop in mapper.c and prop not in kwargs:
                         kwargs[prop] = user_id
                         break
                 inst = cls(**dict(process_args(kwargs, cls)))
             else:
                 inst = cls.create_from_json(json,
                                             user_id,
                                             self,
                                             permissions=permissions)
                 kwargs.update(json)
         except Exception as e:
             print_exc()
             raise e
         assocs = [inst]
         self.decorate_instance(inst, assocs, user_id, self, kwargs)
         if json is None:
             inst = inst.handle_duplication(None, None, self, permissions,
                                            user_id, None, None, kwargs)
             assocs[0] = inst
     return assocs
Beispiel #3
0
 def create_object(self, typename=None, json=None, user_id=None, **kwargs):
     cls = self.get_collection_class(typename)
     permissions = get_permissions(
         user_id, self.get_discussion_id())
     permissions.extend(self.ctx_permissions(permissions))
     with self.parent_instance.db.no_autoflush:
         try:
             if json is None:
                 mapper = sqlainspect(cls)
                 for prop in ('creator_id', 'user_id'):
                     if prop in mapper.c and prop not in kwargs:
                         kwargs[prop] = user_id
                         break
                 inst = cls(**dict(process_args(kwargs, cls)))
             else:
                 inst = cls.create_from_json(
                     json, user_id, self, permissions=permissions)
                 kwargs.update(json)
         except Exception as e:
             print_exc()
             raise e
         assocs = [inst]
         self.decorate_instance(inst, assocs, user_id, self, kwargs)
         if json is None:
             inst = inst.handle_duplication(
                 None, None, self, permissions, user_id,
                 None, None, kwargs)
             assocs[0] = inst
     return assocs
Beispiel #4
0
def process_args(args, cls):
    mapper = sqlainspect(cls)
    for key, value in args.iteritems():
        column = mapper.c.get(key)
        if column is not None:
            if isinstance(column.type, DeclEnumType):
                yield (key, column.type.enum.from_string(value))
            elif column.type.python_type == int:
                yield (key, int(value))
            elif column.type.python_type == float:
                yield (key, float(value))
            elif column.type.python_type == bool:
                yield (key, asbool(value))
            else:
                yield (key, value)
            continue
        reln = mapper.relationships.get(key)
        if (reln is not None and reln.direction.name == 'MANYTOONE'
                and isinstance(value, (str, unicode))):
            assert(len(reln.local_columns) == 1)
            key = next(reln.local_columns.__iter__()).key
            yield (key, reln.mapper.class_.get_database_id(value))
            continue
        attribute = getattr(cls, key, None)
        if isinstance(attribute, property) and attribute.fset is not None:
            yield (key, value)
            continue
Beispiel #5
0
 def create_object(self, typename=None, json=None, user_id=None, **kwargs):
     cls = self.get_class(typename)
     with self._class.default_db.no_autoflush:
         if json is None:
             mapper = sqlainspect(cls)
             for prop in ('creator_id', 'user_id'):
                 if prop in mapper.c and prop not in kwargs:
                     kwargs[prop] = user_id
                     break
             try:
                 return [cls(**dict(process_args(kwargs, cls)))]
             except Exception as e:
                 print_exc()
                 raise e
         else:
             return [cls.create_from_json(json, user_id, self)]
Beispiel #6
0
 def create_object(self, typename=None, json=None, user_id=None, **kwargs):
     cls = self.get_class(typename)
     with self._class.default_db.no_autoflush:
         if json is None:
             mapper = sqlainspect(cls)
             for prop in ('creator_id', 'user_id'):
                 if prop in mapper.c and prop not in kwargs:
                     kwargs[prop] = user_id
                     break
             try:
                 return [cls(**dict(process_args(kwargs, cls)))]
             except Exception as e:
                 print_exc()
                 raise e
         else:
             return [cls.create_from_json(json, user_id, self)]
Beispiel #7
0
 def create_object(self, typename=None, json=None, user_id=None, **kwargs):
     cls = self.get_collection_class(typename)
     with cls.db.no_autoflush:
         try:
             if json is None:
                 mapper = sqlainspect(cls)
                 for prop in ('creator_id', 'user_id'):
                     if prop in mapper.c and prop not in kwargs:
                         kwargs[prop] = user_id
                         break
                 inst = cls(**dict(process_args(kwargs, cls)))
             else:
                 inst = cls.create_from_json(json, user_id, self)
         except Exception as e:
             print_exc()
             raise e
         assocs = [inst]
         self.decorate_instance(inst, assocs, user_id, self, kwargs)
     return assocs