def test_sitewide_settings_get(self): with self.request_context_with_admin("/"): response = self.manager.admin_sitewide_configuration_settings_controller.process_get() settings = response.get("settings") all_settings = response.get("all_settings") eq_([], settings) keys = [s.get("key") for s in all_settings] assert AcquisitionFeed.GROUPED_MAX_AGE_POLICY in keys assert AcquisitionFeed.NONGROUPED_MAX_AGE_POLICY in keys assert Configuration.SECRET_KEY in keys ConfigurationSetting.sitewide(self._db, AcquisitionFeed.GROUPED_MAX_AGE_POLICY).value = 0 ConfigurationSetting.sitewide(self._db, Configuration.SECRET_KEY).value = "secret" self._db.flush() with self.request_context_with_admin("/"): response = self.manager.admin_sitewide_configuration_settings_controller.process_get() settings = response.get("settings") all_settings = response.get("all_settings") eq_(2, len(settings)) settings_by_key = { s.get("key") : s.get("value") for s in settings } eq_("0", settings_by_key.get(AcquisitionFeed.GROUPED_MAX_AGE_POLICY)) eq_("secret", settings_by_key.get(Configuration.SECRET_KEY)) keys = [s.get("key") for s in all_settings] assert AcquisitionFeed.GROUPED_MAX_AGE_POLICY in keys assert AcquisitionFeed.NONGROUPED_MAX_AGE_POLICY in keys assert Configuration.SECRET_KEY in keys self.admin.remove_role(AdminRole.SYSTEM_ADMIN) self._db.flush() assert_raises(AdminNotAuthorized, self.manager.admin_sitewide_configuration_settings_controller.process_get)
def test_sitewide_settings_get(self): with self.request_context_with_admin("/"): response = self.manager.admin_sitewide_configuration_settings_controller.process_get() settings = response.get("settings") all_settings = response.get("all_settings") eq_([], settings) keys = [s.get("key") for s in all_settings] assert Configuration.LOG_LEVEL in keys assert Configuration.DATABASE_LOG_LEVEL in keys assert Configuration.SECRET_KEY in keys ConfigurationSetting.sitewide(self._db, Configuration.DATABASE_LOG_LEVEL).value = 'INFO' ConfigurationSetting.sitewide(self._db, Configuration.SECRET_KEY).value = "secret" self._db.flush() with self.request_context_with_admin("/"): response = self.manager.admin_sitewide_configuration_settings_controller.process_get() settings = response.get("settings") all_settings = response.get("all_settings") eq_(2, len(settings)) settings_by_key = { s.get("key") : s.get("value") for s in settings } eq_("INFO", settings_by_key.get(Configuration.DATABASE_LOG_LEVEL)) eq_("secret", settings_by_key.get(Configuration.SECRET_KEY)) keys = [s.get("key") for s in all_settings] assert Configuration.LOG_LEVEL in keys assert Configuration.DATABASE_LOG_LEVEL in keys assert Configuration.SECRET_KEY in keys self.admin.remove_role(AdminRole.SYSTEM_ADMIN) self._db.flush() assert_raises(AdminNotAuthorized, self.manager.admin_sitewide_configuration_settings_controller.process_get)
def process_post(self): self.require_system_admin() setting = ConfigurationSetting.sitewide(self._db, flask.request.form.get("key")) error = self.validate_form_fields(setting, list(flask.request.form.keys())) if error: return error setting = ConfigurationSetting.sitewide(self._db, flask.request.form.get("key")) setting.value = flask.request.form.get("value") return Response(str(setting.key), 200)
def test_configuration_relevant_lifecycle_event_updates_configuration( self): """When you create or modify a relevant item such as a ConfigurationSetting, site_configuration_has_changed is called. """ ConfigurationSetting.sitewide(self._db, "setting").value = "value" self.mock.assert_was_called() ConfigurationSetting.sitewide(self._db, "setting").value = "value2" self._db.commit() self.mock.assert_was_called()
def process_post(self): self.require_system_admin() setting = ConfigurationSetting.sitewide(self._db, flask.request.form.get("key")) error = self.validate_form_fields(setting, flask.request.form.keys()) if error: return error setting = ConfigurationSetting.sitewide(self._db, flask.request.form.get("key")) setting.value = flask.request.form.get("value") return Response(unicode(setting.key), 200)
def run(url=None): base_url = ConfigurationSetting.sitewide(_db, Configuration.BASE_URL_KEY) base_url = url or base_url.value or u'http://localhost:6500/' scheme, netloc, path, parameters, query, fragment = urlparse.urlparse( base_url) if ':' in netloc: host, port = netloc.split(':') port = int(port) else: host = netloc port = 80 # Required for subdomain support. app.config['SERVER_NAME'] = netloc debug = True # Workaround for a "Resource temporarily unavailable" error when # running in debug mode with the global socket timeout set by isbnlib if debug: import socket socket.setdefaulttimeout(None) logging.info("Starting app on %s:%s", host, port) app.run(debug=debug, host=host, port=port, threaded=True)
def do_run(self, cmd_args=None, in_unit_test=False): parser = self.arg_parser(self._db) parsed = self.parse_command_line(self._db, cmd_args) url = parsed.registry_url registry = RemoteRegistry.for_protocol_goal_and_url( self._db, self.PROTOCOL, self.GOAL, url) stage = parsed.stage # Set up an application context so we have access to url_for. from api.app import app app.manager = CirculationManager(self._db, testing=in_unit_test) base_url = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY).value ctx = app.test_request_context(base_url=base_url) ctx.push() for library in parsed.libraries: registration = Registration(registry, library) library_stage = stage or registration.stage_field.value self.process_library(registration, library_stage, app.manager.url_for) ctx.pop() # For testing purposes, return the application object that was # created. return app
def test_key_pair(self): # Test the ability to create, replace, or look up a # public/private key pair in a ConfigurationSetting. setting = ConfigurationSetting.sitewide( self._db, Configuration.KEY_PAIR ) setting.value = "nonsense" # If you pass in a ConfigurationSetting that is missing its # value, or whose value is not a public key pair, a new key # pair is created. public_key, private_key = Configuration.key_pair(setting) assert 'BEGIN PUBLIC KEY' in public_key assert 'BEGIN RSA PRIVATE KEY' in private_key eq_([public_key, private_key], setting.json_value) setting.value = None public_key, private_key = Configuration.key_pair(setting) assert 'BEGIN PUBLIC KEY' in public_key assert 'BEGIN RSA PRIVATE KEY' in private_key eq_([public_key, private_key], setting.json_value) # If the setting has a good value already, the key pair is # returned as is. new_public, new_private = Configuration.key_pair(setting) eq_(new_public, public_key) eq_(new_private, private_key)
def test_disable(self): # This reaper can be disabled with a configuration setting enabled = ConfigurationSetting.sitewide( self._db, Configuration.MEASUREMENT_REAPER) enabled.value = False measurement1, created = get_one_or_create( self._db, Measurement, quantity_measured="answer", value=12, is_most_recent=True, ) measurement2, created = get_one_or_create( self._db, Measurement, quantity_measured="answer", value=42, is_most_recent=False, ) reaper = MeasurementReaper(self._db) reaper.run() assert [measurement1, measurement2] == self._db.query(Measurement).all() enabled.value = True reaper.run() assert [measurement1] == self._db.query(Measurement).all()
def __init__(self, _db=None, cmd_args=None, testing=False, *args, **kwargs): super(CacheRepresentationPerLane, self).__init__(_db, *args, **kwargs) self.parse_args(cmd_args) from api.app import app app.manager = CirculationManager(self._db, testing=testing) self.app = app self.base_url = ConfigurationSetting.sitewide(self._db, Configuration.BASE_URL_KEY).value
def do_run(self, cmd_args=None, in_unit_test=False): parser = self.arg_parser(self._db) parsed = self.parse_command_line(self._db, cmd_args) url = parsed.registry_url registry = RemoteRegistry.for_protocol_goal_and_url( self._db, self.PROTOCOL, self.GOAL, url ) stage = parsed.stage # Set up an application context so we have access to url_for. from api.app import app app.manager = CirculationManager(self._db, testing=in_unit_test) base_url = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY ).value ctx = app.test_request_context(base_url=base_url) ctx.push() for library in parsed.libraries: registration = Registration(registry, library) library_stage = stage or registration.stage_field.value self.process_library( registration, library_stage, app.manager.url_for ) ctx.pop() # For testing purposes, return the application object that was # created. return app
def activate_debug_mode(self): """Set a site-wide setting that controls whether detailed exception information is provided. """ ConfigurationSetting.sitewide( self._db, Configuration.DATABASE_LOG_LEVEL).value = LogConfiguration.DEBUG
def test_do_run(self): class Mock(LibraryRegistrationScript): processed = [] def process_library(self, *args): self.processed.append(args) script = Mock(self._db) base_url_setting = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY) base_url_setting.value = "http://test-circulation-manager/" library = self._default_library library2 = self._library() cmd_args = [ library.short_name, "--stage=testing", "--registry-url=http://registry/", ] app = script.do_run(cmd_args=cmd_args, in_unit_test=True) # One library was processed. (registration, stage, url_for) = script.processed.pop() assert [] == script.processed assert library == registration.library assert Registration.TESTING_STAGE == stage # A new ExternalIntegration was created for the newly defined # registry at http://registry/. assert "http://registry/" == registration.integration.url # An application environment was created and the url_for # implementation for that environment was passed into # process_library. assert url_for == app.manager.url_for # Let's say the other library was earlier registered in production. registration_2 = Registration(registration.registry, library2) registration_2.stage_field.value = Registration.PRODUCTION_STAGE # Now run the script again without specifying a particular # library or the --stage argument. app = script.do_run(cmd_args=[], in_unit_test=True) # Every library was processed. assert set([library, library2]) == set([x[0].library for x in script.processed]) for i in script.processed: # Since no stage was provided, each library was registered # using the stage already associated with it. assert i[0].stage_field.value == i[1] # Every library was registered with the default # library registry. assert RemoteRegistry.DEFAULT_LIBRARY_REGISTRY_URL == i[ 0].integration.url
def test_do_run(self): class Mock(LibraryRegistrationScript): processed = [] def process_library(self, *args): self.processed.append(args) script = Mock(self._db) base_url_setting = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY ) base_url_setting.value = u'http://test-circulation-manager/' library = self._default_library library2 = self._library() cmd_args = [library.short_name, "--stage=testing", "--registry-url=http://registry/"] app = script.do_run(cmd_args=cmd_args, in_unit_test=True) # One library was processed. (registration, stage, url_for) = script.processed.pop() eq_([], script.processed) eq_(library, registration.library) eq_(Registration.TESTING_STAGE, stage) # A new ExternalIntegration was created for the newly defined # registry at http://registry/. eq_("http://registry/", registration.integration.url) # An application environment was created and the url_for # implementation for that environment was passed into # process_library. eq_(url_for, app.manager.url_for) # Let's say the other library was earlier registered in production. registration_2 = Registration(registration.registry, library2) registration_2.stage_field.value = Registration.PRODUCTION_STAGE # Now run the script again without specifying a particular # library or the --stage argument. app = script.do_run(cmd_args=[], in_unit_test=True) # Every library was processed. eq_(set([library, library2]), set([x[0].library for x in script.processed])) for i in script.processed: # Since no stage was provided, each library was registered # using the stage already associated with it. eq_(i[0].stage_field.value, i[1]) # Every library was registered with the default # library registry. eq_( RemoteRegistry.DEFAULT_LIBRARY_REGISTRY_URL, x[0].integration.url )
def opds_catalog(self): url = ConfigurationSetting.sitewide(self._db, Configuration.BASE_URL_KEY).value_or_default(request.url) catalog = dict( id=url, title='Library Simplified Metadata Wrangler', ) catalog['links'] = [ { "rel": "register", "href": "/register", "type": "application/opds+json;profile=https://librarysimplified.org/rel/profile/metadata-service", "title": "Register your OPDS server with this metadata service" }, { "rel": "http://librarysimplified.org/rel/metadata/lookup", "href": "/lookup{?data_source,urn*}", "type": "application/atom+xml;profile=opds-catalog", "title": "Look up metadata about one or more specific items", "templated": "true" }, { "rel": "http://opds-spec.org/sort/new", "href": "/{collection_metadata_identifier}/updates{?data_source}", "type": "application/atom+xml;profile=opds-catalog", "title": "Recent changes to one of your tracked collections.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/collection-add", "href": "/{collection_metadata_identifier}/add{?data_source,urn*}", "title": "Add items to one of your tracked collections.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/collection-remove", "href": "/{collection_metadata_identifier}/remove{?data_source,urn*}", "title": "Remove items from one of your tracked collections.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/collection-metadata-needed", "href": "/{collection_metadata_identifier}/metadata_needed", "title": "Get items in your collection for which the metadata wrangler needs more information to process.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/resolve-name", "href": "/canonical-author-name{?urn,display_name}", "type": "text/plain", "title": "Look up an author's canonical sort name", "templated": "true" } ] return make_response( json.dumps(catalog), HTTP_OK, {'Content-Type' : OPDS_2_MEDIA_TYPE } )
def opds_catalog(self): url = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY).value_or_default(request.url) catalog = dict( id=url, title='Library Simplified Metadata Wrangler', ) catalog['links'] = [{ "rel": "register", "href": "/register", "type": "application/opds+json;profile=https://librarysimplified.org/rel/profile/metadata-service", "title": "Register your OPDS server with this metadata service" }, { "rel": "http://librarysimplified.org/rel/metadata/lookup", "href": "/lookup{?data_source,urn*}", "type": "application/atom+xml;profile=opds-catalog", "title": "Look up metadata about one or more specific items", "templated": "true" }, { "rel": "http://opds-spec.org/sort/new", "href": "/{collection_metadata_identifier}/updates{?data_source}", "type": "application/atom+xml;profile=opds-catalog", "title": "Recent changes to one of your tracked collections.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/collection-add", "href": "/{collection_metadata_identifier}/add{?data_source,urn*}", "title": "Add items to one of your tracked collections.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/collection-remove", "href": "/{collection_metadata_identifier}/remove{?data_source,urn*}", "title": "Remove items from one of your tracked collections.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/collection-metadata-needed", "href": "/{collection_metadata_identifier}/metadata_needed", "title": "Get items in your collection for which the metadata wrangler needs more information to process.", "templated": "true" }, { "rel": "http://librarysimplified.org/rel/metadata/resolve-name", "href": "/canonical-author-name{?urn,display_name}", "type": "text/plain", "title": "Look up an author's canonical sort name", "templated": "true" }] return make_response(json.dumps(catalog), HTTP_OK, {'Content-Type': OPDS_2_MEDIA_TYPE})
def admin_css(): directory = os.path.join(os.path.abspath(os.path.dirname(__file__)), "node_modules", "simplified-circulation-web", "dist") cache_timeout = ConfigurationSetting.sitewide( app._db, Configuration.STATIC_FILE_CACHE_TIME).int_value return flask.send_from_directory(directory, "circulation-web.css", cache_timeout=cache_timeout)
def __init__(self, _db=None, testing=False): _db = _db or self._db super(LaneSweeperScript, self).__init__(_db) os.environ['AUTOINITIALIZE'] = "False" from api.app import app del os.environ['AUTOINITIALIZE'] app.manager = CirculationManager(_db, testing=testing) self.app = app self.base_url = ConfigurationSetting.sitewide( _db, Configuration.BASE_URL_KEY).value
def setup(self): super(TestLaneScript, self).setup() base_url_setting = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY) base_url_setting.value = u'http://test-circulation-manager/' for k, v in [(Configuration.LARGE_COLLECTION_LANGUAGES, []), (Configuration.SMALL_COLLECTION_LANGUAGES, []), (Configuration.TINY_COLLECTION_LANGUAGES, ['eng', 'fre'])]: ConfigurationSetting.for_library( k, self._default_library).value = json.dumps(v)
def setup(self): super(TestLaneScript, self).setup() base_url_setting = ConfigurationSetting.sitewide( self._db, Configuration.BASE_URL_KEY) base_url_setting.value = u'http://test-circulation-manager/' for k, v in [ (Configuration.LARGE_COLLECTION_LANGUAGES, []), (Configuration.SMALL_COLLECTION_LANGUAGES, []), (Configuration.TINY_COLLECTION_LANGUAGES, ['eng', 'fre']) ]: ConfigurationSetting.for_library( k, self._default_library).value = json.dumps(v)
def test_sitewide_settings_get(self): with self.request_context_with_admin("/"): response = self.manager.admin_sitewide_configuration_settings_controller.process_get( ) settings = response.get("settings") all_settings = response.get("all_settings") eq_([], settings) keys = [s.get("key") for s in all_settings] assert AcquisitionFeed.GROUPED_MAX_AGE_POLICY in keys assert AcquisitionFeed.NONGROUPED_MAX_AGE_POLICY in keys assert Configuration.SECRET_KEY in keys ConfigurationSetting.sitewide( self._db, AcquisitionFeed.GROUPED_MAX_AGE_POLICY).value = 0 ConfigurationSetting.sitewide( self._db, Configuration.SECRET_KEY).value = "secret" self._db.flush() with self.request_context_with_admin("/"): response = self.manager.admin_sitewide_configuration_settings_controller.process_get( ) settings = response.get("settings") all_settings = response.get("all_settings") eq_(2, len(settings)) settings_by_key = {s.get("key"): s.get("value") for s in settings} eq_("0", settings_by_key.get(AcquisitionFeed.GROUPED_MAX_AGE_POLICY)) eq_("secret", settings_by_key.get(Configuration.SECRET_KEY)) keys = [s.get("key") for s in all_settings] assert AcquisitionFeed.GROUPED_MAX_AGE_POLICY in keys assert AcquisitionFeed.NONGROUPED_MAX_AGE_POLICY in keys assert Configuration.SECRET_KEY in keys self.admin.remove_role(AdminRole.SYSTEM_ADMIN) self._db.flush() assert_raises( AdminNotAuthorized, self.manager. admin_sitewide_configuration_settings_controller.process_get)
def test_sitewide_settings_post_create(self): with self.request_context_with_admin("/", method="POST"): flask.request.form = MultiDict([ ("key", AcquisitionFeed.GROUPED_MAX_AGE_POLICY), ("value", "10"), ]) response = self.manager.admin_sitewide_configuration_settings_controller.process_post() eq_(response.status_code, 200) # The setting was created. setting = ConfigurationSetting.sitewide(self._db, AcquisitionFeed.GROUPED_MAX_AGE_POLICY) eq_(setting.key, response.response[0]) eq_("10", setting.value)
def test_sitewide_settings_post_create(self): with self.request_context_with_admin("/", method="POST"): flask.request.form = MultiDict([ ("key", Configuration.DATABASE_LOG_LEVEL), ("value", "10"), ]) response = self.manager.admin_sitewide_configuration_settings_controller.process_post() eq_(response.status_code, 200) # The setting was created. setting = ConfigurationSetting.sitewide(self._db, Configuration.DATABASE_LOG_LEVEL) eq_(setting.key, response.response[0]) eq_("10", setting.value)
def process_get(self): self.require_system_admin() sitewide_settings = Configuration.SITEWIDE_SETTINGS settings = [] for s in sitewide_settings: setting = ConfigurationSetting.sitewide(self._db, s.get("key")) if setting.value: settings += [{ "key": setting.key, "value": setting.value }] return dict( settings=settings, all_settings=sitewide_settings, )
def process_get(self): self.require_system_admin() sitewide_settings = Configuration.SITEWIDE_SETTINGS settings = [] for s in sitewide_settings: setting = ConfigurationSetting.sitewide(self._db, s.get("key")) if setting.value: settings += [{"key": setting.key, "value": setting.value}] return dict( settings=settings, all_settings=sitewide_settings, )
def test_cached_value_deleted(self): # Get setting setting = ConfigurationSetting.sitewide(self._db, "test") setting.value = "testing" # Delete setting self._db.delete(setting) # we should no longer be able to get setting from cache cached = ConfigurationSetting.by_id(self._db, setting.id) cache = ConfigurationSetting._cache_from_session(self._db) assert cached is None assert len(cache.id) == 0 assert len(cache.key) == 0
def test_sitewide_setting_delete(self): setting = ConfigurationSetting.sitewide(self._db, AcquisitionFeed.GROUPED_MAX_AGE_POLICY) setting.value = "10" with self.request_context_with_admin("/", method="DELETE"): self.admin.remove_role(AdminRole.SYSTEM_ADMIN) assert_raises(AdminNotAuthorized, self.manager.admin_sitewide_configuration_settings_controller.process_delete, setting.key) self.admin.add_role(AdminRole.SYSTEM_ADMIN) response = self.manager.admin_sitewide_configuration_settings_controller.process_delete(setting.key) eq_(response.status_code, 200) eq_(None, setting.value)
def test_sitewide_setting_delete(self): setting = ConfigurationSetting.sitewide(self._db, Configuration.DATABASE_LOG_LEVEL) setting.value = "WARN" with self.request_context_with_admin("/", method="DELETE"): self.admin.remove_role(AdminRole.SYSTEM_ADMIN) assert_raises(AdminNotAuthorized, self.manager.admin_sitewide_configuration_settings_controller.process_delete, setting.key) self.admin.add_role(AdminRole.SYSTEM_ADMIN) response = self.manager.admin_sitewide_configuration_settings_controller.process_delete(setting.key) eq_(response.status_code, 200) eq_(None, setting.value)
def test_sitewide_settings_post_create(self): with self.request_context_with_admin("/", method="POST"): flask.request.form = MultiDict([ ("key", Configuration.DATABASE_LOG_LEVEL), ("value", "10"), ]) response = ( self.manager.admin_sitewide_configuration_settings_controller. process_post()) assert response.status_code == 200 # The setting was created. setting = ConfigurationSetting.sitewide( self._db, Configuration.DATABASE_LOG_LEVEL) assert setting.key == response.get_data(as_text=True) assert "10" == setting.value
def test_sitewide_settings_post_edit(self): setting = ConfigurationSetting.sitewide( self._db, AcquisitionFeed.GROUPED_MAX_AGE_POLICY) setting.value = "10" with self.request_context_with_admin("/", method="POST"): flask.request.form = MultiDict([ ("key", AcquisitionFeed.GROUPED_MAX_AGE_POLICY), ("value", "20"), ]) response = self.manager.admin_sitewide_configuration_settings_controller.process_post( ) eq_(response.status_code, 200) # The setting was changed. eq_(setting.key, response.response[0]) eq_("20", setting.value)
def test_sitewide_setting_delete(self): setting = ConfigurationSetting.sitewide( self._db, AcquisitionFeed.GROUPED_MAX_AGE_POLICY) setting.value = "10" with self.request_context_with_admin("/", method="DELETE"): self.admin.remove_role(AdminRole.SYSTEM_ADMIN) assert_raises( AdminNotAuthorized, self.manager.admin_sitewide_configuration_settings_controller. process_delete, setting.key) self.admin.add_role(AdminRole.SYSTEM_ADMIN) response = self.manager.admin_sitewide_configuration_settings_controller.process_delete( setting.key) eq_(response.status_code, 200) eq_(None, setting.value)
def edition_permalink(self, edition): """Gets a unique URL for the target Work""" base_url = ConfigurationSetting.sitewide(self._db, Configuration.BASE_URL_KEY).value scheme, host = urlparse(base_url)[0:2] base_url = '://'.join([scheme, host]) urn = edition.primary_identifier.urn initialization_value = os.environ.get('AUTOINITIALIZE') try: os.environ['AUTOINITIALIZE'] = 'False' from app import app with app.test_request_context(base_url=base_url): permalink = url_for('lookup', urn=urn, _external=True) finally: os.unsetenv('AUTOINITIALIZE') if initialization_value: os.environ['AUTOINITIALIZE'] = initialization_value return permalink
wrangler = EI( name=EI.METADATA_WRANGLER, protocol=EI.METADATA_WRANGLER, goal=EI.METADATA_GOAL ) _db.add(wrangler) wrangler.url = metadata_wrangler_conf.get('url') wrangler.username = metadata_wrangler_conf.get('client_id') wrangler.password = metadata_wrangler_conf.get('client_secret') log_import(wrangler) # Get the base url. content_server_conf = Configuration.integration('Content Server') if content_server_conf: url = content_server_conf.get('url') setting = ConfigurationSetting.sitewide(_db, Configuration.BASE_URL_KEY) setting.value = url log_import(setting) # Copy facet configuration to the library. facet_policy = Configuration.policy("facets", default={}) default_enabled = Configuration.DEFAULT_ENABLED_FACETS enabled = facet_policy.get("enabled", default_enabled) for k, v in enabled.items(): library.enabled_facets_setting(unicode(k)).value = unicode(json.dumps(v)) default_facets = Configuration.DEFAULT_FACET default = facet_policy.get("default", default_facets) for k, v in default.items(): library.default_facet_setting(unicode(k)).value = unicode(v)
shadowcat = EI( name=EI.NYPL_SHADOWCAT, protocol=EI.NYPL_SHADOWCAT, goal=EI.METADATA_GOAL ) _db.add(shadowcat) shadowcat.url = shadowcat_conf.get('url') log_import(shadowcat) content_cafe_conf = Configuration.integration('Content Cafe') if content_cafe_conf: content_cafe = EI( name=EI.CONTENT_CAFE, protocol=EI.CONTENT_CAFE, goal=EI.METADATA_GOAL ) _db.add(content_cafe) content_cafe.username = content_cafe_conf.get('username') content_cafe.password = content_cafe_conf.get('password') metadata_wrangler_conf = Configuration.integration('Metadata Wrangler') if metadata_wrangler_conf: url = metadata_wrangler_conf.get('url') ConfigurationSetting.sitewide(_db, Configuration.BASE_URL_KEY).value = url finally: _db.commit() _db.close()
) from api.config import Configuration _db = production_session() try: Configuration.load() integrations = [] # Get or create a secret key and make it a sitewide setting. secret_key = Configuration.get('secret_key') if not secret_key: secret_key = os.urandom(24).encode('hex') secret_setting = ConfigurationSetting.sitewide( _db, Configuration.SECRET_KEY ) secret_setting.value = secret_key libraries = _db.query(Library).all() # Copy default email address into each library. key = 'default_notification_email_address' value = Configuration.get(key) if value: for library in libraries: ConfigurationSetting.for_library(key, library).value = value # Copy maximum fines into each library. for key in ['max_outstanding_fines', 'minimum_featured_quality', 'featured_lane_size']:
log = logging.getLogger(name="Circulation manager configuration import") def log_import(integration_or_setting): log.info("CREATED: %r" % integration_or_setting) try: Configuration.load() _db = production_session() LIBRARIES = _db.query(Library).all() # Import Circulation Manager base url. circ_manager_conf = Configuration.integration('Circulation Manager') if circ_manager_conf: url = circ_manager_conf.get('url') if url: setting = ConfigurationSetting.sitewide(_db, Configuration.BASE_URL_KEY) setting.value = unicode(url) log_import(setting) # Import Metadata Wrangler configuration. metadata_wrangler_conf = Configuration.integration('Metadata Wrangler') if metadata_wrangler_conf: integration = EI(protocol=EI.METADATA_WRANGLER, goal=EI.METADATA_GOAL) _db.add(integration) integration.url = metadata_wrangler_conf.get('url') integration.username = metadata_wrangler_conf.get('client_id') integration.password = metadata_wrangler_conf.get('client_secret') log_import(integration)
def __init__(self, library): super(LibraryAnnotator, self).__init__() self.library = library _db = Session.object_session(library) self.base_url = ConfigurationSetting.sitewide( _db, Configuration.BASE_URL_KEY).value
Configuration.load() shadowcat_conf = Configuration.integration('Shadowcat') if shadowcat_conf and shadowcat_conf.get('url'): shadowcat = EI(name=EI.NYPL_SHADOWCAT, protocol=EI.NYPL_SHADOWCAT, goal=EI.METADATA_GOAL) _db.add(shadowcat) shadowcat.url = shadowcat_conf.get('url') log_import(shadowcat) content_cafe_conf = Configuration.integration('Content Cafe') if content_cafe_conf: content_cafe = EI(name=EI.CONTENT_CAFE, protocol=EI.CONTENT_CAFE, goal=EI.METADATA_GOAL) _db.add(content_cafe) content_cafe.username = content_cafe_conf.get('username') content_cafe.password = content_cafe_conf.get('password') metadata_wrangler_conf = Configuration.integration('Metadata Wrangler') if metadata_wrangler_conf: url = metadata_wrangler_conf.get('url') ConfigurationSetting.sitewide(_db, Configuration.BASE_URL_KEY).value = url finally: _db.commit() _db.close()
def process_delete(self, key): self.require_system_admin() setting = ConfigurationSetting.sitewide(self._db, key) setting.value = None return Response(unicode(_("Deleted")), 200)
def decorated(*args, **kwargs): patron = app.manager.index_controller.authenticated_patron_from_request( ) if isinstance(patron, ProblemDetail): return patron.response elif isinstance(patron, Response): return patron else: return f(*args, **kwargs) return decorated patron_web_url = None if (hasattr(app, 'manager') and hasattr(app.manager, '_db')): patron_web_url = ConfigurationSetting.sitewide( app.manager._db, Configuration.PATRON_WEB_CLIENT_URL).value if patron_web_url: # The allows_patron_web decorator will add Cross-Origin Resource Sharing # (CORS) headers to routes that will be used by the patron web interface. # This is necessary for a JS app on a different domain to make requests. # # The partial function sets the arguments to the cross_origin decorator, # since they're the same for all routes that use it. allows_patron_web = partial( cross_origin, origins=[patron_web_url], supports_credentials=True, ) else: # If the patron web client isn't configured, the decorator will do nothing.
def test_from_configuration(self): cls = LogConfiguration config = Configuration m = cls.from_configuration # When logging is configured on initial startup, with no # database connection, these are the defaults. internal_log_level, database_log_level, [handler ], errors = m(None, testing=False) assert cls.INFO == internal_log_level assert cls.WARN == database_log_level assert [] == errors assert isinstance(handler.formatter, JSONFormatter) # The same defaults hold when there is a database connection # but nothing is actually configured. internal_log_level, database_log_level, [handler ], errors = m(self._db, testing=False) assert cls.INFO == internal_log_level assert cls.WARN == database_log_level assert [] == errors assert isinstance(handler.formatter, JSONFormatter) # Let's set up a integrations and change the defaults. self.loggly_integration() self.cloudwatch_integration() internal = self._external_integration( protocol=ExternalIntegration.INTERNAL_LOGGING, goal=ExternalIntegration.LOGGING_GOAL, ) ConfigurationSetting.sitewide(self._db, config.LOG_LEVEL).value = config.ERROR internal.setting( SysLogger.LOG_FORMAT).value = SysLogger.TEXT_LOG_FORMAT ConfigurationSetting.sitewide( self._db, config.DATABASE_LOG_LEVEL).value = config.DEBUG ConfigurationSetting.sitewide(self._db, config.LOG_APP_NAME).value = "test app" template = "%(filename)s:%(message)s" internal.setting(SysLogger.LOG_MESSAGE_TEMPLATE).value = template internal_log_level, database_log_level, handlers, errors = m( self._db, testing=False) assert cls.ERROR == internal_log_level assert cls.DEBUG == database_log_level [loggly_handler ] = [x for x in handlers if isinstance(x, LogglyHandler)] assert "http://example.com/a_token/" == loggly_handler.url assert "test app" == loggly_handler.formatter.app_name [cloudwatch_handler ] = [x for x in handlers if isinstance(x, CloudWatchLogHandler)] assert "simplified" == cloudwatch_handler.stream_name assert "simplified" == cloudwatch_handler.log_group assert 60 == cloudwatch_handler.send_interval [stream_handler ] = [x for x in handlers if isinstance(x, logging.StreamHandler)] assert isinstance(stream_handler.formatter, StringFormatter) assert template == stream_handler.formatter._fmt # If testing=True, then the database configuration is ignored, # and the log setup is one that's appropriate for display # alongside unit test output. internal_log_level, database_log_level, [handler ], errors = m(self._db, testing=True) assert cls.INFO == internal_log_level assert cls.WARN == database_log_level assert SysLogger.DEFAULT_MESSAGE_TEMPLATE == handler.formatter._fmt
Configuration.load() if not Configuration.instance: # No need to import configuration if there isn't any. sys.exit() _db = production_session() try: integrations = [] auth_conf = Configuration.policy('authentication') if not auth_conf: sys.exit() bearer_token_signing_secret = auth_conf.get('bearer_token_signing_secret') secret_setting = ConfigurationSetting.sitewide( _db, OAuthAuthenticationProvider.BEARER_TOKEN_SIGNING_SECRET) if bearer_token_signing_secret: secret_setting.value = bearer_token_signing_secret for provider in auth_conf.get('providers'): integration = make_patron_auth_integration(_db, provider) module = provider.get('module') if module == 'api.millenium_patron': convert_millenium(_db, integration, provider) elif module == 'api.firstbook': convert_firstbook(_db, integration, provider) elif module == 'api.clever': convert_clever(_db, integration, provider) elif module == 'api.sip': convert_sip(_db, integration, provider) else:
@returns_problem_detail def remove(collection_metadata_identifier): return CatalogController(Conf.db).remove_items( collection_details=collection_metadata_identifier ) @app.route('/client/update_url', methods=['POST']) @requires_auth @returns_problem_detail def update_url(): return CatalogController(Conf.db).update_client_url() if __name__ == '__main__': debug = True url = ConfigurationSetting.sitewide(Conf.db, Configuration.BASE_URL_KEY).value scheme, netloc, path, parameters, query, fragment = urlparse.urlparse(url) if ':' in netloc: host, port = netloc.split(':') port = int(port) else: host = netloc port = 80 # Workaround for a "Resource temporarily unavailable" error when # running in debug mode with the global socket timeout set by isbnlib if debug: import socket socket.setdefaulttimeout(None) Conf.log.info("Starting app on %s:%s", host, port)