當前位置: 首頁>>代碼示例>>Python>>正文


Python signals.pre_save方法代碼示例

本文整理匯總了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 
開發者ID:muccg,項目名稱:django-useraudit,代碼行數:21,代碼來源:password_expiry.py

示例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() 
開發者ID:aropan,項目名稱:clist,代碼行數:23,代碼來源:models.py

示例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. 
開發者ID:edx,項目名稱:edx-proctoring,代碼行數:26,代碼來源:signals.py

示例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) 
開發者ID:silverapp,項目名稱:silver,代碼行數:26,代碼來源:test_invoice.py

示例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) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:19,代碼來源:tests.py

示例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 
開發者ID:allianceauth,項目名稱:allianceauth,代碼行數:22,代碼來源:signals.py

示例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) 
開發者ID:shtalinberg,項目名稱:django-actions-logger,代碼行數:17,代碼來源:registry.py

示例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. 
開發者ID:edx,項目名稱:edx-proctoring,代碼行數:16,代碼來源:signals.py

示例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') 
開發者ID:edx,項目名稱:edx-proctoring,代碼行數:14,代碼來源:signals.py

示例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. 
開發者ID:edx,項目名稱:edx-proctoring,代碼行數:34,代碼來源:signals.py

示例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') 
開發者ID:edx,項目名稱:edx-proctoring,代碼行數:15,代碼來源:signals.py

示例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). 
開發者ID:WikipediaLibrary,項目名稱:TWLight,代碼行數:28,代碼來源:signals.py

示例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),
        ) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:11,代碼來源:tests.py

示例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) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:11,代碼來源:tests.py

示例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 
開發者ID:maas,項目名稱:maas,代碼行數:5,代碼來源:nodes.py


注:本文中的django.db.models.signals.pre_save方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。