Revision 554ef175

b/snf-astakos-app/astakos/im/views/im.py
145 145
@require_http_methods(["GET", "POST"])
146 146
@cookie_fix
147 147
@valid_astakos_user_required
148
@transaction.commit_manually
148
@transaction.commit_on_success
149 149
def invite(request, template_name='im/invitations.html', extra_context=None):
150 150
    """
151 151
    Allows a user to invite somebody else.
......
154 154
    In case of POST checks whether the user has not run out of invitations and then
155 155
    sends an invitation email to singup to the service.
156 156

  
157
    The view uses commit_manually decorator in order to ensure the number of the
158
    user invitations is going to be updated only if the email has been successfully sent.
157
    The number of the user invitations is going to be updated only if the email
158
    has been successfully sent.
159 159

  
160 160
    If the user isn't logged in, redirects to settings.LOGIN_URL.
161 161

  
......
188 188
        form = InvitationForm(request.POST)
189 189
        if inviter.invitations > 0:
190 190
            if form.is_valid():
191
                try:
192
                    email = form.cleaned_data.get('username')
193
                    realname = form.cleaned_data.get('realname')
194
                    invite_func(inviter, email, realname)
195
                    message = _(astakos_messages.INVITATION_SENT) % locals()
196
                    messages.success(request, message)
197
                except Exception, e:
198
                    transaction.rollback()
199
                    raise
200
                else:
201
                    transaction.commit()
191
                email = form.cleaned_data.get('username')
192
                realname = form.cleaned_data.get('realname')
193
                invite_func(inviter, email, realname)
194
                message = _(astakos_messages.INVITATION_SENT) % locals()
195
                messages.success(request, message)
202 196
        else:
203 197
            message = _(astakos_messages.MAX_INVITATION_NUMBER_REACHED)
204 198
            messages.error(request, message)
......
365 359
                                                          extra_context))
366 360

  
367 361

  
368
@transaction.commit_manually
362
@transaction.commit_on_success
369 363
@require_http_methods(["GET", "POST"])
370 364
@cookie_fix
371 365
def signup(request, template_name='im/signup.html', on_success='index',
......
409 403
    if request.user.is_authenticated():
410 404
        logger.info("%s already signed in, redirect to index",
411 405
                    request.user.log_display)
412
        transaction.rollback()
413 406
        return HttpResponseRedirect(reverse('index'))
414 407

  
415 408
    provider = get_query(request).get('provider', 'local')
416 409
    if not auth.get_provider(provider).get_create_policy:
417 410
        logger.error("%s provider not available for signup", provider)
418
        transaction.rollback()
419 411
        raise PermissionDenied
420 412

  
421 413
    instance = None
......
466 458
            **form_kwargs)
467 459

  
468 460
        if form.is_valid():
469
            commited = False
470
            try:
471
                user = form.save(commit=False)
472

  
473
                # delete previously unverified accounts
474
                if AstakosUser.objects.user_exists(user.email):
475
                    AstakosUser.objects.get_by_identifier(user.email).delete()
476

  
477
                # store_user so that user auth providers get initialized
478
                form.store_user(user, request)
479
                result = activation_backend.handle_registration(user)
480
                if result.status == \
481
                        activation_backend.Result.PENDING_MODERATION:
482
                    # user should be warned that his account is not active yet
483
                    status = messages.WARNING
484
                else:
485
                    status = messages.SUCCESS
486
                message = result.message
487
                activation_backend.send_result_notifications(result, user)
461
            user = form.save(commit=False)
462

  
463
            # delete previously unverified accounts
464
            if AstakosUser.objects.user_exists(user.email):
465
                AstakosUser.objects.get_by_identifier(user.email).delete()
466

  
467
            # store_user so that user auth providers get initialized
468
            form.store_user(user, request)
469
            result = activation_backend.handle_registration(user)
470
            if result.status == \
471
                    activation_backend.Result.PENDING_MODERATION:
472
                # user should be warned that his account is not active yet
473
                status = messages.WARNING
474
            else:
475
                status = messages.SUCCESS
476
            message = result.message
477
            activation_backend.send_result_notifications(result, user)
488 478

  
489
                # commit user entry
490
                transaction.commit()
491
                # commited flag
492
                # in case an exception get raised from this point
493
                commited = True
494

  
495
                if user and user.is_active:
496
                    # activation backend directly activated the user
497
                    # log him in
498
                    next = request.POST.get('next', '')
499
                    response = prepare_response(request, user, next=next)
500
                    return response
501

  
502
                messages.add_message(request, status, message)
503
                return HttpResponseRedirect(reverse(on_success))
504
            except Exception, e:
505
                if not commited:
506
                    transaction.rollback()
507
                raise
508
    else:
509
        transaction.commit()
479
            # commit user entry
480
            transaction.commit()
481

  
482
            if user and user.is_active:
483
                # activation backend directly activated the user
484
                # log him in
485
                next = request.POST.get('next', '')
486
                response = prepare_response(request, user, next=next)
487
                return response
488

  
489
            messages.add_message(request, status, message)
490
            return HttpResponseRedirect(reverse(on_success))
510 491

  
511 492
    return render_response(template_name,
512 493
                           signup_form=form,
......
611 592

  
612 593
@require_http_methods(["GET", "POST"])
613 594
@cookie_fix
614
@transaction.commit_manually
595
@transaction.commit_on_success
615 596
def activate(request, greeting_email_template_name='im/welcome_email.txt',
616 597
             helpdesk_email_template_name='im/helpdesk_notification.txt'):
617 598
    """
