comparison gpp/gcalendar/models.py @ 458:9a4bffdf37c3

Finishing up #220. Updated to GData v2.0 and using the new OAuth access token.
author Brian Neal <bgneal@gmail.com>
date Sat, 02 Jul 2011 03:52:43 +0000
parents 5be072292e2d
children 368d731af479
comparison
equal deleted inserted replaced
457:7b7332037396 458:9a4bffdf37c3
1 """ 1 """
2 Models for the gcalendar application. 2 Models for the gcalendar application.
3 3
4 """ 4 """
5 import datetime
6
5 from django.db import models 7 from django.db import models
6 from django.db.models import Q 8 from django.db.models import Q
7 from django.contrib.auth.models import User 9 from django.contrib.auth.models import User
8 10
9 from core.markup import site_markup 11 from core.markup import site_markup
10 import forums.tools 12 import forums.tools
13 from gcalendar.oauth import serialize_token, deserialize_token
11 14
12 15
13 GIG_FORUM_SLUG = "gigs" 16 GIG_FORUM_SLUG = "gigs"
14 17
15 class PendingEventManager(models.Manager): 18 class PendingEventManager(models.Manager):
105 108
106 self.create_forum_thread = False 109 self.create_forum_thread = False
107 self.save() 110 self.save()
108 111
109 112
113 class AccessTokenManager(models.Manager):
114 """
115 A manager for the AccessToken table. Only one access token is saved in the
116 database. This manager provides a convenience method to either return that
117 access token or a brand new one.
118
119 """
120 def get_token(self):
121 try:
122 token = self.get(pk=1)
123 except AccessToken.DoesNotExist:
124 token = AccessToken()
125
126 return token
127
128
110 class AccessToken(models.Model): 129 class AccessToken(models.Model):
111 """ 130 """
112 This model represents serialized OAuth access tokens for reading and 131 This model represents serialized OAuth access tokens for reading and
113 updating the Google Calendar. 132 updating the Google Calendar.
114 133
115 """ 134 """
116 auth_date = models.DateTimeField() 135 auth_date = models.DateTimeField()
117 token = models.TextField() 136 token = models.TextField()
118 137
138 objects = AccessTokenManager()
139
119 def __unicode__(self): 140 def __unicode__(self):
120 return u'Access token created on ' + unicode(self.auth_date) 141 return u'Access token created on ' + unicode(self.auth_date)
121 142
143 def update(self, access_token, auth_date=None):
144 """
145 This function updates the AccessToken object with the input parameters:
146 access_token - an access token from Google's OAuth dance
147 auth_date - a datetime or None. If None, now() is used.
148
149 """
150 self.auth_date = auth_date if auth_date else datetime.datetime.now()
151 self.token = serialize_token(access_token)
152
153 def access_token(self):
154 """
155 This function returns a Google OAuth access token by deserializing the
156 token field from the database.
157 If the token attribute is empty, None is returned.
158
159 """
160 return deserialize_token(self.token) if self.token else None