Exemplo n.º 1
0
def edit_account(request, id):
    account = get_object_or_404(Account, id=id)

    if request.method == 'POST':
        account_form = AccountForm(data=request.POST, instance=account)
        smtp_form = SMTPForm(data=request.POST, prefix='smtp',
                             instance=account.smtp)
        imap_form = IMAPForm(data=request.POST, prefix='imap',
                             instance=account.imap)
        if all([form.is_valid() for form in (account_form, smtp_form,
                                             imap_form)]):
            account_form.save()
            imap_form.save()
            smtp_form.save()
            messages.success(request, _('Your account have been successfully'
                                        ' updated.'))
            return redirect(reverse('accounts'))
    else:
        account_form = AccountForm(instance=account)
        smtp_form = SMTPForm(prefix='smtp', instance=account.smtp)
        imap_form = IMAPForm(prefix='imap', instance=account.imap)

    context = {
        'account': account,
        'account_form': account_form,
        'imap': imap_form,
        'smtp': smtp_form
    }
    return render(request, 'users/edit_account.html', context)
Exemplo n.º 2
0
def add_account(request):
    if request.method == 'POST':
        account_form = AccountForm(data=request.POST)
        smtp_form = SMTPForm(data=request.POST, prefix='smtp')
        imap_form = IMAPForm(data=request.POST, prefix='imap')
        if all([form.is_valid() for form in (account_form,
                                             smtp_form,
                                             imap_form)]):
            # Create an Account, attach it an IMAP and an SMTP instance.
            account = account_form.save(commit=False)
            account.profile = request.user.get_profile()
            account.imap = imap_form.save()
            account.smtp = smtp_form.save()
            account.save()
            messages.success(request, _('Your account has been successfully '
                                       'created'))
            return redirect(reverse('edit_account', args=[account.id]))
    else:
        account_form = AccountForm()
        smtp_form = SMTPForm(prefix='smtp')
        imap_form = IMAPForm(prefix='imap')
    context = {
        'account': account_form,
        'imap': imap_form,
        'smtp': smtp_form
    }
    return render(request, 'users/add_account.html', context)