def update_ancestor(sender, instance, **kwargs):
if kwargs['created'] or not kwargs['update_fields'] or 'parent' in kwargs['update_fields']:
ancestor_or_self = instance.recursive_ancestor_or_self()
if instance.ancestor_or_self != ancestor_or_self:
FundingInstitution.objects.filter(id=instance.id).update(
ancestor_or_self=ancestor_or_self
)
if not kwargs['raw']:
for child in instance.fundinginstitution_set.all():
post_save.send(
sender=sender,
instance=child,
raw=kwargs['raw'],
created=kwargs['created'],
using=kwargs['using'],
update_fields=kwargs['update_fields'],
)
python类send()的实例源码
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
def update(self, **kwargs):
"""
Convert custom '__raw' flag to 'raw' flag from base_save.
Call post_save signal on update.
"""
raw = kwargs.get('__raw', False)
if raw:
del kwargs['__raw']
super(KFTQuerySet, self).update(**kwargs)
for instance in self._clone():
post_save.send(
sender=self.model,
instance=instance,
raw=raw
)
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
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.')
def fake_save(self, instance):
cls = instance.__class__
pre_save.send(sender=cls, instance=instance)
for field in cls._meta.fields:
if isinstance(field, FileField):
getattr(instance, field.name)._committed = True
post_save.send(sender=cls, instance=instance)
return self.storage.listdir('avatars')[1]
def update(self, using=None, **kwargs):
"""
Updates specified attributes on the current instance.
"""
assert self.pk, "Cannot update an instance that has not yet been created."
using = using or router.db_for_write(self.__class__, instance=self)
for field in self._meta.fields:
if getattr(field, 'auto_now', False) and field.name not in kwargs:
kwargs[field.name] = field.pre_save(self, False)
affected = self.__class__._base_manager.using(using).filter(pk=self.pk).update(**kwargs)
for k, v in kwargs.iteritems():
if isinstance(v, ExpressionNode):
v = resolve_expression_node(self, v)
setattr(self, k, v)
if affected == 1:
post_save.send(sender=self.__class__, instance=self, created=False)
return affected
elif affected == 0:
return affected
elif affected < 0:
raise ValueError("Somehow we have updated a negative amount of rows, you seem to have a problem with your db backend.")
else:
raise ValueError("Somehow we have updated multiple rows, and you are now royally fucked.")