本文整理汇总了Python中django.db.models.signals.post_save.send方法的典型用法代码示例。如果您正苦于以下问题:Python post_save.send方法的具体用法?Python post_save.send怎么用?Python post_save.send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.signals.post_save
的用法示例。
在下文中一共展示了post_save.send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_participants
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def add_participants(self, request, *participants_ids):
"""
Ensures the current user has the authorization to add the participants.
By default, a user can add a participant if he himself is a participant.
A callback can be added in the settings here.
"""
participants_ids_returned_by_callback = getattr(settings, 'REST_MESSAGING_ADD_PARTICIPANTS_CALLBACK', self._limit_participants)(request, *participants_ids)
participations = []
ids = []
for participant_id in participants_ids_returned_by_callback:
participations.append(Participation(participant_id=participant_id, thread=self))
ids.append(participant_id)
Participation.objects.bulk_create(participations)
post_save.send(Thread, instance=self, created=True, created_and_add_participants=True, request_participant_id=request.rest_messaging_participant.id)
return ids
示例2: ajax_change_status
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def ajax_change_status(request):
if not request.is_ajax():
return HttpResponseForbidden()
post_dict = dict(request.POST)
if 'statuses_id[]' in post_dict and 'profile_ids[]' in post_dict:
statuses = UserStatus.objects.filter(id__in=post_dict['statuses_id[]'])
for profile in UserProfile.objects.filter(id__in=post_dict['profile_ids[]']):
for status in statuses:
profile.set_status(status)
post_save.send(UserProfile, instance=profile, created=False)
reversion.set_user(request.user)
reversion.set_comment("Change from user status bulk change")
return HttpResponse("OK")
示例3: test_pre_social_login
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def test_pre_social_login(self):
"""Test the pre-social-login Allauth signal handling."""
mock_obj = mock.Mock()
discord_login = SocialLogin(self.django_user, self.social_user)
github_login = SocialLogin(self.django_user, self.social_user_github)
unmapped_login = SocialLogin(self.django_user, self.social_unmapped)
with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
AllauthSignalListener()
# Don't attempt to apply groups if the user doesn't have a linked Discord account
pre_social_login.send(SocialLogin, sociallogin=github_login)
mock_obj.assert_not_called()
# Don't attempt to apply groups if the user hasn't joined the Discord server
pre_social_login.send(SocialLogin, sociallogin=unmapped_login)
mock_obj.assert_not_called()
# Attempt to apply groups if everything checks out
pre_social_login.send(SocialLogin, sociallogin=discord_login)
mock_obj.assert_called_with(self.discord_user, self.social_user)
示例4: test_social_added
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def test_social_added(self):
"""Test the social-account-added Allauth signal handling."""
mock_obj = mock.Mock()
discord_login = SocialLogin(self.django_user, self.social_user)
github_login = SocialLogin(self.django_user, self.social_user_github)
unmapped_login = SocialLogin(self.django_user, self.social_unmapped)
with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
AllauthSignalListener()
# Don't attempt to apply groups if the user doesn't have a linked Discord account
social_account_added.send(SocialLogin, sociallogin=github_login)
mock_obj.assert_not_called()
# Don't attempt to apply groups if the user hasn't joined the Discord server
social_account_added.send(SocialLogin, sociallogin=unmapped_login)
mock_obj.assert_not_called()
# Attempt to apply groups if everything checks out
social_account_added.send(SocialLogin, sociallogin=discord_login)
mock_obj.assert_called_with(self.discord_user, self.social_user)
示例5: test_social_updated
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def test_social_updated(self):
"""Test the social-account-updated Allauth signal handling."""
mock_obj = mock.Mock()
discord_login = SocialLogin(self.django_user, self.social_user)
github_login = SocialLogin(self.django_user, self.social_user_github)
unmapped_login = SocialLogin(self.django_user, self.social_unmapped)
with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
AllauthSignalListener()
# Don't attempt to apply groups if the user doesn't have a linked Discord account
social_account_updated.send(SocialLogin, sociallogin=github_login)
mock_obj.assert_not_called()
# Don't attempt to apply groups if the user hasn't joined the Discord server
social_account_updated.send(SocialLogin, sociallogin=unmapped_login)
mock_obj.assert_not_called()
# Attempt to apply groups if everything checks out
social_account_updated.send(SocialLogin, sociallogin=discord_login)
mock_obj.assert_called_with(self.discord_user, self.social_user)
示例6: test_social_removed
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def test_social_removed(self):
"""Test the social-account-removed Allauth signal handling."""
mock_obj = mock.Mock()
with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
AllauthSignalListener()
# Don't attempt to remove groups if the user doesn't have a linked Discord account
social_account_removed.send(SocialLogin, socialaccount=self.social_user_github)
mock_obj.assert_not_called()
# Don't attempt to remove groups if the social account doesn't map to a Django user
social_account_removed.send(SocialLogin, socialaccount=self.social_unmapped)
mock_obj.assert_not_called()
# Attempt to remove groups if everything checks out
social_account_removed.send(SocialLogin, socialaccount=self.social_user)
mock_obj.assert_called_with(self.discord_user, self.social_user, deletion=True)
示例7: get_or_create_thread
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def get_or_create_thread(self, request, name=None, *participant_ids):
"""
When a Participant posts a message to other participants without specifying an existing Thread,
we must
1. Create a new Thread if they have not yet opened the discussion.
2. If they have already opened the discussion and multiple Threads are not allowed for the same users, we must
re-attach this message to the existing thread.
3. If they have already opened the discussion and multiple Threads are allowed, we simply create a new one.
"""
# we get the current participant
# or create him if he does not exit
participant_ids = list(participant_ids)
if request.rest_messaging_participant.id not in participant_ids:
participant_ids.append(request.rest_messaging_participant.id)
# we need at least one other participant
if len(participant_ids) < 2:
raise Exception('At least two participants are required.')
if getattr(settings, "REST_MESSAGING_THREAD_UNIQUE_FOR_ACTIVE_RECIPIENTS", True) is True:
# if we limit the number of threads by active participants
# we ensure a thread is not already running
existing_threads = self.get_active_threads_involving_all_participants(*participant_ids)
if len(list(existing_threads)) > 0:
return existing_threads[0]
# we have no existing Thread or multiple Thread instances are allowed
thread = Thread.objects.create(name=name)
# we add the participants
thread.add_participants(request, *participant_ids)
# we send a signal to say the thread with participants is created
post_save.send(Thread, instance=thread, created=True, created_and_add_participants=True, request_participant_id=request.rest_messaging_participant.id)
return thread
示例8: remove_participant
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def remove_participant(self, request, participant):
removable_participants_ids = self.get_removable_participants_ids(request)
if participant.id in removable_participants_ids:
participation = Participation.objects.get(participant=participant, thread=self, date_left=None)
participation.date_left = now()
participation.save()
post_save.send(Thread, instance=self, created=False, remove_participant=True, removed_participant=participant, request_participant_id=request.rest_messaging_participant.id)
return participation
else:
raise Exception('The participant may not be removed.')
示例9: on_model_pre_create
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def on_model_pre_create(self, model, instance):
try:
model_cls, django_instance = self.get_django_instance(model, instance)
pre_save.send(model_cls, raw=True, using=self, instance=django_instance, update_fields=[])
except Exception as e:
logger.warning('[!] on_model_pre_create signal failed: {}'.format(str(e)))
示例10: on_model_post_create
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def on_model_post_create(self, model, instance):
try:
model_cls, django_instance = self.get_django_instance(model, instance)
post_save.send(model_cls, raw=True, using=self, instance=django_instance, created=True, update_fields=[])
except Exception as e:
logger.warning('[!] on_model_post_create signal failed: {}'.format(str(e)))
示例11: on_model_pre_update
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def on_model_pre_update(self, model, instance):
try:
model_cls, django_instance = self.get_django_instance(model, instance)
pre_save.send(model_cls, raw=True, using=self, instance=django_instance, update_fields=[])
except Exception as e:
logger.warning('[!] on_model_pre_update signal failed: {}'.format(str(e)))
示例12: on_model_post_update
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def on_model_post_update(self, model, instance):
try:
model_cls, django_instance = self.get_django_instance(model, instance)
post_save.send(model_cls, raw=True, using=self, instance=django_instance, created=False, update_fields=[])
except Exception as e:
logger.warning('[!] on_model_post_update signal failed: {}'.format(str(e)))
示例13: on_model_pre_delete
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def on_model_pre_delete(self, model, instance):
try:
model_cls, django_instance = self.get_django_instance(model, instance)
pre_delete.send(model_cls, using=self, instance=django_instance)
self.pre_delete_django_instance = django_instance
except Exception as e:
logger.warning('[!] on_model_pre_delete signal failed: {}'.format(str(e)))
示例14: run_journal_signals
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def run_journal_signals():
"""
Gets and saves all journal objects forcing them to fire signals, 1.2 -> 1.3 introduced a couple of signals
so we want them to be fired on upgrade.
:return: None
"""
print('Processing journal signals')
journals = journal_models.Journal.objects.all()
for journal in journals:
print('Firing signals for {journal_code}'.format(journal_code=journal.code), end='...')
post_save.send(journal_models.Journal, instance=journal, created=True)
print(' [OK]')
示例15: test_model_save
# 需要导入模块: from django.db.models.signals import post_save [as 别名]
# 或者: from django.db.models.signals.post_save import send [as 别名]
def test_model_save(self):
"""Test signal handling for when Discord user model objects are saved to DB."""
mock_obj = mock.Mock()
with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
AllauthSignalListener()
post_save.send(
DiscordUser,
instance=self.discord_user,
raw=True,
created=None, # Not realistic, but we don't use it
using=None, # Again, we don't use it
update_fields=False # Always false during integration testing
)
mock_obj.assert_not_called()
post_save.send(
DiscordUser,
instance=self.discord_user,
raw=False,
created=None, # Not realistic, but we don't use it
using=None, # Again, we don't use it
update_fields=False # Always false during integration testing
)
mock_obj.assert_called_with(self.discord_user, self.social_user)