Exemplo n.º 1
0
def copy(mailout, user):
    recipients = mailout.recipient_set.all()

    new_mailout = Mailout(subject=mailout.subject, body=mailout.body, organisation=mailout.organisation, created_by=user)
    new_mailout.save()
    for r in recipients:
        new_recipient = Recipient(mailout=new_mailout, user=r.user)
        new_recipient.save()

    return new_mailout.pk
Exemplo n.º 2
0
    def save(self, user):
        pm = PM(sender=user,
                subject=self.cleaned_data['subject'],
                body=self.cleaned_data['body'])
        pm.save()

        for user in self.cleaned_data['recipients_user']:
            recip = Recipient(recipient=user, message=pm)
            recip.save()
        return pm
Exemplo n.º 3
0
    def save(self, user):
        pm = PM(
            sender = user,
            subject = self.cleaned_data['subject'],
            body = self.cleaned_data['body']
        )
        pm.save()

        for user in self.cleaned_data['recipients_user']:
            recip = Recipient(recipient=user, message=pm)
            recip.save()
        return pm
Exemplo n.º 4
0
def copy(mailout, user):
    recipients = mailout.recipient_set.all()

    new_mailout = Mailout(subject=mailout.subject,
                          body=mailout.body,
                          organisation=mailout.organisation,
                          created_by=user)
    new_mailout.save()
    for r in recipients:
        new_recipient = Recipient(mailout=new_mailout, user=r.user)
        new_recipient.save()

    return new_mailout.pk
Exemplo n.º 5
0
def new(users, user, organisation_id=None):
    """Make and return a new mailout to be sent to the given list of users."""

    if not user.is_staff:
        organisation_id = user.get_profile().representativeprofile.organisation.id

    organisation = None
    if organisation_id:
        organisation = Organisation.objects.get(pk=organisation_id)

    mailout = Mailout(created_by=user, organisation=organisation)
    mailout.save()

    for u in users:
        r = Recipient(user=u)
        r.mailout = mailout
        r.save()

    return mailout
Exemplo n.º 6
0
def new(users, user, organisation_id=None):
    """Make and return a new mailout to be sent to the given list of users."""

    if not user.is_staff:
        organisation_id = user.get_profile(
        ).representativeprofile.organisation.id

    organisation = None
    if organisation_id:
        organisation = Organisation.objects.get(pk=organisation_id)

    mailout = Mailout(created_by=user, organisation=organisation)
    mailout.save()

    for u in users:
        r = Recipient(user=u)
        r.mailout = mailout
        r.save()

    return mailout
Exemplo n.º 7
0
def mailout(request, mailout_id):
    mailout = get_object_or_404(Mailout, pk=mailout_id)

    if request.method == "POST":
        if "delete" in request.POST:
            if mailout.sent:
                messages.info(
                    request,
                    "Cannot delete a mailout that has already been sent.")
            else:
                mailout.delete()
                messages.info(request, "Mailout deleted.")
                return redirect("/mailouts/")
        elif "send" in request.POST:
            # Don't save changes in the form.
            form = forms.MailoutForm(instance=mailout)
            if mailout.sent:
                messages.info(
                    request,
                    "Mailout could not be sent because it has already been sent. This could happen if you accidentally click 'send' twice."
                )
            else:
                logic.send(mailout, request)
                mailout.sent = datetime.datetime.now()
                mailout.sent_by = request.user
                mailout.save()
                messages.info(request, "Your mailout has been dispatched.")
        elif "save" in request.POST:
            form = forms.MailoutForm(request.POST)
            if mailout.sent:
                messages.info(
                    request,
                    "Mailout could not be saved because it has already been sent."
                )
            elif form.is_valid():
                mailout.subject = form.cleaned_data['subject']
                mailout.body = form.cleaned_data['body']
                mailout.save()
                form = forms.MailoutForm(instance=mailout)
                messages.info(
                    request,
                    "Mailout updated. Check the preview and, if the mailout is ready, click 'send' below the preview to dispatch the mailout."
                )
            else:
                messages.info(
                    request,
                    "There is a problem with your mailout. Check the error messages below and try again."
                )
        elif "delete_recipient" in request.POST:
            form = forms.MailoutForm(instance=mailout)
            if mailout.sent:
                messages.info(
                    request,
                    "Cannot delete recipient once mailout has been sent.")
            else:
                recipient = get_object_or_404(
                    Recipient, pk=int(request.POST['recipient_id']))
                recipient.delete()
                messages.info(
                    request, "%s removed from recipient list" %
                    recipient.user.get_full_name())
        elif "new_recipient" in request.POST:
            form = forms.MailoutForm(instance=mailout)
            if mailout.sent:
                messages.info(
                    request,
                    "Cannot add recipient once mailout has been sent.")
            else:
                user = get_object_or_404(User,
                                         pk=int(request.POST['recipient_id']))
                recipient = Recipient(user=user, mailout=mailout)
                recipient.save()
                messages.info(
                    request,
                    user.get_full_name() + " added to the recipient list.")
        else:
            form = forms.MailoutForm(request.POST, instance=mailout)
            messages.info(request, "Unrecognised action!")
    else:
        form = forms.MailoutForm(instance=mailout)

    return render_to_response("mailouts/mailout.html", {
        'mailout': mailout,
        'form': form,
    },
                              context_instance=RequestContext(request))