本文整理匯總了Python中django.dispatch.Signal方法的典型用法代碼示例。如果您正苦於以下問題:Python dispatch.Signal方法的具體用法?Python dispatch.Signal怎麽用?Python dispatch.Signal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.dispatch
的用法示例。
在下文中一共展示了dispatch.Signal方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: read_notification
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def read_notification(**kwargs):
"""
Mark notification as read.
Raises NotificationError if the user doesn't have access
to read the notification
"""
warnings.warn(
'The \'read\' Signal will be removed in 2.6.5 '
'Please use the helper functions in notifications.utils',
PendingDeprecationWarning
)
notify_id = kwargs['notify_id']
recipient = kwargs['recipient']
notification = Notification.objects.get(id=notify_id)
if recipient != notification.recipient:
raise NotificationError('You cannot read this notification')
notification.read()
示例2: create_notification
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def create_notification(**kwargs):
"""Notify signal receiver."""
warnings.warn(
'The \'notify\' Signal will be removed in 2.6.5 '
'Please use the helper functions in notifications.utils',
PendingDeprecationWarning
)
# make fresh copy and retain kwargs
params = kwargs.copy()
del params['signal']
del params['sender']
try:
del params['silent']
except KeyError:
pass
notification = Notification(**params)
# If it's a not a silent notification, save the notification
if not kwargs.get('silent', False):
notification.save()
# Send the notification asynchronously with celery
send_notification.delay(notification.to_json())
示例3: __unicode__
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def __unicode__(self):
return self.name
#
# Signal receivers
#
示例4: __init__
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def __init__(self, func, signal: Signal = None, kwargs=None):
super().__init__(func)
if kwargs is None:
kwargs = {}
self.signal = signal
self.signal_kwargs = kwargs
self._serializer = None
self.signal.connect(self.handle, **self.signal_kwargs)
示例5: catch_ldap_error
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def catch_ldap_error(signal: Signal, **kwargs: Any) -> None:
"""
Inside django_auth_ldap populate_user(), if LDAPError is raised,
e.g. due to invalid connection credentials, the function catches it
and emits a signal (ldap_error) to communicate this error to others.
We normally don't use signals, but here there's no choice, so in this function
we essentially convert the signal to a normal exception that will properly
propagate out of django_auth_ldap internals.
"""
if kwargs['context'] == 'populate_user':
# The exception message can contain the password (if it was invalid),
# so it seems better not to log that, and only use the original exception's name here.
raise PopulateUserLDAPError(kwargs['exception'].__class__.__name__)
示例6: get_signal_list
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def get_signal_list(self):
if not hasattr(self.sender._meta, 'module_name'):
return Signal.objects.filter(
model__model=self.sender._meta.model_name,
is_active=True
)
return Signal.objects.filter(
model__model=self.sender._meta.module_name,
is_active=True
)
示例7: run_deferred
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def run_deferred(self):
try:
self.signal = Signal.objects.get(pk=self.signal_pk, is_active=True)
self.get_current_instance()
self.send_mail()
except ObjectDoesNotExist:
pass
示例8: initial_signals
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def initial_signals():
for signal in Signal.objects.filter(is_active=True):
def_signal = getattr(signals, signal.signal)
def_signal.connect(
signal_receiver, sender=signal.model.model_class(),
dispatch_uid=signal.model.name
)
示例9: test_documentation_includes_signals
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def test_documentation_includes_signals(app):
app = "byro." + app.split(".")[1]
with suppress(ImportError):
module = importlib.import_module(app + ".signals")
for key in dir(module):
attrib = getattr(module, key)
if isinstance(attrib, Signal):
assert (
key in plugin_docs
), "Signal {app}.signals.{key} is not documented!".format(
app=app, key=key
)
示例10: test_cannot_connect_no_kwargs
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def test_cannot_connect_no_kwargs(self):
def receiver_no_kwargs(sender):
pass
msg = 'Signal receivers must accept keyword arguments (**kwargs).'
with self.assertRaisesMessage(ValueError, msg):
a_signal.connect(receiver_no_kwargs)
self.assertTestIsClean(a_signal)
示例11: test_cannot_connect_non_callable
# 需要導入模塊: from django import dispatch [as 別名]
# 或者: from django.dispatch import Signal [as 別名]
def test_cannot_connect_non_callable(self):
msg = 'Signal receivers must be callable.'
with self.assertRaisesMessage(AssertionError, msg):
a_signal.connect(object())
self.assertTestIsClean(a_signal)