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 handle(self, *args, **options): now = datetime.datetime.now() dow = now.weekday() begin = now + datetime.timedelta(minutes=WARN_DELAY_MIN) end = now + datetime.timedelta(minutes=WARN_DELAY_MAX) slots = WebclassSlot.published.filter(day=dow, start_hour__gte=begin.time(), start_hour__lt=end.time()) site = Site.objects.all()[0] admin = User.objects.get(username='******') date = now.strftime('%d/%m/%Y') for slot in slots: hour = slot.start_hour.strftime('%Hh%M') print("Sending notifications for %s:%s (%s)" % (slot.webclass.course, slot.start_hour, slot.id)) subject = "Webclass_%s_%s_%s" % (slot.webclass.course, date, hour) body = """Votre Webclasse va commencer.""" users = slot.participants.all() for user in users: print(" => %s" % user) msg = Message(subject=subject, body=body, sender=admin, recipient=user) msg.moderation_status = 'a' msg.save() notify_user(msg, 'acceptance', site)
def send(self): site = Site.objects.all()[0] users = [student.user for student in self.group.students.all()] for user in users: mess = Message(sender=self.sender, recipient=user, subject=self.subject[:119], body=self.message) mess.moderation_status = 'a' mess.save() notify_user(mess, 'acceptance', site)
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 reject(self): self.date_marked = datetime.datetime.now() self.date_rejected = datetime.datetime.now() site = Site.objects.all()[0] context = {'script': self, 'site': site} text = render_to_string('exam/messages/script_rejected.txt', context) a = ugettext('Script') v = ugettext('rejected') subject = '%s %s' % (a, v) mess = Message(sender=self.corrector, recipient=self.author, subject=subject[:119], body=text) mess.moderation_status = 'a' mess.save() notify_user(mess, 'acceptance', site)
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)