Exemplo n.º 1
0
def accept_offer(request, pk, trader_pk):
    if(request.POST['acceptbtn']):
        #Close favor post
        curFavor = Favor.objects.get(pk=pk)
        curFavor.status = 'closed'
        curFavor.save()

        #Create new agreement
        curAgreement = Agreement()
        curAgreement.favor = curFavor
        curAgreement.accepter = User.objects.get(pk=trader_pk)
        curAgreement.save()

        #Create message
        curMessage = Message(subject=curFavor.title, body="Favor agreement has been made. You may now initiate conversation with the other user.", moderation_status='a')
        curMessage.sender = User.objects.get(pk=trader_pk)
        curMessage.recipient = curFavor.author
        curMessage.agreement = curAgreement
        curMessage.save()
        curMessage.thread = curMessage
        curMessage.save()
        otherMessage = Message(subject=curFavor.title, body="Favor agreement has been made. You may now initiate conversation with the other user.", moderation_status='a')
        otherMessage.sender = curFavor.author
        otherMessage.recipient = User.objects.get(pk=trader_pk)
        otherMessage.thread = curMessage
        otherMessage.save()
    return HttpResponseRedirect("/messages/")
Exemplo n.º 2
0
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)
Exemplo n.º 3
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'))            
    
    
Exemplo n.º 4
0
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())
    def create(self,
               validated_data,
               recipient=None,
               parent=None,
               reply_all=None,
               auto_moderators=[]):
        """
        Save as many messages as there are recipients.

        Additional actions:
        - If it's a reply, build a conversation
        - Call auto-moderators
        - Notify parties if needed

        Return False if one of the messages is rejected.

        """

        if validated_data and 'recipients' in validated_data:  # action is "create"
            recipients = validated_data['recipients']
            new_messsage = DmMessage(subject=validated_data['subject'],
                                     body=validated_data['body'],
                                     sender=self.sender,
                                     moderation_status=STATUS_ACCEPTED)
        elif parent:  # action is "reply"
            if reply_all:
                recipients = validated_data['recipients']
            else:
                recipients = parent.sender
            quoted = parent.quote(*(format_subject, format_body))
            # print quoted, parent.subject
            sbjct = validated_data['subject'] if (
                validated_data
                and 'subject' in validated_data) else quoted['subject']
            # bdy = validated_data['body'] if (validated_data and 'body' in validated_data) else format_body(parent.sender, parent.body)
            new_messsage = DmMessage(subject=sbjct,
                                     body=validated_data['body'],
                                     sender=self.sender,
                                     moderation_status=STATUS_ACCEPTED)

        # print type(recipients), new_messsage.subject, new_messsage.body

        if parent and not parent.thread_id:  # at the very first reply, make it a conversation
            parent.thread = parent
            parent.save()
            # but delay the setting of parent.replied_at to the moderation step
        if parent:
            new_messsage.parent = parent
            new_messsage.thread_id = parent.thread_id

        initial_moderation = new_messsage.get_moderation()
        initial_dates = new_messsage.get_dates()
        initial_status = new_messsage.moderation_status
        if recipient:
            if isinstance(recipient,
                          get_user_model()) and recipient in recipients:
                recipients.remove(recipient)
            recipients.insert(0, recipient)
        is_successful = True

        if isinstance(recipients, get_user_model()):  # change to list type
            recipients = [recipients]

        for r in recipients:
            usr_model = get_user_model()
            if isinstance(r, get_user_model()):
                new_messsage.recipient = r
            else:
                new_messsage.recipient = usr_model.objects.get(email=r)
            new_messsage.pk = None  # force_insert=True is not accessible from here
            new_messsage.auto_moderate(auto_moderators)
            new_messsage.clean_moderation(initial_status)
            new_messsage.clean_for_visitor()
            m = new_messsage.save()
            if new_messsage.is_rejected():
                is_successful = False
            new_messsage.update_parent(initial_status)
            # new_messsage.notify_users(initial_status, self.site)

            # some resets for next reuse
            if not isinstance(r, get_user_model()):
                new_messsage.email = ''
            new_messsage.set_moderation(*initial_moderation)
            new_messsage.set_dates(*initial_dates)
        return is_successful