Пример #1
0
def package_setup():
    """Make sure the database schema is initialized and initial
    data is in place.
    """

    # Ensure that the log configuration starts in a known state.
    LogConfiguration.initialize(None, testing=True)

    engine, connection = DatabaseTest.get_database_connection()

    # First, recreate the schema.
    #
    # Base.metadata.drop_all(connection) doesn't work here, so we
    # approximate by dropping everything except the materialized
    # views.
    for table in reversed(Base.metadata.sorted_tables):
        if not table.name.startswith('mv_'):
            engine.execute(table.delete())

    Base.metadata.create_all(connection)

    # Initialize basic database data needed by the application.
    _db = Session(connection)
    SessionManager.initialize_data(_db)    
    _db.commit()
    connection.close()
    engine.dispose()
Пример #2
0
def package_setup():
    """Make sure the database schema is initialized and initial
    data is in place.
    """
    engine, connection = DatabaseTest.get_database_connection()

    # First, recreate the schema.
    #
    # Base.metadata.drop_all(connection) doesn't work here, so we
    # approximate by dropping everything except the materialized
    # views.
    for table in reversed(Base.metadata.sorted_tables):
        if not table.name.startswith('mv_'):
            engine.execute(table.delete())

    Base.metadata.create_all(connection)

    # Initialize basic database data needed by the application.
    _db = Session(connection)
    SessionManager.initialize_data(_db)

    # Create the patron used by the dummy authentication mechanism.
    # TODO: This can be probably be moved to circulation.
    get_one_or_create(
        _db, Patron, authorization_identifier="200",
        create_method_kwargs=dict(external_identifier="200200200")
    )
    _db.commit()
    connection.close()
    engine.dispose()
Пример #3
0
def package_setup():
    """Make sure the application starts in a pristine state.
    """
    # This will make sure we always connect to the test database.
    os.environ['TESTING'] = 'true'

    # This will make sure we always connect to the test database.
    os.environ['TESTING'] = 'true'

    # Ensure that the log configuration starts in a known state.
    LogConfiguration.initialize(None, testing=True)

    # Drop any existing schema. It will be recreated when
    # SessionManager.initialize() runs.
    #
    # Base.metadata.drop_all(connection) doesn't work here, so we
    # approximate by dropping every item individually.
    engine = SessionManager.engine()
    for table in reversed(Base.metadata.sorted_tables):
        if table.name.startswith('mv_'):
            statement = "drop materialized view %s" % table.name
        else:
            statement = table.delete()

        try:
            engine.execute(statement)
        except ProgrammingError, e:
            if 'does not exist' in e.message:
                # This is the first time running these tests
                # on this server, and the tables don't exist yet.
                pass
Пример #4
0
    def add_to_materialized_view(self, works, true_opds=False):
        """Make sure all the works in `works` show up in the materialized view.

        :param true_opds: Generate real OPDS entries for each each work,
        rather than faking it.
        """
        if not isinstance(works, list):
            works = [works]
        for work in works:
            if true_opds:
                work.calculate_opds_entries(verbose=False)
            else:
                work.presentation_ready = True
                work.simple_opds_entry = "<entry>an entry</entry>"
        self._db.commit()
        SessionManager.refresh_materialized_views(self._db)
Пример #5
0
def package_setup():
    """Make sure the application starts in a pristine state.
    """
    # This will make sure we always connect to the test database.
    os.environ['TESTING'] = 'true'

    # This will make sure we always connect to the test database.
    os.environ['TESTING'] = 'true'

    # Ensure that the log configuration starts in a known state.
    LogConfiguration.initialize(None, testing=True)

    # Drop any existing schema. It will be recreated when
    # SessionManager.initialize() runs.
    #
    # Base.metadata.drop_all(connection) doesn't work here, so we
    # approximate by dropping every item individually.
    engine = SessionManager.engine()
    for table in reversed(Base.metadata.sorted_tables):
        if table.name.startswith('mv_'):
            statement = "drop materialized view %s" % table.name
        else:
            statement = table.delete()

        try:
            engine.execute(statement)
        except ProgrammingError, e:
            if 'does not exist' in e.message:
                # This is the first time running these tests
                # on this server, and the tables don't exist yet.
                pass
