Revision 357e5b54 flowspec/views.py

b/flowspec/views.py
8 8
from django.core import serializers
9 9
from django.contrib.auth.decorators import login_required
10 10
from django.contrib.auth import logout
11
from django.contrib.sites.models import Site
12
from django.contrib.auth.models import User
11 13
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse
12 14
from django.shortcuts import get_object_or_404, render_to_response
13 15
from django.core.context_processors import request
......
25 27

  
26 28
from flowspy.flowspec.forms import * 
27 29
from flowspy.flowspec.models import *
30
from registration.models import RegistrationProfile
28 31

  
29 32
from copy import deepcopy
30 33
from flowspy.utils.decorators import shib_required
......
239 242
        if error_mail:
240 243
            error = error + "Your idP should release the HTTP_SHIB_INETORGPERSON_MAIL attribute towards this service"
241 244
        if error_username or error_orgname or error_affiliation or error_mail:
242
            return render_to_response('error.html', {'error': error,},
245
            return render_to_response('error.html', {'error': error},
243 246
                                  context_instance=RequestContext(request))
247
        try:
248
            user = User.objects.get(username__exact=username)
249
            user_exists = True
250
        except:
251
            user_exists = False
244 252
        user = authenticate(username=username, firstname=firstname, lastname=lastname, mail=mail, organization=organization, affiliation=affiliation)
245 253
        if user is not None:
246
            login(request, user)
247
            return HttpResponseRedirect(reverse("group-routes"))
248
                # Redirect to a success page.
249
                # Return a 'disabled account' error message
254
            if not user_exists:
255
                user_activation_notify(user)
256
            if user.is_active:
257
               login(request, user)
258
               return HttpResponseRedirect(reverse("group-routes"))
259
            else:
260
                error = "User <strong>%s</strong> is not active yet. Administrators have been notified and will soon activate this account. <br>If your problem persists contact Helpdesk" %user.username
261
                return render_to_response('error.html', {'error': error, 'inactive': True},
262
                                  context_instance=RequestContext(request))
250 263
        else:
251 264
            error = "Something went wrong during user authentication. Contact your administrator"
252 265
            return render_to_response('error.html', {'error': error,},
253 266
                                  context_instance=RequestContext(request))
254
    except Exception as e:
267
    except Exception:
255 268
        error = "Invalid login procedure"
256 269
        return render_to_response('error.html', {'error': error,},
257 270
                                  context_instance=RequestContext(request))
258 271
        # Return an 'invalid login' error message.
259 272
#    return HttpResponseRedirect(reverse("user-routes"))
260 273

  
274
def user_activation_notify(user):
275
    current_site = Site.objects.get_current()
276
    subject = render_to_string('registration/activation_email_subject.txt',
277
                                   { 'site': current_site })
278
    # Email subject *must not* contain newlines
279
    subject = ''.join(subject.splitlines())
280
    registration_profile = RegistrationProfile.objects.create_profile(user)
281
    message = render_to_string('registration/activation_email.txt',
282
                                   { 'activation_key': registration_profile.activation_key,
283
                                     'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
284
                                     'site': current_site,
285
                                     'user': user })
286
    send_new_mail(settings.EMAIL_SUBJECT_PREFIX + subject, 
287
                              message, settings.SERVER_EMAIL,
288
                             get_peer_techc_mails(user), [])
289
    
261 290
@login_required
262 291
@never_cache
263 292
def add_rate_limit(request):

Also available in: Unified diff