1 from django.db import models
2 from django.contrib.auth.models import User
7 class KeyManager(models.Manager):
8 '''Add support for random key/secret generation
10 def generate_random_codes(self):
11 key = User.objects.make_random_password(length=KEY_SIZE)
12 secret = User.objects.make_random_password(length=SECRET_SIZE)
14 while self.filter(key__exact=key, secret__exact=secret).count():
15 secret = User.objects.make_random_password(length=SECRET_SIZE)
20 class ConsumerManager(KeyManager):
21 def create_consumer(self, name, description=None, user=None):
23 Shortcut to create a consumer with random key/secret.
25 consumer, created = self.get_or_create(name=name)
31 consumer.description = description
34 consumer.key, consumer.secret = self.generate_random_codes()
39 _default_consumer = None
41 class ResourceManager(models.Manager):
42 _default_resource = None
44 def get_default_resource(self, name):
46 Add cache if you use a default resource.
48 if not self._default_resource:
49 self._default_resource = self.get(name=name)
51 return self._default_resource
53 class TokenManager(KeyManager):
54 def create_token(self, consumer, token_type, timestamp, user=None):
56 Shortcut to create a token with random key/secret.
58 token, created = self.get_or_create(consumer=consumer,
59 token_type=token_type,
64 token.key, token.secret = self.generate_random_codes()