Source

django-piston-oauth10a / piston / models.py

jst...@joseph-st… 02814fb 
jst...@joseph-st… 40e6a7f 


Jesper Nøhr 76c0ea5 

Jesper Nøhr 1b484de 
Jesper Nøhr 788bbd3 
Jesper Nøhr 76c0ea5 
jst...@joseph-st… 40e6a7f 
Jesper Nøhr 1c13041 
jst...@joseph-st… 40e6a7f 
Jesper Nøhr 1c13041 


Jesper Nøhr 788bbd3 

jst...@joseph-st… 40e6a7f 
Jesper Nøhr 788bbd3 

jst...@joseph-st… 40e6a7f 
Jesper Nøhr 788bbd3 
Jesper Nøhr 76c0ea5 








Jesper Nøhr 1b484de 
Jesper Nøhr 76c0ea5 


Jesper Nøhr 788bbd3 

Jesper Nøhr 76c0ea5 

Jesper Nøhr 788bbd3 


Jesper Nøhr 76c0ea5 





Jesper Nøhr 1c13041 
jst...@joseph-st… 40e6a7f 


Jesper Nøhr 788bbd3 
jst...@joseph-st… 40e6a7f 





Jesper Nøhr 1c13041 
Jesper Nøhr 788bbd3 
Jesper Nøhr 1c13041 







Jesper Nøhr 788bbd3 
Jesper Nøhr 1b484de 
Jesper Nøhr 76c0ea5 








jst...@joseph-st… 02814fb 
Jesper Nøhr 76c0ea5 

Jesper Nøhr 1b484de 
Jesper Nøhr 76c0ea5 















Jesper Nøhr 1c13041 










James Emerton 8a96532 
jst...@joseph-st… 40e6a7f 


import urllib, time

# Django imports
from django.db.models.signals import post_save, post_delete
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from django.core.mail import send_mail, mail_admins

# Piston imports
from managers import TokenManager, ConsumerManager, ResourceManager
from signals import consumer_post_save, consumer_post_delete

KEY_SIZE = 18
SECRET_SIZE = 32

CONSUMER_STATES = (
    ('pending', 'Pending'),
    ('accepted', 'Accepted'),
    ('canceled', 'Canceled'),
    ('rejected', 'Rejected')
)

class Nonce(models.Model):
    token_key = models.CharField(max_length=KEY_SIZE)
    consumer_key = models.CharField(max_length=KEY_SIZE)
    key = models.CharField(max_length=255)
    
    def __unicode__(self):
        return u"Nonce %s for %s" % (self.key, self.consumer_key)

admin.site.register(Nonce)

class Consumer(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()

    key = models.CharField(max_length=KEY_SIZE)
    secret = models.CharField(max_length=SECRET_SIZE)

    status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending')
    user = models.ForeignKey(User, null=True, blank=True, related_name='consumers')

    objects = ConsumerManager()
        
    def __unicode__(self):
        return u"Consumer %s with key %s" % (self.name, self.key)

    def generate_random_codes(self):
        """
        Used to generate random key/secret pairings. Use this after you've
        added the other data in place of save(). 

        c = Consumer()
        c.name = "My consumer" 
        c.description = "An app that makes ponies from the API."
        c.user = some_user_object
        c.generate_random_codes()
        """
        key = User.objects.make_random_password(length=KEY_SIZE)

        secret = User.objects.make_random_password(length=SECRET_SIZE)

        while Consumer.objects.filter(key__exact=key, secret__exact=secret).count():
            secret = User.objects.make_random_password(length=SECRET_SIZE)

        self.key = key
        self.secret = secret
        self.save()

admin.site.register(Consumer)

class Token(models.Model):
    REQUEST = 1
    ACCESS = 2
    TOKEN_TYPES = ((REQUEST, u'Request'), (ACCESS, u'Access'))
    
    key = models.CharField(max_length=KEY_SIZE)
    secret = models.CharField(max_length=SECRET_SIZE)
    token_type = models.IntegerField(choices=TOKEN_TYPES)
    timestamp = models.IntegerField(default=long(time.time()))
    is_approved = models.BooleanField(default=False)
    
    user = models.ForeignKey(User, null=True, blank=True, related_name='tokens')
    consumer = models.ForeignKey(Consumer)
    
    objects = TokenManager()
    
    def __unicode__(self):
        return u"%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)

    def to_string(self, only_key=False):
        token_dict = {
            'oauth_token': self.key, 
            'oauth_token_secret': self.secret
        }
        if only_key:
            del token_dict['oauth_token_secret']
        return urllib.urlencode(token_dict)

    def generate_random_codes(self):
        key = User.objects.make_random_password(length=KEY_SIZE)
        secret = User.objects.make_random_password(length=SECRET_SIZE)

        while Token.objects.filter(key__exact=key, secret__exact=secret).count():
            secret = User.objects.make_random_password(length=SECRET_SIZE)

        self.key = key
        self.secret = secret
        self.save()
        
admin.site.register(Token)

# Attach our signals
post_save.connect(consumer_post_save, sender=Consumer)
post_delete.connect(consumer_post_delete, sender=Consumer)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.