示例#1
0
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'))