Cleaned up long lines, unused imports.
[django-cas-provider.git] / cas_provider / views.py
index 295c3f3..2979d1b 100644 (file)
@@ -1,4 +1,5 @@
 import logging
+logger = logging.getLogger('cas_provider.views')
 import urllib
 
 from django.http import HttpResponse, HttpResponseRedirect
@@ -13,6 +14,8 @@ from models import ServiceTicket
 from utils import create_service_ticket
 from exceptions import SameEmailMismatchedPasswords
 
+from . import signals
+
 __all__ = ['login', 'validate', 'logout']
 
 
@@ -32,19 +35,19 @@ def login(request, template_name='cas/login.html', success_redirect='/account/',
             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)
     errors = []
     if request.method == 'POST':
         if merge:
-            logging.debug('Using mergeloginform, email is %' % request.GET.get('email'))
             form = MergeLoginForm(request.POST, request=request)
         else:
             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'])
@@ -90,10 +93,8 @@ def login(request, template_name='cas/login.html', success_redirect='/account/',
                     errors.append('Incorrect username and/or password.')
     else:
         if merge:
-            logging.debug('Using mergeloginform, email is %' % request.GET.get('email'))
             form = MergeLoginForm(initial={'service': service, 'email': request.GET.get('email')})
         else:
-            logging.debug('Using normal login form.')
             form = LoginForm(initial={'service': service})
     logging.debug('Rendering response on %s, merge is %s', template_name, merge)
     return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
@@ -129,14 +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)
+    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)
+        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()
-            return HttpResponse("yes\n%s\n" % username)
-        except:
-            pass
+
+            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))
+
+    logger.info('Validation failed.')
     return HttpResponse("no\n\n")