/usr/lib/python3/dist-packages/piston3/managers.py is in python3-django-piston3 0.3~rc2-3ubuntu5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | from django.db import models
from django.contrib.auth.models import User
KEY_SIZE = 18
SECRET_SIZE = 32
class KeyManager(models.Manager):
'''Add support for random key/secret generation
'''
def generate_random_codes(self):
key = User.objects.make_random_password(length=KEY_SIZE)
secret = User.objects.make_random_password(length=SECRET_SIZE)
while self.filter(key__exact=key, secret__exact=secret).count():
secret = User.objects.make_random_password(length=SECRET_SIZE)
return key, secret
class ConsumerManager(KeyManager):
def create_consumer(self, name, description=None, user=None):
"""
Shortcut to create a consumer with random key/secret.
"""
consumer, created = self.get_or_create(name=name)
if user:
consumer.user = user
if description:
consumer.description = description
if created:
consumer.key, consumer.secret = self.generate_random_codes()
consumer.save()
return consumer
_default_consumer = None
class ResourceManager(models.Manager):
_default_resource = None
def get_default_resource(self, name):
"""
Add cache if you use a default resource.
"""
if not self._default_resource:
self._default_resource = self.get(name=name)
return self._default_resource
class TokenManager(KeyManager):
def create_token(self, consumer, token_type, timestamp, user=None):
"""
Shortcut to create a token with random key/secret.
"""
token, created = self.get_or_create(consumer=consumer,
token_type=token_type,
timestamp=timestamp,
user=user)
if created:
token.key, token.secret = self.generate_random_codes()
token.save()
return token
|