618 599
    Activates the user identified by the ``auth`` request parameter, sends a
619 600
    welcome email and renews the user token.
620 601

  
621
    The view uses commit_manually decorator in order to ensure the user state
622
    will be updated only if the email will be send successfully.
602
    The user state will be updated only if the email will be send successfully.
623 603
    """
624 604
    token = request.GET.get('auth')
625 605
    next = request.GET.get('next')
......
633 613
    try:
634 614
        user = AstakosUser.objects.get(verification_code=token)
635 615
    except AstakosUser.DoesNotExist:
636
        transaction.rollback()
637 616
        raise Http404
638 617

  
639 618
    if user.email_verified:
......
641 620
        messages.error(request, message)
642 621
        return HttpResponseRedirect(reverse('index'))
643 622

  
644
    try:
645
        backend = activation_backends.get_backend()
646
        result = backend.handle_verification(user, token)
647
        backend.send_result_notifications(result, user)
648
        next = settings.ACTIVATION_REDIRECT_URL or next
649
        response = HttpResponseRedirect(reverse('index'))
650
        if user.is_active:
651
            response = prepare_response(request, user, next, renew=True)
652
            messages.success(request, _(result.message))
653
        else:
654
            messages.warning(request, _(result.message))
655
    except Exception:
656
        transaction.rollback()
657
        raise
623
    backend = activation_backends.get_backend()
624
    result = backend.handle_verification(user, token)
625
    backend.send_result_notifications(result, user)
626
    next = settings.ACTIVATION_REDIRECT_URL or next
627
    response = HttpResponseRedirect(reverse('index'))
628
    if user.is_active:
629
        response = prepare_response(request, user, next, renew=True)
630
        messages.success(request, _(result.message))
658 631
    else:
659
        transaction.commit()
660
        return response
632
        messages.warning(request, _(result.message))
633

  
634
    return response
661 635

  
662 636

  
663 637
@require_http_methods(["GET", "POST"])
......
720 694

  
721 695
@require_http_methods(["GET", "POST"])
722 696
@cookie_fix
723
@transaction.commit_manually
697
@transaction.commit_on_success
724 698
def change_email(request, activation_key=None,
725 699
                 email_template_name='registration/email_change_email.txt',
726 700
                 form_template_name='registration/email_change_form.html',
......
737 711
                email_change = EmailChange.objects.get(
738 712
                    activation_key=activation_key)
739 713
            except EmailChange.DoesNotExist:
740
                transaction.rollback()
741 714
                logger.error("[change-email] Invalid or used activation "
742 715
                             "code, %s", activation_key)
743 716
                raise Http404
......
753 726
            else:
754 727
                logger.error("[change-email] Access from invalid user, %s %s",
755 728
                             email_change.user, request.user.log_display)
756
                transaction.rollback()
757 729
                raise PermissionDenied
758 730
        except ValueError, e:
759 731
            messages.error(request, e)
......
780 752

  
781 753
    form = EmailChangeForm(request.POST or None)
782 754
    if request.method == 'POST' and form.is_valid():
783
        try:
784
            ec = form.save(request, email_template_name, request)
785
        except Exception, e:
786
            transaction.rollback()
787
            raise
788
        else:
789
            msg = _(astakos_messages.EMAIL_CHANGE_REGISTERED)
790
            messages.success(request, msg)
791
            transaction.commit()
792
            return HttpResponseRedirect(reverse('edit_profile'))
755
        ec = form.save(request, email_template_name, request)
756
        msg = _(astakos_messages.EMAIL_CHANGE_REGISTERED)
757
        messages.success(request, msg)
758
        transaction.commit()
759
        return HttpResponseRedirect(reverse('edit_profile'))
793 760

  
794 761
    if request.user.email_change_is_pending():
795 762
        messages.warning(request, astakos_messages.PENDING_EMAIL_CHANGE_REQUEST)

Also available in: Unified diff