Cleaned up long lines, unused imports.
[django-cas-provider.git] / cas_provider / views.py
index 7204ae2..2979d1b 100644 (file)
@@ -14,9 +14,7 @@ from models import ServiceTicket
 from utils import create_service_ticket
 from exceptions import SameEmailMismatchedPasswords
 
 from utils import create_service_ticket
 from exceptions import SameEmailMismatchedPasswords
 
-#FIXME: Remove this dependency
-from accounts.models import IdentifierHistory
-from django.contrib.auth.models import User
+from . import signals
 
 __all__ = ['login', 'validate', 'logout']
 
 
 __all__ = ['login', 'validate', 'logout']
 
@@ -37,7 +35,7 @@ def login(request, template_name='cas/login.html', success_redirect='/account/',
             else:
                 url = service + '&ticket=' + ticket.ticket
                 logging.debug('Redirecting to %s', url)
             else:
                 url = service + '&ticket=' + ticket.ticket
                 logging.debug('Redirecting to %s', url)
-                return HttpResponseRedirect()
+                return HttpResponseRedirect(url)
         else:
             logging.debug('Redirecting to %s', success_redirect)
             return HttpResponseRedirect(success_redirect)
         else:
             logging.debug('Redirecting to %s', success_redirect)
             return HttpResponseRedirect(success_redirect)
@@ -49,6 +47,7 @@ def login(request, template_name='cas/login.html', success_redirect='/account/',
             form = LoginForm(request.POST, request=request)
 
         if form.is_valid():
             form = LoginForm(request.POST, request=request)
 
         if form.is_valid():
+            service = form.cleaned_data.get('service', None)
             try:
                 auth_args = dict(username=form.cleaned_data['email'],
                                  password=form.cleaned_data['password'])
             try:
                 auth_args = dict(username=form.cleaned_data['email'],
                                  password=form.cleaned_data['password'])
@@ -131,23 +130,24 @@ def socialauth_login(request, template_name='cas/login.html', success_redirect='
 def validate(request):
     service = request.GET.get('service', None)
     ticket_string = request.GET.get('ticket', None)
 def validate(request):
     service = request.GET.get('service', None)
     ticket_string = request.GET.get('ticket', None)
-    logger.debug("service: %s"% service)
-    logger.debug("ticket_string: %s"% ticket_string)
+    logger.info('Validating ticket %s for %s', ticket_string, service)
     if service is not None and ticket_string is not None:
         try:
             ticket = ServiceTicket.objects.get(ticket=ticket_string)
     if service is not None and ticket_string is not None:
         try:
             ticket = ServiceTicket.objects.get(ticket=ticket_string)
+        except ServiceTicket.DoesNotExist:
+            logger.exception("Tried to validate with an invalid ticket %s for %s", ticket_string, service)
+        except Exception as e:
+            logger.exception('Got an exception: %s', e)
+        else:
             username = ticket.user.username
             ticket.delete()
 
             username = ticket.user.username
             ticket.delete()
 
-            histories = IdentifierHistory.objects.filter(user__email__iexact=ticket.user.email)
-            histories = '\n'.join(histories.values_list('identifier', flat=True))
-            histories = '%s\n' % histories if histories else ''
-
+            results = signals.on_cas_collect_histories.send(sender=validate, for_email=ticket.user.email)
+            histories = '\n'.join('\n'.join(rs) for rc, rs in results)
+            logger.info('Validated %s %s', username, "(also %s)" % histories if histories else '')
             return HttpResponse("yes\n%s\n%s" % (username, histories))
 
             return HttpResponse("yes\n%s\n%s" % (username, histories))
 
-        except Exception as e:
-            logger.exception("Got an exception!: %s"% e)
-
+    logger.info('Validation failed.')
     return HttpResponse("no\n\n")
     
 
     return HttpResponse("no\n\n")