本文整理汇总了Python中config.Configuration.policy方法的典型用法代码示例。如果您正苦于以下问题:Python Configuration.policy方法的具体用法?Python Configuration.policy怎么用?Python Configuration.policy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类config.Configuration
的用法示例。
在下文中一共展示了Configuration.policy方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: server_side_validation
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
def server_side_validation(self, identifier, password):
if not hasattr(self, 'identifier_re'):
self.identifier_re = Configuration.policy(
Configuration.IDENTIFIER_REGULAR_EXPRESSION,
default=Configuration.DEFAULT_IDENTIFIER_REGULAR_EXPRESSION)
if not hasattr(self, 'password_re'):
self.password_re = Configuration.policy(
Configuration.PASSWORD_REGULAR_EXPRESSION,
default=Configuration.DEFAULT_PASSWORD_REGULAR_EXPRESSION)
valid = True
if self.identifier_re:
valid = valid and (self.identifier_re.match(identifier) is not None)
if self.password_re:
valid = valid and (self.password_re.match(password) is not None)
return valid
示例2: page
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
def page(cls, _db, title, url, annotator=None,
use_materialized_works=True):
"""Create a feed of content to preload on devices."""
configured_content = Configuration.policy(Configuration.PRELOADED_CONTENT)
identifiers = [Identifier.parse_urn(_db, urn)[0] for urn in configured_content]
identifier_ids = [identifier.id for identifier in identifiers]
if use_materialized_works:
from core.model import MaterializedWork
q = _db.query(MaterializedWork)
q = q.filter(MaterializedWork.primary_identifier_id.in_(identifier_ids))
# Avoid eager loading of objects that are contained in the
# materialized view.
q = q.options(
lazyload(MaterializedWork.license_pool, LicensePool.data_source),
lazyload(MaterializedWork.license_pool, LicensePool.identifier),
lazyload(MaterializedWork.license_pool, LicensePool.edition),
)
else:
q = _db.query(Work).join(Work.primary_edition)
q = q.filter(Edition.primary_identifier_id.in_(identifier_ids))
works = q.all()
feed = cls(_db, title, url, works, annotator)
annotator.annotate_feed(feed, None)
content = unicode(feed)
return content
示例3: make_lanes
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
def make_lanes(_db, definitions=None):
definitions = definitions or Configuration.policy(
Configuration.LANES_POLICY
)
if not definitions:
lanes = make_lanes_default(_db)
else:
lanes = [Lane(_db=_db, **definition) for definition in definitions]
return LaneList.from_description(_db, None, lanes)
示例4: initialize
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
def initialize(cls, _db, test=False):
if test:
from millenium_patron import (
DummyMilleniumPatronAPI,
)
return cls(DummyMilleniumPatronAPI(), [])
providers = Configuration.policy("authentication")
if not providers:
raise CannotLoadConfiguration(
"No authentication policy given."
)
if isinstance(providers, basestring):
providers = [providers]
basic_auth_provider = None
oauth_providers = []
for provider_string in providers:
provider_module = importlib.import_module(provider_string)
provider_class = getattr(provider_module, "AuthenticationAPI")
if provider_class.TYPE == Authenticator.BASIC_AUTH:
if basic_auth_provider != None:
raise CannotLoadConfiguration(
"Two basic auth providers configured"
)
basic_auth_provider = provider_class.from_config()
elif provider_class.TYPE == Authenticator.OAUTH:
oauth_providers.append(provider_class.from_config())
else:
raise CannotLoadConfiguration(
"Unrecognized authentication provider: %s" % provider
)
if not basic_auth_provider and not oauth_providers:
raise CannotLoadConfiguration(
"No authentication provider configured"
)
return cls(basic_auth_provider, oauth_providers)
示例5: borrow
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
def borrow(self, patron, pin, licensepool, delivery_mechanism,
hold_notification_email):
"""Either borrow a book or put it on hold. Don't worry about fulfilling
the loan yet.
:return: A 3-tuple (`Loan`, `Hold`, `is_new`). Either `Loan`
or `Hold` must be None, but not both.
"""
now = datetime.datetime.utcnow()
if licensepool.open_access:
# We can 'loan' open-access content ourselves just by
# putting a row in the database.
now = datetime.datetime.utcnow()
__transaction = self._db.begin_nested()
loan, is_new = licensepool.loan_to(patron, start=now, end=None)
__transaction.commit()
return loan, None, is_new
# Okay, it's not an open-access book. This means we need to go
# to an external service to get the book.
#
# This also means that our internal model of whether this book
# is currently on loan or on hold might be wrong.
api = self.api_for_license_pool(licensepool)
must_set_delivery_mechanism = (
api.SET_DELIVERY_MECHANISM_AT == BaseCirculationAPI.BORROW_STEP)
if must_set_delivery_mechanism and not delivery_mechanism:
raise DeliveryMechanismMissing()
content_link = content_expires = None
internal_format = api.internal_format(delivery_mechanism)
if patron.fines:
def parse_fines(fines):
dollars, cents = re.match("\$([\d]+)\.(\d\d)", fines).groups()
return (dollars * 100) + cents
max_fines = Configuration.policy(Configuration.MAX_OUTSTANDING_FINES)
if max_fines:
if parse_fines(patron.fines) >= parse_fines(max_fines):
raise OutstandingFines()
# First, try to check out the book.
loan_info = None
try:
loan_info = api.checkout(
patron, pin, licensepool, internal_format
)
except AlreadyCheckedOut:
# This is good, but we didn't get the real loan info.
# Just fake it.
identifier = licensepool.identifier
loan_info = LoanInfo(
identifier.type,
identifier,
start_date=None,
end_date=now + datetime.timedelta(hours=1)
)
except NoAvailableCopies:
# That's fine, we'll just (try to) place a hold.
pass
if loan_info:
# We successfuly secured a loan. Now create it in our
# database.
__transaction = self._db.begin_nested()
loan, is_new = licensepool.loan_to(
patron, start=loan_info.start_date or now,
end=loan_info.end_date)
if must_set_delivery_mechanism:
loan.fulfillment = delivery_mechanism
existing_hold = get_one(
self._db, Hold, patron=patron, license_pool=licensepool,
on_multiple='interchangeable'
)
if existing_hold:
# The book was on hold, and now we have a loan.
# Delete the record of the hold.
self._db.delete(existing_hold)
__transaction.commit()
return loan, None, is_new
# Checking out a book didn't work, so let's try putting
# the book on hold.
try:
hold_info = api.place_hold(
patron, pin, licensepool,
hold_notification_email
)
except AlreadyOnHold, e:
hold_info = HoldInfo(
licensepool.identifier.type, licensepool.identifier.identifier,
None, None, None
)
示例6: borrow
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
def borrow(self, patron, pin, licensepool, delivery_mechanism,
hold_notification_email):
"""Either borrow a book or put it on hold. Don't worry about fulfilling
the loan yet.
:return: A 3-tuple (`Loan`, `Hold`, `is_new`). Either `Loan`
or `Hold` must be None, but not both.
"""
now = datetime.datetime.utcnow()
if licensepool.open_access:
# We can 'loan' open-access content ourselves just by
# putting a row in the database.
now = datetime.datetime.utcnow()
__transaction = self._db.begin_nested()
loan, is_new = licensepool.loan_to(patron, start=now, end=None)
__transaction.commit()
return loan, None, is_new
# Okay, it's not an open-access book. This means we need to go
# to an external service to get the book.
#
# This also means that our internal model of whether this book
# is currently on loan or on hold might be wrong.
api = self.api_for_license_pool(licensepool)
must_set_delivery_mechanism = (
api.SET_DELIVERY_MECHANISM_AT == BaseCirculationAPI.BORROW_STEP)
if must_set_delivery_mechanism and not delivery_mechanism:
raise DeliveryMechanismMissing()
content_link = content_expires = None
internal_format = api.internal_format(delivery_mechanism)
if patron.fines:
def parse_fines(fines):
dollars, cents = re.match("\$([\d]+)\.(\d\d)", fines).groups()
return (dollars * 100) + cents
max_fines = Configuration.policy(Configuration.MAX_OUTSTANDING_FINES)
if max_fines:
if parse_fines(patron.fines) >= parse_fines(max_fines):
raise OutstandingFines()
# Do we (think we) already have this book out on loan?
existing_loan = get_one(
self._db, Loan, patron=patron, license_pool=licensepool,
on_multiple='interchangeable'
)
loan_info = None
hold_info = None
if existing_loan:
# Sync with the API to see if the loan still exists. If
# it does, we still want to perform a 'checkout' operation
# on the API, because that's how loans are renewed, but
# certain error conditions (like NoAvailableCopies) mean
# something different if you already have a confirmed
# active loan.
self.sync_bookshelf(patron, pin)
existing_loan = get_one(
self._db, Loan, patron=patron, license_pool=licensepool,
on_multiple='interchangeable'
)
try:
loan_info = api.checkout(
patron, pin, licensepool, internal_format
)
except AlreadyCheckedOut:
# This is good, but we didn't get the real loan info.
# Just fake it.
identifier = licensepool.identifier
loan_info = LoanInfo(
identifier.type,
identifier,
start_date=None,
end_date=now + datetime.timedelta(hours=1)
)
except AlreadyOnHold:
# We're trying to check out a book that we already have on hold.
hold_info = HoldInfo(
licensepool.identifier.type, licensepool.identifier.identifier,
None, None, None
)
except NoAvailableCopies:
if existing_loan:
# The patron tried to renew a loan but there are
# people waiting in line for them to return the book,
# so renewals are not allowed.
raise CannotRenew(
_("You cannot renew a loan if other patrons have the work on hold.")
)
else:
# That's fine, we'll just (try to) place a hold.
#
# Since the patron incorrectly believed there were
# copies available, update availability information
# immediately.
#.........这里部分代码省略.........
示例7: log_import
# 需要导入模块: from config import Configuration [as 别名]
# 或者: from config.Configuration import policy [as 别名]
_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)
log.info('Default facets imported')
except Exception as e:
# Catch any error and roll back the database so the full
开发者ID:NYPL-Simplified,项目名称:content_server,代码行数:33,代码来源:20170714-1-move-third-party-config-to-external-integrations.py