# Path: webmachine/util/const.py
# KEY_SIZE = 32
# 
# SECRET_SIZE = 32 
# 
# VERIFIER_SIZE = 16
# 
# TOKEN_TYPES = (
#     (TOKEN_ACCESS, _("Access")),
#     (TOKEN_REQUEST, _("Request"))
# )
# 
# PENDING = 1
# 
# CONSUMER_STATES = (
#     (PENDING,  _('Pending')),
#     (ACCEPTED, _('Accepted')),
#     (CANCELED, _('Canceled')),
#     (REJECTED, _('Rejected')),
# )
# 
# Path: webmachine/managers.py
# class ConsumerManager(KeyManager):
# 
#     def create_consumer(self, name=None, description=None, user=None):
#         key, secret = self.generate_key_secret()
#         consumer = self.create(
#                 key=key, 
#                 secret=secret,
#                 name=name or '',
#                 description=description or '', 
#                 user=user
#         )
#         return consumer
# 
# class TokenManager(KeyManager):
# 
#     def create_token(self, consumer, token_type, timestamp, user=None):
#         key, secret = self.generate_key_secret()
#         kwargs = dict(
#             key=key, 
#             secret=secret, 
#             token_type=token_type,
#             consumer=consumer,
#             timestamp=timestamp,
#             user=user
#         )
#         if not user:
#             del kwargs["user"]
#         token = self.create(**kwargs)
#         return token


import time
import urllib
import urlparse
from django.contrib.auth.models import User
from django.db import models
from webmachine.util.const import KEY_SIZE, SECRET_SIZE, VERIFIER_SIZE, \
TOKEN_TYPES, PENDING, CONSUMER_STATES
from webmachine.managers import ConsumerManager, TokenManager
        data = {'oauth_consumer_key': self.key,
            'oauth_consumer_secret': self.secret}

        return urllib.urlencode(data)

class Token(models.Model):
    key = models.CharField(max_length=KEY_SIZE)
    secret = models.CharField(max_length=SECRET_SIZE)
    token_type = models.SmallIntegerField(choices=TOKEN_TYPES)
    callback = models.CharField(max_length=2048) #URL
    callback_confirmed = models.BooleanField(default=False)
    verifier = models.CharField(max_length=VERIFIER_SIZE)
    consumer = models.ForeignKey(Consumer,
            related_name="tokens_consumer")
    timestamp = models.IntegerField(default=time.time())
    user = models.ForeignKey(User, null=True, blank=True, 
            related_name="tokens_user")
    is_approved = models.BooleanField(default=False)
    
    objects = TokenManager()

    def set_callback(self, callback):
        self.callback = callback
        self.callback_confirmed = True
        self.save()

    def set_verifier(self, verifier=None):
        if verifier is not None:
            self.verifier = verifier
        else:

# Path: webmachine/util/const.py
# KEY_SIZE = 32
# 
# SECRET_SIZE = 32 
# 
# VERIFIER_SIZE = 16
# 
# TOKEN_TYPES = (
#     (TOKEN_ACCESS, _("Access")),
#     (TOKEN_REQUEST, _("Request"))
# )
# 
# PENDING = 1
# 
# CONSUMER_STATES = (
#     (PENDING,  _('Pending')),
#     (ACCEPTED, _('Accepted')),
#     (CANCELED, _('Canceled')),
#     (REJECTED, _('Rejected')),
# )
# 
# Path: webmachine/managers.py
# class ConsumerManager(KeyManager):
# 
#     def create_consumer(self, name=None, description=None, user=None):
#         key, secret = self.generate_key_secret()
#         consumer = self.create(
#                 key=key, 
#                 secret=secret,
#                 name=name or '',
#                 description=description or '', 
#                 user=user
#         )
#         return consumer
# 
# class TokenManager(KeyManager):
# 
#     def create_token(self, consumer, token_type, timestamp, user=None):
#         key, secret = self.generate_key_secret()
#         kwargs = dict(
#             key=key, 
#             secret=secret, 
#             token_type=token_type,
#             consumer=consumer,
#             timestamp=timestamp,
#             user=user
#         )
#         if not user:
#             del kwargs["user"]
#         token = self.create(**kwargs)
#         return token


# Path: webmachine/models.py
import time
import urllib
import urlparse
from django.contrib.auth.models import User
from django.db import models
from webmachine.util.const import KEY_SIZE, SECRET_SIZE, VERIFIER_SIZE, \
TOKEN_TYPES, PENDING, CONSUMER_STATES
from webmachine.managers import ConsumerManager, TokenManager

        data = {'oauth_consumer_key': self.key,
            'oauth_consumer_secret': self.secret}

        return urllib.urlencode(data)

class Token(models.Model):
    key = models.CharField(max_length=KEY_SIZE)
    secret = models.CharField(max_length=SECRET_SIZE)
    token_type = models.SmallIntegerField(choices=TOKEN_TYPES)
    callback = models.CharField(max_length=2048) #URL
    callback_confirmed = models.BooleanField(default=False)
    verifier = models.CharField(max_length=VERIFIER_SIZE)
    consumer = models.ForeignKey(Consumer,
            related_name="tokens_consumer")
    timestamp = models.IntegerField(default=time.time())
    user = models.ForeignKey(User, null=True, blank=True, 
            related_name="tokens_user")
    is_approved = models.BooleanField(default=False)
    
    objects = TokenManager()

    def set_callback(self, callback):
        self.callback = callback
        self.callback_confirmed = True
        self.save()

    def set_verifier(self, verifier=None):
        if verifier is not None:
            self.verifier = verifier
        else:

            self.verifier = generate_random(VERIFIER_SIZE)