Пример #6
0
    def add_to_materialized_view(self, works, true_opds=False):
        """Make sure all the works in `works` show up in the materialized view.

        :param true_opds: Generate real OPDS entries for each each work,
        rather than faking it.
        """
        if not isinstance(works, list):
            works = [works]
        for work in works:
            if true_opds:
                work.calculate_opds_entries(verbose=False)
            else:
                work.presentation_ready = True
                work.simple_opds_entry = "<entry>an entry</entry>"
        self._db.commit()
        SessionManager.refresh_materialized_views(self._db)
Пример #7
0
def login():
    error = None
    if request.method == 'POST':
        user = UserManager.login(
            request.form['login'],
            request.form['password'])

        if user:
            SessionManager.start_session(user)
            return redirect(url_for('dashboard'))
        else:
            error = "Incorrect username or password"
    else:
        user = SessionManager.get_user()
        if user:
            return redirect(url_for('dashboard'))

    return render_template('login.html', error=error)
Пример #8
0
def signup():
    error = None
    if request.method == 'POST':
        user = UserManager.register(
            request.form['login'],
            request.form['display_name'],
            request.form['password'])

        if user:
            SessionManager.start_session(user)
            return redirect(url_for('dashboard'))
        else:
            error = "Login is already used"
    else:
        user = SessionManager.get_user()
        if user:
            return redirect(url_for('dashboard'))

    return render_template('signup.html', error=error)
Пример #9
0
 def test_workers_are_created_with_sessions(self):
     session_factory = SessionManager.sessionmaker(session=self._db)
     bind = session_factory.kw['bind']
     pool = DatabasePool(2, session_factory)
     try:
         for worker in pool.workers:
             assert worker._db
             eq_(bind, worker._db.connection())
     finally:
         pool.join()
Пример #10
0
    def cdns(cls):
        """Get CDN configuration, loading it from the database
        if necessary.
        """
        if not cls.cdns_loaded_from_database():
            # The CDNs were never initialized from the database.
            # Create a new database connection and find that
            # information now.
            from model import SessionManager
            url = cls.database_url()
            _db = SessionManager.session(url)
            cls.load_cdns(_db)

        from model import ExternalIntegration
        return cls.integration(ExternalIntegration.CDN)
Пример #11
0
    def cdns(cls):
        """Get CDN configuration, loading it from the database
        if necessary.
        """
        if not cls.cdns_loaded_from_database():
            # The CDNs were never initialized from the database.
            # Create a new database connection and find that
            # information now.
            from model import SessionManager
            url = cls.database_url()
            _db = SessionManager.session(url)
            cls.load_cdns(_db)

        from model import ExternalIntegration
        return cls.integration(ExternalIntegration.CDN)
Пример #12
0
from app_helpers import (
    compressible,
    has_library_factory,
    uses_location_factory,
)

app = Flask(__name__)
babel = Babel(app)

# Create annotators for this app.
has_library = has_library_factory(app)
uses_location = uses_location_factory(app)

testing = 'TESTING' in os.environ
db_url = Configuration.database_url(testing)
SessionManager.initialize(db_url)
session_factory = SessionManager.sessionmaker(db_url)
_db = flask_scoped_session(session_factory, app)

log_level = LogConfiguration.initialize(_db, testing=testing)
debug = log_level == 'DEBUG'
app.config['DEBUG'] = debug
app.debug = debug
app._db = _db

if os.environ.get('AUTOINITIALIZE') == 'False':
    pass
    # It's the responsibility of the importing code to set app.library_registry
    # appropriately.
else:
    if getattr(app, 'library_registry', None) is None:
Пример #13
0
    def get_database_connection(cls):
        url = Configuration.database_url()
        engine, connection = SessionManager.initialize(url)

        return engine, connection
