Esempio n. 1
0
 def __init__(self, name, model, sessionmaker, category):
     self.__name__ = self.name = name
     self.model = model
     self.schema = create_schema(model)
     self.category = category
     self.sessionmaker = sessionmaker
     # TODO: use inspect primary key
     self.repository = SQLARepository(model,
                                      "id", sessionmaker)
def includeme(config):
    config.include('rebecca.repository')
    init()
    db_session = config.registry.settings.get('rebecca.users.db_session')
    db_session = config.maybe_dotted(db_session)
    user_model = config.registry.settings.get('rebecca.users.user_model', User)
    user_model = config.maybe_dotted(user_model)
    user_repository = SQLARepository(user_model, user_model.username,
                                     db_session)

    config.add_repository(user_repository, name='user')
    config.registry.registerUtility(RepositoryAuthenticator(user_repository),
                                    IAuthenticator)
Esempio n. 3
0
class SQLAModelAdmin(object):

    def __init__(self, name, model, sessionmaker, category):
        self.__name__ = self.name = name
        self.model = model
        self.schema = create_schema(model)
        self.category = category
        self.sessionmaker = sessionmaker
        # TODO: use inspect primary key
        self.repository = SQLARepository(model,
                                         "id", sessionmaker)

    @property
    def db_session(self):
        return self.sessionmaker

    def items(self):
        return iter(self.repository)

    def __getitem__(self, key):
        logger.debug('traversal {key}'.format(key=key))
        try:
            item = self.repository[key]
            resource = SQLAModelDetail(self, key,
                                       self.model, self.schema,
                                       item, self.db_session)
            logger.debug('{key} {resource}'.format(key=key,
                                                   resource=resource))
            return resource
        except KeyError as e:
            logger.debug('Key Error {e}'.format(e=e))
            raise

    def add(self, values):
        return self.repository.new_item(**values)

    def search_relation(self, relation_name, limit=None, offset=None):
        query = query_relation(self.sessionmaker, self.model, relation_name)
        if limit is not None:
            query = query.limit(limit)
        if offset is not None:
            query = query.offset(offset)
        return query.all()