Ejemplo n.º 1
0
def email_changelist(request):
    email_accounts = get_email_accounts()
    return render_to_response(
        'email_changelist.html',
        {
            'emails': email_accounts,
        },
        RequestContext(request),
    )
Ejemplo n.º 2
0
def email_changelist(request):
    email_accounts = get_email_accounts()
    return render_to_response(
        'email_changelist.html',
        {
            'emails': email_accounts,
        },
        RequestContext(request),
    )
Ejemplo n.º 3
0
def email_changeform(request, id=None):
    if request.method == 'POST':
        # Submitting the form
        form = EmailForm(request.POST)
        change = False
        if form.is_valid():
            f = form.cleaned_data
            server = xmlrpclib.Server('https://api.webfaction.com/')
            session_id, account = server.login(settings.WEBFACTION_USERNAME, settings.WEBFACTION_PASSWORD)
            if id == None:
                # Creating a new email
                if f['create_mailbox']:
                    mailbox_name = generate_mailbox_name(f['email_address'])
                    password = server.create_mailbox(session_id, mailbox_name, f['enable_spam_protection'])['password']
                    targets = generate_targets(mailbox_name, f['redirect'])
                else:
                    targets = generate_targets(None, f['redirect'])
                server.create_email(session_id, f['email_address'], targets, f['autoresponder_on'], f['autoresponder_subject'], f['autoresponder_message'])
                email_msg = "Created email address %s" % f['email_address']
                l = Log(user=request.user, action=email_msg)
                l.save()
                messages.add_message(request, messages.INFO, email_msg)
                if f['create_mailbox']:
                    mailbox_msg = "Created mailbox %s" % mailbox_name
                    password_msg = mailbox_msg +" with password %s" % password
                    if settings.WEBFACTION_LOG_PASSWORD:
                        l = Log(user=request.user, action=password_msg)
                    else:
                        l = Log(user=request.user, action=mailbox_msg)
                    l.save()
                    messages.add_message(request, messages.INFO, password_msg)
            else:
                # Editing an existing email
                change = True
                message_list = []
                update_email = False
                if f['autoresponder_on']!=f['autoresponder_on_prev']:
                    message_list.append("Autoresponder Status for %s changed to %s" % (f['email_address'], f['autoresponder_on']))
                    update_email = True
                if f['autoresponder_subject']!=f['autoresponder_subject_prev']:
                    message_list.append("Autoresponder Subject for %s changed from '%s' to '%s'" % (f['email_address'], f['autoresponder_subject_prev'], f['autoresponder_subject']))
                    update_email = True
                if f['autoresponder_message']!=f['autoresponder_message_prev']:
                    message_list.append("Autoresponder Message for %s changed from '%s' to '%s'" % (f['email_address'], f['autoresponder_message_prev'], f['autoresponder_message']))
                    update_email = True
                if f['redirect']!=f['redirect_prev']:
                    message_list.append("Redirect Address for %s changed from '%s' to '%s'" % (f['email_address'], f['redirect_prev'], f['redirect']))
                    update_email = True
                if update_email:
                    mailbox_name = f.get('mailbox_prev', None)
                    targets = generate_targets(mailbox_name, f['redirect'])
                    server.update_email(session_id, f['email_address'], targets, f['autoresponder_on'], f['autoresponder_subject'], f['autoresponder_message'], f['email_address'])
                if f['enable_spam_protection']!=f['enable_spam_protection_prev']:
                    try:
                        server.update_mailbox(session_id, mailbox_name, f['enable_spam_protection'])
                        message_list.append("Spam Protection Status for %s changed to %s" % (f['enable_spam_protection'], f['email_address']))
                    except xmlrpclib.Fault: #Probably means this is a redirect only address
                        message_list.append("Error. Can only change spam protection status on addresses with their own mailbox")
                for msg in message_list:
                    messages.add_message(request, messages.INFO, msg)
                    l = Log(user=request.user, action=msg)
                    l.save()
            return HttpResponseRedirect('..')
    else:
        # Blank form
        if id==None: # We are creating
            change = False
            form = EmailForm()
        else: # We are updating
            change = True
            email_accounts = get_email_accounts()
            email_account = [x for x in email_accounts if x['id']==int(id)][0] # Assume only one match
            if email_account.get('mailbox', None):
                # Has a mailbox
                enable_spam_protection = email_account['mailbox']['enable_spam_protection']
            else:
                # Is just a redirect
                enable_spam_protection = False
            if email_account.get('mailbox', False):
                mailbox_name = email_account['mailbox']['mailbox']
            else:
                mailbox_name = ''
            form = EmailForm({
                'email_address': email_account['email_address'],
                'email_address_prev': email_account['email_address'],
                'autoresponder_on': email_account['autoresponder_on'],
                'autoresponder_on_prev': email_account['autoresponder_on'],
                'autoresponder_subject': email_account['autoresponder_subject'],
                'autoresponder_subject_prev': email_account['autoresponder_subject'],
                'autoresponder_message': email_account['autoresponder_message'],
                'autoresponder_message_prev': email_account['autoresponder_message'],
                'enable_spam_protection': enable_spam_protection,
                'enable_spam_protection_prev': enable_spam_protection ,
                'create_mailbox': email_account.get('mailbox', False),
                'mailbox_prev' : mailbox_name,
                'redirect': email_account.get('redirect', ''),
                'redirect_prev': email_account.get('redirect', ''),
            })
            del form.fields['create_mailbox']
    return render_to_response('email_changeform.html', {
        'change': change,
        'form': form,
        },
        RequestContext(request),
    )