Пример #14
0
    def setup(self):
        super(TestLanesQuery, self).setup()

        # Look up the Fantasy genre and some of its subgenres.
        self.fantasy, ig = Genre.lookup(self._db, classifier.Fantasy)
        self.epic_fantasy, ig = Genre.lookup(self._db, classifier.Epic_Fantasy)
        self.urban_fantasy, ig = Genre.lookup(self._db,
                                              classifier.Urban_Fantasy)

        # Look up the History genre and some of its subgenres.
        self.history, ig = Genre.lookup(self._db, classifier.History)
        self.african_history, ig = Genre.lookup(self._db,
                                                classifier.African_History)

        self.adult_works = {}
        self.ya_works = {}
        self.childrens_works = {}

        for genre in (self.fantasy, self.epic_fantasy, self.urban_fantasy,
                      self.history, self.african_history):
            fiction = True
            if genre in (self.history, self.african_history):
                fiction = False

            # Create a number of books for each genre.
            adult_work = self._work(
                title="%s Adult" % genre.name,
                audience=Lane.AUDIENCE_ADULT,
                fiction=fiction,
                with_license_pool=True,
                genre=genre,
            )
            self.adult_works[genre] = adult_work
            adult_work.simple_opds_entry = '<entry>'

            # Childrens and YA books need to be attached to a data
            # source other than Gutenberg, or they'll get filtered
            # out.
            ya_edition, lp = self._edition(
                title="%s YA" % genre.name,
                data_source_name=DataSource.OVERDRIVE,
                with_license_pool=True)
            ya_work = self._work(
                audience=Lane.AUDIENCE_YOUNG_ADULT,
                fiction=fiction,
                with_license_pool=True,
                presentation_edition=ya_edition,
                genre=genre,
            )
            self.ya_works[genre] = ya_work
            ya_work.simple_opds_entry = '<entry>'

            childrens_edition, lp = self._edition(
                title="%s Childrens" % genre.name,
                data_source_name=DataSource.OVERDRIVE,
                with_license_pool=True)
            childrens_work = self._work(
                audience=Lane.AUDIENCE_CHILDREN,
                fiction=fiction,
                with_license_pool=True,
                presentation_edition=childrens_edition,
                genre=genre,
            )
            if genre == self.epic_fantasy:
                childrens_work.target_age = NumericRange(7, 9, '[]')
            else:
                childrens_work.target_age = NumericRange(8, 10, '[]')
            self.childrens_works[genre] = childrens_work
            childrens_work.simple_opds_entry = '<entry>'

        # Create generic 'Adults Only' fiction and nonfiction books
        # that are not in any genre.
        self.nonfiction = self._work(title="Generic Nonfiction",
                                     fiction=False,
                                     audience=Lane.AUDIENCE_ADULTS_ONLY,
                                     with_license_pool=True)
        self.nonfiction.simple_opds_entry = '<entry>'
        self.fiction = self._work(title="Generic Fiction",
                                  fiction=True,
                                  audience=Lane.AUDIENCE_ADULTS_ONLY,
                                  with_license_pool=True)
        self.fiction.simple_opds_entry = '<entry>'

        # Create a work of music.
        self.music = self._work(
            title="Music",
            fiction=False,
            audience=Lane.AUDIENCE_ADULT,
            with_license_pool=True,
        )
        self.music.presentation_edition.medium = Edition.MUSIC_MEDIUM
        self.music.simple_opds_entry = '<entry>'

        # Create a Spanish book.
        self.spanish = self._work(title="Spanish book",
                                  fiction=True,
                                  audience=Lane.AUDIENCE_ADULT,
                                  with_license_pool=True,
                                  language='spa')
        self.spanish.simple_opds_entry = '<entry>'

        # Refresh the materialized views so that all these books are present
        # in them.
        SessionManager.refresh_materialized_views(self._db)
