Added UK translations +fixture, updated widget and created widget page.
[wolnelektury.git] / apps / piston / store.py
1 import oauth
2
3 from models import Nonce, Token, Consumer
4 from models import generate_random, VERIFIER_SIZE
5
6 class DataStore(oauth.OAuthDataStore):
7     """Layer between Python OAuth and Django database."""
8     def __init__(self, oauth_request):
9         self.signature = oauth_request.parameters.get('oauth_signature', None)
10         self.timestamp = oauth_request.parameters.get('oauth_timestamp', None)
11         self.scope = oauth_request.parameters.get('scope', 'all')
12
13     def lookup_consumer(self, key):
14         try:
15             self.consumer = Consumer.objects.get(key=key)
16             return self.consumer
17         except Consumer.DoesNotExist:
18             return None
19
20     def lookup_token(self, token_type, token):
21         if token_type == 'request':
22             token_type = Token.REQUEST
23         elif token_type == 'access':
24             token_type = Token.ACCESS
25         try:
26             self.request_token = Token.objects.get(key=token,
27                                                    token_type=token_type)
28             return self.request_token
29         except Token.DoesNotExist:
30             return None
31
32     def lookup_nonce(self, oauth_consumer, oauth_token, nonce):
33         if oauth_token is None:
34             return None
35         nonce, created = Nonce.objects.get_or_create(consumer_key=oauth_consumer.key,
36                                                      token_key=oauth_token.key,
37                                                      key=nonce)
38         if created:
39             return None
40         else:
41             return nonce.key
42
43     def fetch_request_token(self, oauth_consumer, oauth_callback):
44         if oauth_consumer.key == self.consumer.key:
45             self.request_token = Token.objects.create_token(consumer=self.consumer,
46                                                             token_type=Token.REQUEST,
47                                                             timestamp=self.timestamp)
48
49             if oauth_callback:
50                 self.request_token.set_callback(oauth_callback)
51
52             return self.request_token
53         return None
54
55     def fetch_access_token(self, oauth_consumer, oauth_token, oauth_verifier):
56         if oauth_consumer.key == self.consumer.key \
57         and oauth_token.key == self.request_token.key \
58         and oauth_verifier == self.request_token.verifier \
59         and self.request_token.is_approved:
60             self.access_token = Token.objects.create_token(consumer=self.consumer,
61                                                            token_type=Token.ACCESS,
62                                                            timestamp=self.timestamp,
63                                                            user=self.request_token.user)
64             return self.access_token
65         return None
66
67     def authorize_request_token(self, oauth_token, user):
68         if oauth_token.key == self.request_token.key:
69             # authorize the request token in the store
70             self.request_token.is_approved = True
71             self.request_token.user = user
72             self.request_token.verifier = generate_random(VERIFIER_SIZE)
73             self.request_token.save()
74             return self.request_token
75         return None