1 from django.contrib.auth.models import User
2 from django.db import models
3 from django.utils.translation import ugettext_lazy as _
4 from random import Random
9 if hasattr(urlparse, 'parse_qs'):
10 parse_qs = urlparse.parse_qs
12 # Python <2.6 compatibility
13 from cgi import parse_qs
16 __all__ = ['ServiceTicket', 'LoginTicket']
19 class BaseTicket(models.Model):
20 ticket = models.CharField(_('ticket'), max_length=32)
21 created = models.DateTimeField(_('created'), auto_now=True)
26 def __init__(self, *args, **kwargs):
27 if 'ticket' not in kwargs:
28 kwargs['ticket'] = self._generate_ticket()
29 super(BaseTicket, self).__init__(*args, **kwargs)
31 def __unicode__(self):
34 def _generate_ticket(self, length=29, chars=string.ascii_letters + string.digits):
35 """ Generates a random string of the requested length. Used for creation of tickets. """
36 return u"%s-%s" % (self.prefix, ''.join(Random().sample(chars, length)))
39 class ServiceTicket(BaseTicket):
40 user = models.ForeignKey(User, verbose_name=_('user'))
41 service = models.URLField(_('service'), verify_exists=False)
46 verbose_name = _('Service Ticket')
47 verbose_name_plural = _('Service Tickets')
49 def get_redirect_url(self):
50 parsed = urlparse.urlparse(self.service)
51 query = parse_qs(parsed.query)
52 query['ticket'] = [self.ticket]
53 query = [ ((k, v) if len(v) > 1 else (k, v[0])) for k, v in query.iteritems()]
54 parsed = urlparse.ParseResult(parsed.scheme, parsed.netloc,
55 parsed.path, parsed.params,
56 urllib.urlencode(query), parsed.fragment)
57 return parsed.geturl()
60 class LoginTicket(BaseTicket):
65 verbose_name = _('Login Ticket')
66 verbose_name_plural = _('Login Tickets')