Exemplo n.º 1
0
    def test_add_default_data(self):
        data = pkg_resources.resource_stream('aybu.core.data',
                                             'default_data.json').read()
        data = json.loads(data)
        add_default_data(self.session, data)

        self.assertIn(self.session.query(Setting).get('max_pages'),
                      self.session.query(Setting).all())
Exemplo n.º 2
0
    def command(self):

        if not self.args:
            raise BadCommand('You must give a configuration file.')

        file_name = self.args[0]
        if not file_name.startswith("/"):
            file_name = os.path.join(os.getcwd(), file_name)

        # Setup logging via the logging module's fileConfig function
        # with the specified 'config_file', if applicable.
        self.logging_file_config(file_name)

        config = appconfig('config:{}'.format(file_name))
        engine = engine_from_config(config, 'sqlalchemy.')
        Base.metadata.bind = engine
        Base.metadata.drop_all()
        Base.metadata.create_all()
        command.stamp(Config(file_name.split('#')[0]), 'head')
        Session = sessionmaker(bind=engine)
        try:
            session = Session()
            init_session_events(session)
            source_ = pkg_resources.resource_stream('aybu.core.data',
                                                    'default_data.json')
            data = json.loads(source_.read())

            add_default_data(session, data)
            user = User(username=config['default_user.username'],
                        password=config['default_user.password'])
            session.merge(user)
            group = Group(name=u'admin')
            group.users.append(user)
            session.merge(group)
            session.flush()

        except:
            session.rollback()

        else:
            session.commit()

        finally:
            session.close()
            source_.close()
Exemplo n.º 3
0
    def populate(self, session=None):
        SessionFactory = None
        if not session:
            SessionFactory = sessionmaker(bind=self.engine)
            session = SessionFactory()

        source_ = pkg_resources.resource_stream("aybu.core.data", "default_data.json")
        data = json.loads(source_.read())
        source_.close()
        add_default_data(session, data)
        user = User(username=self.config["default_user.username"], password=self.config["default_user.password"])
        session.merge(user)
        group = Group(name=u"admin")
        group.users.append(user)
        session.merge(group)
        session.commit()

        if SessionFactory:
            session.close()
            SessionFactory.close_all()