Example #1
0
    def test_cached_values_are_properly_updated(self):
        setting_key = "key"
        setting_old_value = "old value"
        setting_new_value = "new value"

        # First, let's create a ConfigurationSetting instance and save it in the database.
        setting = ConfigurationSetting(key=setting_key, _value=setting_old_value)
        self._db.add(setting)
        self._db.commit()

        # Let's save ConfigurationSetting's ID to find it later.
        setting_id = setting.id

        # Now let's fetch the configuration setting from the database and add it to the cache.
        db_setting1 = (
            self._db.query(ConfigurationSetting)
            .filter(ConfigurationSetting.key == setting_key)
            .one()
        )
        ConfigurationSetting.cache_warm(self._db, lambda: [db_setting1])

        # After, let's fetch it again and change its value.
        db_setting2 = (
            self._db.query(ConfigurationSetting)
            .filter(ConfigurationSetting.key == setting_key)
            .one()
        )
        db_setting2.value = setting_new_value

        # Now let's make sure that the cached value has also been updated.
        assert (
            ConfigurationSetting.by_id(self._db, setting_id)._value == setting_new_value
        )
    def process_get(self):
        collections_db = self._db.query(Collection).order_by(
            Collection.name).all()
        ConfigurationSetting.cache_warm(self._db)
        Collection.cache_warm(self._db, lambda: collections_db)
        protocols = self._get_collection_protocols()
        user = flask.request.admin
        collections = []
        protocolClass = None
        for collection_object in collections_db:
            if not user or not user.can_see_collection(collection_object):
                continue

            collection_dict = self.collection_to_dict(collection_object)

            if collection_object.protocol in [
                    p.get("name") for p in protocols
            ]:
                [protocol] = [
                    p for p in protocols
                    if p.get("name") == collection_object.protocol
                ]
                libraries = self.load_libraries(collection_object, user,
                                                protocol)
                collection_dict["libraries"] = libraries
                settings = self.load_settings(
                    protocol.get("settings"),
                    collection_object,
                    collection_dict.get("settings"),
                )
                collection_dict["settings"] = settings
                protocolClass = self.find_protocol_class(collection_object)

            collection_dict[
                "self_test_results"] = self._get_prior_test_results(
                    collection_object, protocolClass)
            collection_dict[
                "marked_for_deletion"] = collection_object.marked_for_deletion

            collections.append(collection_dict)

        return dict(
            collections=collections,
            protocols=protocols,
        )
    def process_get(self):
        response = []
        libraries = self._db.query(Library).order_by(Library.name).all()
        ConfigurationSetting.cache_warm(self._db)

        for library in libraries:
            # Only include libraries this admin has librarian access to.
            if not flask.request.admin or not flask.request.admin.is_librarian(
                    library):
                continue

            settings = dict()
            for setting in Configuration.LIBRARY_SETTINGS:
                if setting.get("type") == "announcements":
                    value = ConfigurationSetting.for_library(
                        setting.get("key"), library).json_value
                    if value:
                        value = AnnouncementListValidator(
                        ).validate_announcements(value)
                if setting.get("type") == "list":
                    value = ConfigurationSetting.for_library(
                        setting.get("key"), library).json_value
                    if value and setting.get("format") == "geographic":
                        value = self.get_extra_geographic_information(value)
                else:
                    value = self.current_value(setting, library)

                if value:
                    settings[setting.get("key")] = value

            response += [
                dict(
                    uuid=library.uuid,
                    name=library.name,
                    short_name=library.short_name,
                    settings=settings,
                )
            ]
        return dict(libraries=response,
                    settings=Configuration.LIBRARY_SETTINGS)