def pm_broadcast(sender, recipients, subject, body='', skip_notification=False): """ Broadcast a message to multiple Users. For an easier cleanup, all these messages are directly marked as archived and deleted on the sender side. The message is expected to be issued from a trusted application, so moderation is not necessary and the status is automatically set to 'accepted'. Optional argument: ``skip_notification``: if the normal notification event is not wished """ message = Message(subject=subject, body=body, sender=sender, sender_archived=True, sender_deleted_at=now(), moderation_status=STATUS_ACCEPTED, moderation_date=now()) if not isinstance(recipients, (tuple, list)): recipients = (recipients, ) for recipient in recipients: message.recipient = recipient message.pk = None message.save() if not skip_notification: message.notify_users(STATUS_PENDING)
def pm_write(sender, recipient, subject, body='', skip_notification=False, auto_archive=False, auto_delete=False, auto_moderators=None): """ Write a message to a User. Contrary to pm_broadcast(), the message is archived and/or deleted on the sender side only if requested. The message may come from an untrusted application, a gateway for example, so it may be useful to involve some auto moderators in the processing. Optional arguments: ``skip_notification``: if the normal notification event is not wished ``auto_archive``: to mark the message as archived on the sender side ``auto_delete``: to mark the message as deleted on the sender side ``auto_moderators``: a list of auto-moderation functions """ message = Message(subject=subject, body=body, sender=sender, recipient=recipient) initial_status = message.moderation_status if auto_moderators: message.auto_moderate(auto_moderators) else: message.moderation_status = STATUS_ACCEPTED message.clean_moderation(initial_status) if auto_archive: message.sender_archived = True if auto_delete: message.sender_deleted_at = now() message.save() if not skip_notification: message.notify_users(initial_status, _get_site())
def create_message(sender, recipient, subject, body='', skip_notification=False, auto_archive=False, auto_delete=False, auto_moderators=None, sent_at=None): if not user_is_valid(sender): raise InvalidSenderException() if not user_is_valid(recipient): raise InvalidRecipientException() if subject is None or subject == '': raise NoMessageSubjectException('this message has an empty subject line. Messages without subject cannot be sent') if body is None or body == '': raise NoMessageBodyException('this message has an empty message body. Empty message cannot be sent') message = Message(subject=subject, body=body, sender=sender, recipient=recipient) if not sent_at is None: message.sent_at = sent_at initial_status = message.moderation_status if auto_moderators: message.auto_moderate(auto_moderators) else: message.moderation_status = STATUS_ACCEPTED message.clean_moderation(initial_status) if auto_archive: message.sender_archived = True if auto_delete: message.sender_deleted_at = now() message.save() if not skip_notification: message.notify_users(initial_status, _get_site()) return message
def pm_write(sender, recipient, subject, body='', skip_notification=False, auto_archive=False, auto_delete=False): """ Write a message to a User. Contrary to pm_broadcast(), the message is archived and/or deleted on the sender side only if requested. Optional arguments: ``skip_notification``: if the normal notification event is not wished ``auto_archive``: to mark the message as archived on the sender side ``auto_delete``: to mark the message as deleted on the sender side """ message = Message(subject=subject, body=body, sender=sender, recipient=recipient, moderation_status=STATUS_ACCEPTED, moderation_date=now()) if auto_archive: message.sender_archived = True if auto_delete: message.sender_deleted_at = now() message.save() if not skip_notification: message.notify_users(STATUS_PENDING)
def deliver_pending(request): ''' Find notifications in Queue state and deliver to all recipients. We actually do two things here: 1) Created a Delivered record in our own Notifications app, so we have a custom record that can be displayed in the widget, which the user can check off as completed, etc.; and 2) Deliver a system message via Postman. The get_members() method on DynamicList aggregates all logical recipients. Django-Postman lets us save a Message instance and takes care of delivery. This function is never accessed by users - superusers and cron jobs only. Superusers can trigger delivery without cron by accessing /notifications/deliver_pending ''' notifications = Notification.objects.filter(state='queue') for n in notifications: # Find all members associated with this notification's associated dynamic list if n.dlist: recips = n.dlist.get_members() if n.offering: recips = n.offering.get_members() # What if there's both a dlist and an offering? Need a way to combine them here for r in recips: # Create Postman message msg = Message() # Instantiate new message on Postman's Message class msg.subject = 'New %s on CalCentral: %s' % (n.type, n.title) msg.body = n.description msg.sender = n.author msg.recipient = r msg.moderation_status = STATUS_ACCEPTED # a = accepted. Override postman default = all msgs are pending msg.notify_users(STATUS_PENDING,is_auto_moderated=True) msg.save() # Create a Deliver instance for each user d = Delivered() d.notification = n d.user = r d.completed = False d.deliver_date = datetime.datetime.now() d.save() # Move the notification from the queue to the archive n.state = 'arch' n.save() # Since this is not a browser view, just need an empty httpresponse return HttpResponseRedirect(reverse('notifications'))
def pm_write_corp(sender, recipient, subject, body='', msgType=None,auto_moderators=None): if auto_moderators == None: auto_moderators= [mod1] else: auto_moderators.append(mod1) message = Message(subject=subject, body=body, sender=sender, recipient=recipient) initial_status = message.moderation_status if auto_moderators: message.auto_moderate(auto_moderators) else: message.moderation_status = STATUS_ACCEPTED message.clean_moderation(initial_status) message.save() message.notify_users(initial_status,msgType=msgType)
def pm_broadcast(sender, recipients, subject, body='', skip_notification=False): """ Broadcast a message to multiple Users. For an easier cleanup, all these messages are directly marked as archived and deleted on the sender side. The message is expected to be issued from a trusted application, so moderation is not necessary and the status is automatically set to 'accepted'. Optional argument: ``skip_notification``: if the normal notification event is not wished """ message = Message(subject=subject, body=body, sender=sender, sender_archived=True, sender_deleted_at=now(), moderation_status=STATUS_ACCEPTED, moderation_date=now()) if not isinstance(recipients, (tuple, list)): recipients = (recipients,) for recipient in recipients: message.recipient = recipient message.pk = None message.save() if not skip_notification: message.notify_users(STATUS_PENDING, _get_site())