Package madrona :: Package openid :: Module models
[hide private]

Source Code for Module madrona.openid.models

 1  # -*- coding: utf-8 -*- 
 2  # Copyright 2007, 2008,2009 by Benoît Chesneau <benoitc@e-engura.org> 
 3  #  
 4  # Licensed under the Apache License, Version 2.0 (the "License"); 
 5  # you may not use this file except in compliance with the License. 
 6  # You may obtain a copy of the License at 
 7  # 
 8  #     http://www.apache.org/licenses/LICENSE-2.0 
 9  # 
10  # Unless required by applicable law or agreed to in writing, software 
11  # distributed under the License is distributed on an "AS IS" BASIS, 
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13  # See the License for the specific language governing permissions and 
14  # limitations under the License. 
15  # 
16   
17  import os 
18  import random 
19  import sys 
20  import time 
21  try: 
22      from hashlib import md5 as _md5 
23  except ImportError: 
24      import md5 
25      _md5 = md5.new 
26   
27  from django.conf import settings 
28  from django.template.loader import render_to_string 
29  from django.contrib.auth.models import User 
30  from django.contrib.sites.models import Site 
31  from django.db import models 
32  from django.utils.translation import ugettext_lazy as _ 
33   
34  from madrona.openid.signals import oid_associate 
35   
36  __all__ = ['Nonce', 'Association', 'UserAssociation'] 
37   
38 -class Nonce(models.Model):
39 """ openid nonce """ 40 server_url = models.CharField(max_length=255) 41 timestamp = models.IntegerField() 42 salt = models.CharField(max_length=40) 43
44 - def __unicode__(self):
45 return u"Nonce: %s" % self.id
46
47 -class Association(models.Model):
48 """ association openid url and lifetime """ 49 server_url = models.TextField(max_length=2047) 50 handle = models.CharField(max_length=255) 51 secret = models.TextField(max_length=255) # Stored base64 encoded 52 issued = models.IntegerField() 53 lifetime = models.IntegerField() 54 assoc_type = models.TextField(max_length=64) 55
56 - def __unicode__(self):
57 return u"Association: %s, %s" % (self.server_url, self.handle)
58
59 -class UserAssociation(models.Model):
60 """ 61 model to manage association between openid and user 62 """ 63 openid_url = models.CharField(primary_key=True, blank=False, 64 max_length=255, verbose_name=_('OpenID URL')) 65 user = models.ForeignKey(User, verbose_name=_('User')) 66
67 - def __unicode__(self):
68 return "Openid %s with user %s" % (self.openid_url, self.user)
69
70 - def save(self, send_email=True):
71 super(UserAssociation, self).save() 72 if send_email: 73 from django.core.mail import send_mail 74 current_site = Site.objects.get_current() 75 subject = render_to_string('authopenid/associate_email_subject.txt', 76 {'site': current_site, 77 'user': self.user}) 78 message = render_to_string('authopenid/associate_email.txt', 79 {'site': current_site, 80 'user': self.user, 81 'openid': self.openid_url 82 }) 83 84 send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, 85 [self.user.email], fail_silently=True) 86 oid_associate.send(sender=self, user=self.user, openid=self.openid_url)
87
88 - class Meta:
89 verbose_name = _('user association') 90 verbose_name_plural = _('user associations')
91