本文整理汇总了Python中django.db.models.signals.pre_save方法的典型用法代码示例。如果您正苦于以下问题:Python signals.pre_save方法的具体用法?Python signals.pre_save怎么用?Python signals.pre_save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.signals
的用法示例。
在下文中一共展示了signals.pre_save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user_pre_save
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def user_pre_save(sender, instance=None, raw=False, **kwargs):
user = instance
attrs = ExpirySettings.get()
# We're saving the password change date only for existing users
# Users just created should be taken care of by auto_now_add.
# This way we can assume that a User profile object already exists
# for the user. This is essential, because password change detection
# can happen only in pre_save, in post_save it is too late.
is_new_user = user.pk is None
if is_new_user or raw:
return
if attrs.date_changed:
update_date_changed(user, attrs.date_changed)
# User has been re-activated. Ensure the last_login is set to None so
# that the user isn't inactivated on next login by the AccountExpiryBackend
current_user = sender.objects.get(pk=user.pk)
if not current_user.is_active and user.is_active:
user.last_login = None
示例2: update_account_url
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def update_account_url(signal, instance, **kwargs):
def default_url():
args = [instance.key, instance.resource.host]
return reverse('coder:account', args=args)
if signal is pre_save:
if instance.url:
return
instance.url = default_url()
elif signal is m2m_changed:
if not kwargs.get('action').startswith('post_'):
return
url = None
for coder in instance.coders.iterator():
if url is not None:
url = None
break
url = reverse('coder:profile', args=[coder.username]) + f'?search=resource:{instance.resource.host}'
instance.url = url
instance.save()
示例3: save_exam_on_backend
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def save_exam_on_backend(sender, instance, **kwargs): # pylint: disable=unused-argument
"""
Save the exam to the backend provider when our model changes.
It also combines the review policy into the exam when saving to the backend
"""
if sender == models.ProctoredExam:
exam_obj = instance
review_policy = models.ProctoredExamReviewPolicy.get_review_policy_for_exam(instance.id)
else:
exam_obj = instance.proctored_exam
review_policy = instance
if exam_obj.is_proctored:
from edx_proctoring.serializers import ProctoredExamJSONSafeSerializer
exam = ProctoredExamJSONSafeSerializer(exam_obj).data
if review_policy:
exam['rule_summary'] = review_policy.review_policy
backend = get_backend_provider(exam)
external_id = backend.on_exam_saved(exam)
if external_id and external_id != exam_obj.external_id:
exam_obj.external_id = external_id
exam_obj.save()
# Hook up the pre_save signal to record creations in the ProctoredExamReviewPolicyHistory table.
示例4: test_get_invoice
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def test_get_invoice(authenticated_api_client, settings, issued_invoice):
invoice = issued_invoice
customer = issued_invoice.customer
issued_invoice.generate_pdf()
with mute_signals(pre_save):
[
TransactionFactory.create(
state=state, invoice=issued_invoice,
payment_method=PaymentMethodFactory(customer=customer)
)
for state in Transaction.States.as_list()
if state not in [Transaction.States.Canceled,
Transaction.States.Refunded,
Transaction.States.Failed]
]
url = reverse('invoice-detail', kwargs={'pk': invoice.pk})
response = authenticated_api_client.get(url, format='json')
assert response.status_code == status.HTTP_200_OK, response.data
invoice_definition.check_response(invoice, response_data=response.data)
示例5: test_decorators
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def test_decorators(self):
data = []
@receiver(signals.pre_save, weak=False)
def decorated_handler(signal, sender, instance, **kwargs):
data.append(instance)
@receiver(signals.pre_save, sender=Car, weak=False)
def decorated_handler_with_sender_arg(signal, sender, instance, **kwargs):
data.append(instance)
try:
c1 = Car.objects.create(make="Volkswagen", model="Passat")
self.assertEqual(data, [c1, c1])
finally:
signals.pre_save.disconnect(decorated_handler)
signals.pre_save.disconnect(decorated_handler_with_sender_arg, sender=Car)
示例6: pre_save_config
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def pre_save_config(sender, instance, *args, **kwargs):
"""
Checks if enable was toggled on group config and
deletes groups if necessary.
"""
logger.debug("Received pre_save from {}".format(instance))
if not instance.pk:
# new model being created
return
try:
old_instance = AutogroupsConfig.objects.get(pk=instance.pk)
# Check if enable was toggled, delete groups?
if old_instance.alliance_groups is True and instance.alliance_groups is False:
instance.delete_alliance_managed_groups()
if old_instance.corp_groups is True and instance.corp_groups is False:
instance.delete_corp_managed_groups()
except AutogroupsConfig.DoesNotExist:
pass
示例7: __init__
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def __init__(self, create=True, update=True, delete=True, custom=None):
from actionslog.receivers import action_log_create, action_log_update, action_log_delete
self._registry = {}
self._signals = {}
if create:
self._signals[post_save] = action_log_create
if update:
self._signals[pre_save] = action_log_update
if delete:
self._signals[post_delete] = action_log_delete
if custom is not None:
self._signals.update(custom)
示例8: on_review_policy_changed
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def on_review_policy_changed(sender, instance, signal, **kwargs): # pylint: disable=unused-argument
"""
Archiving all changes made to the Review Policy.
Will only archive on update/delete, and not on new entries created.
"""
if signal is pre_save:
if instance.id:
instance = sender.objects.get(id=instance.id)
else:
return
models.archive_model(models.ProctoredExamReviewPolicyHistory, instance, id='original_id')
# Hook up the post_save signal to record creations in the ProctoredExamStudentAllowanceHistory table.
示例9: on_allowance_changed
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def on_allowance_changed(sender, instance, signal, **kwargs): # pylint: disable=unused-argument
"""
Archiving all changes made to the Student Allowance.
Will only archive on update/delete, and not on new entries created.
"""
if signal is pre_save:
if instance.id:
instance = sender.objects.get(id=instance.id)
else:
return
models.archive_model(models.ProctoredExamStudentAllowanceHistory, instance, id='allowance_id')
示例10: on_attempt_changed
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def on_attempt_changed(sender, instance, signal, **kwargs): # pylint: disable=unused-argument
"""
Archive the exam attempt whenever the attempt status is about to be
modified. Make a new entry with the previous value of the status in the
ProctoredExamStudentAttemptHistory table.
"""
if signal is pre_save:
if instance.id:
# on an update case, get the original
# and see if the status has changed, if so, then we need
# to archive it
original = sender.objects.get(id=instance.id)
if original.status != instance.status:
instance = original
else:
return
else:
return
else:
# remove the attempt on the backend
# timed exams have no backend
backend = get_backend_provider(name=instance.proctored_exam.backend)
if backend:
result = backend.remove_exam_attempt(instance.proctored_exam.external_id, instance.external_id)
if not result:
log.error(u'Failed to remove attempt %d from %s', instance.id, backend.verbose_name)
models.archive_model(models.ProctoredExamStudentAttemptHistory, instance, id='attempt_id')
# Hook up the signals to record updates/deletions in the ProctoredExamSoftwareSecureReview table.
示例11: on_review_changed
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def on_review_changed(sender, instance, signal, **kwargs): # pylint: disable=unused-argument
"""
Archiving all changes made to the Review.
Will only archive on update/delete, and not on new entries created.
"""
if signal is pre_save:
if instance.id:
# only for update cases
instance = sender.objects.get(id=instance.id)
else:
# don't archive on create
return
models.archive_model(models.ProctoredExamSoftwareSecureReviewHistory, instance, id='review_id')
示例12: set_partner_status
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def set_partner_status(sender, **kwargs):
"""
Whenever an application is approved (except for in BatchEditView)
we do some calculations to see if we've run out of accounts. If
we have, we mark the partner as waitlisted if not, continue.
"""
partner_pk = kwargs["partner_pk"]
try:
partner = Partner.objects.get(pk=partner_pk)
partner.status = Partner.WAITLIST
partner.save()
except Partner.DoesNotExist:
logger.info(
"set_partner_status signal received, but"
" partner {pk} does not exist - unable to set"
" partner status".format(pk=partner_pk)
)
# IMPORTANT: pre_save is not sent by Queryset.update(), so *none of this
# behavior will happen on if you update() an Application queryset*.
# That is sometimes okay, but it is not always okay.
# Errors caused by days_open not existing when expected to
# exist can show up in weird parts of the application (for example, template
# rendering failing when get_num_days_open returns None but its output is
# passed to a template filter that needs an integer).
示例13: setUp
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def setUp(self):
# Save up the number of connected signals so that we can check at the
# end that all the signals we register get properly unregistered (#9989)
self.pre_signals = (
len(signals.pre_save.receivers),
len(signals.post_save.receivers),
len(signals.pre_delete.receivers),
len(signals.post_delete.receivers),
)
示例14: tearDown
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def tearDown(self):
# All our signals got disconnected properly.
post_signals = (
len(signals.pre_save.receivers),
len(signals.post_save.receivers),
len(signals.pre_delete.receivers),
len(signals.post_delete.receivers),
)
self.assertEqual(self.pre_signals, post_signals)
示例15: post_init_store_previous_status
# 需要导入模块: from django.db.models import signals [as 别名]
# 或者: from django.db.models.signals import pre_save [as 别名]
def post_init_store_previous_status(sender, instance, **kwargs):
"""Store the pre_save status of the instance."""
instance.__previous_status = instance.status