Exemplo n.º 1
0
def create_email(request):
    savedsearches = ContactSavedSearch.objects.filter(user=request.user).order_by("-event_date")
    if request.method == "POST":
        form = CreateEmail(request.POST, request.FILES, user=request.user)
        if form.is_valid():
            url = '/emails/update/%d'
            status = ""
            if request.POST['Submit'] == "Save as draft":
                status = "draft"
            elif request.POST['Submit'] == "Save as template":
                status = "template"
            elif request.POST['Submit'] == "Send":
                status = "sent mail"
                url = '/emails/sent/'
            else:
                print "in apps/emails/views.py>create_email.  Template has changed" +\
                      " and view has not been updated.  You getting database errors?"
            e = form.save(extra_attrs={'user': request.user, 'status': status})
            if request.POST['Submit'] == "Send":
                send_email(e)
                return HttpResponseRedirect(url)
            return HttpResponseRedirect(url % e.id)
    else:
        # Check if an event was stuck in the get from the invite interface
        event = request.GET.get('event', None)
        form = CreateEmail(user=request.user)
    return render_to_response('emails/email_form.html', locals())
Exemplo n.º 2
0
def update_email(request, object_id):
    email = Email.objects.get(pk=object_id)
    extra_attrs = {}
    if request.method == "POST":
        request.session['lasteditedemail'] = 0

        if "draft" in request.POST['Submit']:
            if email.status == "draft":
                # update the email
                form = CreateEmail(request.POST, request.FILES, instance=email, user=request.user)
                if form.is_valid():
                    form.save()
                    return HttpResponseRedirect('/emails/list/draft/')
            else:
                # create new email from form as "draft"
                form = CreateEmail(request.POST, request.FILES, user=request.user)
                if form.is_valid():
                    extra_attrs['status'] = "draft"
                    extra_attrs['user'] = request.user
                    new_email = form.save(extra_attrs=extra_attrs)
                    # Copy recipients
                    for contact in email.recipients.all():
                        new_email.recipients.add(contact)

                    return HttpResponseRedirect('/emails/list/draft/')

        elif "template" in request.POST['Submit']:
            # If it's a sent mail make a new template, else just turn it into a template
            if email.status == "sent mail":
                form = CreateEmail(request.POST, request.FILES, user=request.user)
            else:
                form = CreateEmail(request.POST, request.FILES, instance=email, user=request.user)

            if form.is_valid():
                # change the status to "template"
                extra_attrs['status'] = "template"
                extra_attrs['user'] = request.user

                new_email = form.save(extra_attrs=extra_attrs)
                                # If new copy recipients
                if email.id != new_email.id:
                    for contact in email.recipients.all():
                        new_email.recipients.add(contact)

                return HttpResponseRedirect('/emails/list/template/')
        elif "Send" in request.POST['Submit']:
            # update the email
            if email.status == 'draft':
                form = CreateEmail(request.POST, request.FILES, instance=email, user=request.user)
            else:
                form = CreateEmail(request.POST, request.FILES, user=request.user)

            if form.is_valid():
                # change the status to "sent mail"
                extra_attrs['status'] = "sent mail"
                extra_attrs['user'] = request.user
                new_email = form.save(extra_attrs=extra_attrs)
                
                # If new copy recipients and attachment
                if email.id != new_email.id:
                    for contact in email.recipients.all():
                        new_email.recipients.add(contact)

                    if new_email.attachment in (None, ''):
                        new_email.attachment = email.attachment
                        print "attachment copied: %s" % (email.attachment,)
                        new_email.save()
                # send the email
                send_email(new_email)
                return HttpResponseRedirect('/emails/sent/')

    elif request.method == "GET":
        request.session['lasteditedemail'] = email.id
        form = CreateEmail(instance=email, user=request.user)


    savedsearches = ContactSavedSearch.objects.filter(user=request.user).order_by("-event_date")
    atchfilename = email.attachment.name.split("/")[-1]

    return render_to_response('emails/email_form.html',locals())