Пример #15
0
    def test_custom_list_can_set_featured_works(self):
        my_list = self._customlist(num_entries=4)[0]

        featured_entries = my_list.entries[1:3]
        featured_works = list()
        for entry in featured_entries:
            featured_works.append(entry.edition.work)
            entry.featured = True

        other_works = [
            e.edition.work for e in my_list.entries if not e.featured
        ]
        for work in other_works:
            # Make the other works feature-quality so they are in the running.
            work.quality = 1.0

        self._db.commit()
        SessionManager.refresh_materialized_views(self._db)

        lane = Lane(self._db,
                    u'My Lane',
                    list_identifier=my_list.foreign_identifier)

        result = lane.list_featured_works_query.all()
        eq_(sorted(featured_works), sorted(result))

        def _assert_featured_works(size,
                                   expected_works=None,
                                   expected_length=None,
                                   sampled_works=None):
            featured_works = None
            featured_materialized_works = None
            with temp_config() as config:
                config[Configuration.POLICIES] = {
                    Configuration.FEATURED_LANE_SIZE: size
                }
                featured_works = lane.featured_works(
                    use_materialized_works=False)
                featured_materialized_works = lane.featured_works()

            expected_length = expected_length
            if expected_length == None:
                expected_length = size
            eq_(expected_length, len(featured_works))
            eq_(expected_length, len(featured_materialized_works))

            expected_works = expected_works or []
            for work in expected_works:
                assert work in featured_works
                # There's also a single MaterializedWork that matches the work.
                [materialized_work] = filter(lambda mw: mw.works_id == work.id,
                                             featured_materialized_works)

                # Remove the confirmed works for the next test.
                featured_works.remove(work)
                featured_materialized_works.remove(materialized_work)

            sampled_works = sampled_works or []
            for work in featured_works:
                assert work in sampled_works
            for work in featured_materialized_works:
                [sampled_work
                 ] = filter(lambda sample: sample.id == work.works_id,
                            sampled_works)

        # If the number of featured works completely fills the lane,
        # we only get featured works back.
        _assert_featured_works(2, featured_works)

        # If the number of featured works doesn't fill the lane, a
        # random other work that does will be sampled from the lane's
        # works
        _assert_featured_works(3, featured_works, sampled_works=other_works)

        # If the number of featured works falls slightly below the featured
        # lane size, all the available books are returned, without the
        # CustomList features being duplicated.
        _assert_featured_works(5,
                               featured_works,
                               expected_length=4,
                               sampled_works=other_works)

        # If the number of featured works falls far (>5) below the featured
        # lane size, nothing is returned.
        _assert_featured_works(10, expected_length=0)
Пример #16
0
def about():
    user = SessionManager.get_user()
    return render_template('about.html', user=user)
Пример #17
0
def logout():
    SessionManager.stop_session()
    return redirect(url_for('about'))
Пример #18
0
    def get_database_connection(cls):
        url = Configuration.database_url()
        engine, connection = SessionManager.initialize(url)

        return engine, connection
Пример #19
0
def page_not_found(error):
    user = SessionManager.get_user()
    return render_template('page_not_found.html', user=user), 404
Пример #20
0
    """Navigate the directory structure on the server to find
    game directories.
    
    When you hit the end of a pattern, call the function,
    which defaults to download_game"""
    global current_path
    if len(patterns) > 0:
       for linkname, href in get_links(grab_page(start_url), patterns[0]):
          newdir = href.split("/")[-2]
          if newdir not in os.listdir(current_path):
             os.mkdir(join(current_path, newdir))
          current_path = join(current_path, newdir)
          navigate_dirs(start_url + href, patterns[1:], fun)
          current_path = abspath(join(current_path, pardir))
    else:
       fun(start_url)

def download_with_patterns(patterns=default_patterns, local_dir='downloads'):

    global current_path
    if not isdir(local_dir):
        os.mkdir(local_dir)
    current_path = join(current_path, local_dir)
    navigate_dirs(server_string + start_dir, patterns, download_game) 
    current_path = abspath(join(current_path, pardir))

if __name__ == "__main__":
    from settings import postgres_db, postgres_user, postgres_password
    SessionManager.create(postgres_db, postgres_user, postgres_password)
    download_with_patterns(local_dir='download')
Пример #21
0
    
    When you hit the end of a pattern, call the function,
    which defaults to download_game"""
    global current_path
    if len(patterns) > 0:
        for linkname, href in get_links(grab_page(start_url), patterns[0]):
            newdir = href.split("/")[-2]
            if newdir not in os.listdir(current_path):
                os.mkdir(join(current_path, newdir))
            current_path = join(current_path, newdir)
            navigate_dirs(start_url + href, patterns[1:], fun)
            current_path = abspath(join(current_path, pardir))
    else:
        fun(start_url)


def download_with_patterns(patterns=default_patterns, local_dir='downloads'):

    global current_path
    if not isdir(local_dir):
        os.mkdir(local_dir)
    current_path = join(current_path, local_dir)
    navigate_dirs(server_string + start_dir, patterns, download_game)
    current_path = abspath(join(current_path, pardir))


if __name__ == "__main__":
    from settings import postgres_db, postgres_user, postgres_password
    SessionManager.create(postgres_db, postgres_user, postgres_password)
    download_with_patterns(local_dir='download')
Пример #22
0
 def wrapper(*args, **kwargs):
     user = SessionManager.get_user()
     if not user:
         return redirect(url_for('login'))
     return func(user, *args, **kwargs)