comparison gcalendar/models.py @ 857:9165edfb1709

For issue #80, use new Google Calendar v3 API.
author Brian Neal <bgneal@gmail.com>
date Sat, 22 Nov 2014 13:27:13 -0600
parents 3e1905e523be
children 68c3343f3318
comparison
equal deleted inserted replaced
856:c2dfd1b1323e 857:9165edfb1709
1 """ 1 """
2 Models for the gcalendar application. 2 Models for the gcalendar application.
3 3
4 """ 4 """
5 import datetime
6
7 from django.db import models 5 from django.db import models
8 from django.db.models import Q 6 from django.db.models import Q
9 from django.contrib.auth.models import User 7 from django.contrib.auth.models import User
10 8
11 from core.markup import site_markup 9 from core.markup import site_markup
12 import forums.tools 10 import forums.tools
13 from gcalendar.oauth import serialize_token, deserialize_token
14 11
15 12
16 GIG_FORUM_SLUG = "gigs" 13 GIG_FORUM_SLUG = "gigs"
17 14
18 class PendingEventManager(models.Manager): 15 class PendingEventManager(models.Manager):
70 67
71 class Meta: 68 class Meta:
72 ordering = ('-date_submitted', ) 69 ordering = ('-date_submitted', )
73 70
74 def save(self, *args, **kwargs): 71 def save(self, *args, **kwargs):
75 self.html = site_markup(self.description) 72 self.html = site_markup(self.description, relative_urls=False)
76 super(Event, self).save(*args, **kwargs) 73 super(Event, self).save(*args, **kwargs)
77 74
78 def is_approved(self): 75 def is_approved(self):
79 return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ) 76 return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ)
80 is_approved.boolean = True 77 is_approved.boolean = True
105 topic_name=topic_name, 102 topic_name=topic_name,
106 post_body=post_body) 103 post_body=post_body)
107 104
108 self.create_forum_thread = False 105 self.create_forum_thread = False
109 self.save() 106 self.save()
110
111
112 class AccessTokenManager(models.Manager):
113 """
114 A manager for the AccessToken table. Only one access token is saved in the
115 database. This manager provides a convenience method to either return that
116 access token or a brand new one.
117
118 """
119 def get_token(self):
120 try:
121 token = self.get(pk=1)
122 except AccessToken.DoesNotExist:
123 token = AccessToken()
124
125 return token
126
127
128 class AccessToken(models.Model):
129 """
130 This model represents serialized OAuth access tokens for reading and
131 updating the Google Calendar.
132
133 """
134 auth_date = models.DateTimeField()
135 token = models.TextField()
136
137 objects = AccessTokenManager()
138
139 def __unicode__(self):
140 return u'Access token created on ' + unicode(self.auth_date)
141
142 def update(self, access_token, auth_date=None):
143 """
144 This function updates the AccessToken object with the input parameters:
145 access_token - an access token from Google's OAuth dance
146 auth_date - a datetime or None. If None, now() is used.
147
148 """
149 self.auth_date = auth_date if auth_date else datetime.datetime.now()
150 self.token = serialize_token(access_token)
151
152 def access_token(self):
153 """
154 This function returns a Google OAuth access token by deserializing the
155 token field from the database.
156 If the token attribute is empty, None is returned.
157
158 """
159 return deserialize_token(self.token) if self.token else None