Ejemplo n.º 4
0
def email_changeform(request, id=None):
    if request.method == 'POST':
        # Submitting the form
        form = EmailForm(request.POST)
        change = False
        if form.is_valid():
            f = form.cleaned_data
            server = xmlrpclib.Server('https://api.webfaction.com/')
            session_id, account = server.login(settings.WEBFACTION_USERNAME,
                                               settings.WEBFACTION_PASSWORD)
            if id == None:
                # Creating a new email
                if f['create_mailbox']:
                    mailbox_name = generate_mailbox_name(f['email_address'])
                    password = server.create_mailbox(
                        session_id, mailbox_name,
                        f['enable_spam_protection'])['password']
                    targets = generate_targets(mailbox_name, f['redirect'])
                else:
                    targets = generate_targets(None, f['redirect'])
                server.create_email(session_id, f['email_address'], targets,
                                    f['autoresponder_on'],
                                    f['autoresponder_subject'],
                                    f['autoresponder_message'])
                email_msg = "Created email address %s" % f['email_address']
                l = Log(user=request.user, action=email_msg)
                l.save()
                request.user.message_set.create(message=email_msg)
                if f['create_mailbox']:
                    mailbox_msg = "Created mailbox %s" % mailbox_name
                    password_msg = mailbox_msg + " with password %s" % password
                    if settings.WEBFACTION_LOG_PASSWORD:
                        l = Log(user=request.user, action=password_msg)
                    else:
                        l = Log(user=request.user, action=mailbox_msg)
                    l.save()
                    request.user.message_set.create(message=password_msg)
            else:
                # Editing an existing email
                change = True
                msgs = []
                update_email = False
                if f['autoresponder_on'] != f['autoresponder_on_prev']:
                    msgs.append("Autoresponder Status for %s changed to %s" %
                                (f['email_address'], f['autoresponder_on']))
                    update_email = True
                if f['autoresponder_subject'] != f[
                        'autoresponder_subject_prev']:
                    msgs.append(
                        "Autoresponder Subject for %s changed from '%s' to '%s'"
                        % (f['email_address'], f['autoresponder_subject_prev'],
                           f['autoresponder_subject']))
                    update_email = True
                if f['autoresponder_message'] != f[
                        'autoresponder_message_prev']:
                    msgs.append(
                        "Autoresponder Message for %s changed from '%s' to '%s'"
                        % (f['email_address'], f['autoresponder_message_prev'],
                           f['autoresponder_message']))
                    update_email = True
                if f['redirect'] != f['redirect_prev']:
                    msgs.append(
                        "Redirect Address for %s changed from '%s' to '%s'" %
                        (f['email_address'], f['redirect_prev'],
                         f['redirect']))
                    update_email = True
                if update_email:
                    mailbox_name = f.get('mailbox_prev', None)
                    targets = generate_targets(mailbox_name, f['redirect'])
                    server.update_email(session_id, f['email_address'],
                                        targets, f['autoresponder_on'],
                                        f['autoresponder_subject'],
                                        f['autoresponder_message'],
                                        f['email_address'])
                if f['enable_spam_protection'] != f[
                        'enable_spam_protection_prev']:
                    try:
                        server.update_mailbox(session_id, mailbox_name,
                                              f['enable_spam_protection'])
                        msgs.append(
                            "Spam Protection Status for %s changed to %s" %
                            (f['enable_spam_protection'], f['email_address']))
                    except xmlrpclib.Fault:  #Probably means this is a redirect only address
                        msgs.append(
                            "Error. Can only change spam protection status on addresses with their own mailbox"
                        )
                for msg in msgs:
                    messages.info(request, msg)
                    l = Log(user=request.user, action=msg)
                    l.save()
            return HttpResponseRedirect('..')
    else:
        # Blank form
        if id == None:  # We are creating
            change = False
            form = EmailForm()
        else:  # We are updating
            change = True
            email_accounts = get_email_accounts()
            email_account = [x for x in email_accounts
                             if x['id'] == int(id)][0]  # Assume only one match
            if email_account.get('mailbox', None):
                # Has a mailbox
                enable_spam_protection = email_account['mailbox'][
                    'enable_spam_protection']
            else:
                # Is just a redirect
                enable_spam_protection = False
            if email_account.get('mailbox', False):
                mailbox_name = email_account['mailbox']['mailbox']
            else:
                mailbox_name = ''
            form = EmailForm({
                'email_address':
                email_account['email_address'],
                'email_address_prev':
                email_account['email_address'],
                'autoresponder_on':
                email_account['autoresponder_on'],
                'autoresponder_on_prev':
                email_account['autoresponder_on'],
                'autoresponder_subject':
                email_account['autoresponder_subject'],
                'autoresponder_subject_prev':
                email_account['autoresponder_subject'],
                'autoresponder_message':
                email_account['autoresponder_message'],
                'autoresponder_message_prev':
                email_account['autoresponder_message'],
                'enable_spam_protection':
                enable_spam_protection,
                'enable_spam_protection_prev':
                enable_spam_protection,
                'create_mailbox':
                email_account.get('mailbox', False),
                'mailbox_prev':
                mailbox_name,
                'redirect':
                email_account.get('redirect', ''),
                'redirect_prev':
                email_account.get('redirect', ''),
            })
            del form.fields['create_mailbox']
    return render_to_response(
        'email_changeform.html',
        {
            'change': change,
            'form': form,
        },
        RequestContext(request),
    )