From 7e2917e9779002d8cd9680e095e00d0982642af8 Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Thu, 28 May 2015 14:02:17 +0200 Subject: [PATCH] add a single_log_out parametters on Ticket for query optimisation --- .../migrations/0015_auto_20150528_1202.py | 32 +++++++++++++++++++ cas_server/models.py | 28 ++++++++-------- cas_server/views.py | 3 +- 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 cas_server/migrations/0015_auto_20150528_1202.py diff --git a/cas_server/migrations/0015_auto_20150528_1202.py b/cas_server/migrations/0015_auto_20150528_1202.py new file mode 100644 index 0000000..da29498 --- /dev/null +++ b/cas_server/migrations/0015_auto_20150528_1202.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('cas_server', '0014_auto_20150528_0012'), + ] + + operations = [ + migrations.AddField( + model_name='proxygrantingticket', + name='single_log_out', + field=models.BooleanField(default=False), + preserve_default=True, + ), + migrations.AddField( + model_name='proxyticket', + name='single_log_out', + field=models.BooleanField(default=False), + preserve_default=True, + ), + migrations.AddField( + model_name='serviceticket', + name='single_log_out', + field=models.BooleanField(default=False), + preserve_default=True, + ), + ] diff --git a/cas_server/models.py b/cas_server/models.py index 401ebd1..1b47d3c 100644 --- a/cas_server/models.py +++ b/cas_server/models.py @@ -17,10 +17,10 @@ from django.db import models from django.contrib import messages from picklefield.fields import PickledObjectField from django.utils.translation import ugettext_lazy as _ +from django.utils import timezone import re import os -import time import random import string @@ -69,15 +69,15 @@ class User(models.Model): """Sending SLO request to all services the user logged in""" async_list = [] session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10)) - for ticket in ServiceTicket.objects.filter(user=self, validate=True): - async_list.append(ticket.logout(request, session)) - ticket.delete() - for ticket in ProxyTicket.objects.filter(user=self, validate=True): - async_list.append(ticket.logout(request, session)) - ticket.delete() - for ticket in ProxyGrantingTicket.objects.filter(user=self, validate=True): - async_list.append(ticket.logout(request, session)) - ticket.delete() + ticket_classes = [ServiceTicket, ProxyTicket, ProxyGrantingTicket] + for ticket_class in ticket_classes: + for ticket in ticket_class.objects.filter( + user=self, + validate=True, + single_log_out=True + ): + async_list.append(ticket.logout(request, session)) + ticket.delete() for future in async_list: if future: try: @@ -112,7 +112,8 @@ class User(models.Model): attributs=service_attributs, service=service, renew=renew, - service_pattern=service_pattern + service_pattern=service_pattern, + single_log_out=service_pattern.single_log_out ) ticket.save() return ticket @@ -306,13 +307,14 @@ class Ticket(models.Model): service_pattern = models.ForeignKey(ServicePattern, related_name="%(class)s") creation = models.DateTimeField(auto_now_add=True) renew = models.BooleanField(default=False) + single_log_out = models.BooleanField(default=False) def __unicode__(self): return u"Ticket(%s, %s)" % (self.user, self.service) def logout(self, request, session): """Send a SLO request to the ticket service""" - if self.validate and self.service_pattern.single_log_out: + if self.validate and self.single_log_out: xml = """ @@ -320,7 +322,7 @@ class Ticket(models.Model): """ % \ { 'id' : os.urandom(20).encode("hex"), - 'datetime' : int(time.time()), + 'datetime' : timezone.now().isoformat(), 'ticket': self.value } headers = {'Content-Type': 'text/xml'} diff --git a/cas_server/views.py b/cas_server/views.py index 9dd9bc6..ef0570c 100644 --- a/cas_server/views.py +++ b/cas_server/views.py @@ -288,7 +288,8 @@ def ps_validate(request, ticket_type=None): pticket = models.ProxyGrantingTicket.objects.create( user=ticket.user, service=pgt_url, - service_pattern=pattern + service_pattern=pattern, + single_log_out=pattern.single_log_out ) url = utils.update_url(pgt_url, {'pgtIou':proxyid, 'pgtId':pticket.value}) try: