Commit 90f1762d authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

rewrite ``activate`` view

parent 39525987
......@@ -232,16 +232,37 @@ def send_change_email(ec, request, email_template_name='registration/email_chang
msg = 'Sent change email for %s' % ec.user.email
logger._log(LOGGING_LEVEL, msg, [])
def activate(user, email_template_name='im/welcome_email.txt'):
def activate(user, email_template_name='im/welcome_email.txt',
helpdesk_email_template_name='im/helpdesk_notification.txt', verify_email=False):
"""
Activates the specific user and sends email.
Raises SendGreetingError, ValidationError
"""
user.is_active = True
if verify_email:
user.email_verified = True
user.save()
send_helpdesk_notification(user, helpdesk_email_template_name)
send_greeting(user, email_template_name)
def switch_account_to_shibboleth(user, local_user):
if not user or not isinstance(user, AstakosUser):
return
if not local_user or not isinstance(user, AstakosUser):
return
if not user.provider == 'shibboleth':
return
user.delete()
local_user.provider = 'shibboleth'
local_user.set_unusable_password()
local_user.third_party_identifier = user.third_party_identifier
local_user.save()
return local_user
def invite(invitation, inviter, email_template_name='im/welcome_email.txt'):
"""
Send an invitation email and upon success reduces inviter's invitation by one.
......
......@@ -59,7 +59,7 @@ from astakos.im.activation_backends import get_backend, SimpleBackend
from astakos.im.util import get_context, prepare_response, set_cookie, get_query
from astakos.im.forms import *
from astakos.im.functions import send_greeting, send_feedback, SendMailError, \
invite as invite_func, logout as auth_logout, send_helpdesk_notification
invite as invite_func, logout as auth_logout, activate as activate_func, switch_account_to_shibboleth
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, COOKIE_NAME, COOKIE_DOMAIN, IM_MODULES, SITENAME, LOGOUT_NEXT, LOGGING_LEVEL
logger = logging.getLogger(__name__)
......@@ -431,8 +431,7 @@ def logout(request, template='registration/logged_out.html', extra_context={}):
return response
@transaction.commit_manually
def activate(request, email_template_name='im/welcome_email.txt', on_failure='im/signup.html',
helpdesk_email_template_name='im/helpdesk_notification.txt'):
def activate(request, email_template_name='im/welcome_email.txt', helpdesk_email_template_name='im/helpdesk_notification.txt'):
"""
Activates the user identified by the ``auth`` request parameter, sends a welcome email
and renews the user token.
......@@ -448,53 +447,49 @@ def activate(request, email_template_name='im/welcome_email.txt', on_failure='im
return HttpResponseBadRequest(_('No such user'))
if user.is_active:
message = 'Account already active.'
message = _('Account already active.')
messages.add_message(request, messages.ERROR, message)
return render_response(on_failure)
return index(request)
notify_helpdesk = False
try:
local_user = AstakosUser.objects.get(~Q(id = user.id), email=user.email, is_active=True)
except AstakosUser.DoesNotExist:
user.is_active = True
user.email_verified = True
try:
user.save()
except ValidationError, e:
return HttpResponseBadRequest(e)
notify_helpdesk = True
activate_func(user, email_template_name, helpdesk_email_template_name, verify_email=True)
response = prepare_response(request, user, next, renew=True)
transaction.commit()
return response
except SendMailError, e:
message = e.message
messages.add_message(request, messages.ERROR, message)
transaction.rollback()
return index(request)
except BaseException, e:
status = messages.ERROR
message = _('Something went wrong.')
messages.add_message(request, messages.ERROR, message)
logger.exception(e)
transaction.rollback()
return index(request)
else:
# switch the existing account to shibboleth one
if user.provider == 'shibboleth':
local_user.provider = 'shibboleth'
local_user.set_unusable_password()
local_user.third_party_identifier = user.third_party_identifier
try:
local_user.save()
except ValidationError, e:
return HttpResponseBadRequest(e)
user.delete()
user = local_user
try:
if notify_helpdesk:
send_helpdesk_notification(user, helpdesk_email_template_name)
send_greeting(user, email_template_name)
response = prepare_response(request, user, next, renew=True)
transaction.commit()
return response
except SendMailError, e:
message = e.message
messages.add_message(request, messages.ERROR, message)
transaction.rollback()
return render_response(on_failure)
except BaseException, e:
status = messages.ERROR
message = _('Something went wrong.')
messages.add_message(request, messages.ERROR, message)
logger.exception(e)
transaction.rollback()
return signup(request, on_failure)
try:
user = switch_account_to_shibboleth(user, local_user)
send_greeting(user, email_template_name)
response = prepare_response(request, user, next, renew=True)
transaction.commit()
return response
except SendMailError, e:
message = e.message
messages.add_message(request, messages.ERROR, message)
transaction.rollback()
return index(request)
except BaseException, e:
status = messages.ERROR
message = _('Something went wrong.')
messages.add_message(request, messages.ERROR, message)
logger.exception(e)
transaction.rollback()
return index(request)
def approval_terms(request, term_id=None, template_name='im/approval_terms.html', extra_context={}):
term = None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment