Mercurial > public > sg101
comparison gcalendar/models.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/gcalendar/models.py@368d731af479 |
children | 3e1905e523be |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Models for the gcalendar application. | |
3 | |
4 """ | |
5 import datetime | |
6 | |
7 from django.db import models | |
8 from django.db.models import Q | |
9 from django.contrib.auth.models import User | |
10 | |
11 from core.markup import site_markup | |
12 import forums.tools | |
13 from gcalendar.oauth import serialize_token, deserialize_token | |
14 | |
15 | |
16 GIG_FORUM_SLUG = "gigs" | |
17 | |
18 class PendingEventManager(models.Manager): | |
19 """A manager for pending events.""" | |
20 | |
21 def get_query_set(self): | |
22 """Returns a queryset of events that have been approved to update | |
23 the Google calendar.""" | |
24 return super(PendingEventManager, self).get_query_set().filter( | |
25 Q(status=Event.NEW_APRV) | | |
26 Q(status=Event.EDIT_APRV) | | |
27 Q(status=Event.DEL_APRV) | |
28 ) | |
29 | |
30 | |
31 class Event(models.Model): | |
32 """Model to represent calendar events.""" | |
33 | |
34 # Event status codes: | |
35 (NEW, NEW_APRV, EDIT_REQ, EDIT_APRV, DEL_REQ, DEL_APRV, ON_CAL) = range(7) | |
36 | |
37 STATUS_CHOICES = ( | |
38 (NEW, 'New'), | |
39 (NEW_APRV, 'New Approved'), | |
40 (EDIT_REQ, 'Edit Request'), | |
41 (EDIT_APRV, 'Edit Approved'), | |
42 (DEL_REQ, 'Delete Request'), | |
43 (DEL_APRV, 'Delete Approved'), | |
44 (ON_CAL, 'On Calendar'), | |
45 ) | |
46 | |
47 user = models.ForeignKey(User) | |
48 what = models.CharField(max_length=255) | |
49 start_date = models.DateField() | |
50 start_time = models.TimeField(null=True, blank=True) | |
51 end_date = models.DateField() | |
52 end_time = models.TimeField(null=True, blank=True) | |
53 time_zone = models.CharField(max_length=64, blank=True) | |
54 all_day = models.BooleanField(default=False) | |
55 where = models.CharField(max_length=255, blank=True) | |
56 description = models.TextField(blank=True) | |
57 html = models.TextField(blank=True) | |
58 date_submitted = models.DateTimeField(auto_now_add=True) | |
59 google_id = models.CharField(max_length=255, blank=True) | |
60 google_url = models.URLField(max_length=255, blank=True) | |
61 status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW, | |
62 db_index=True) | |
63 create_forum_thread = models.BooleanField(default=False) | |
64 | |
65 objects = models.Manager() | |
66 pending_events = PendingEventManager() | |
67 | |
68 def __unicode__(self): | |
69 return self.what | |
70 | |
71 class Meta: | |
72 ordering = ('-date_submitted', ) | |
73 | |
74 def save(self, *args, **kwargs): | |
75 self.html = site_markup(self.description) | |
76 super(Event, self).save(*args, **kwargs) | |
77 | |
78 def is_approved(self): | |
79 return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ) | |
80 is_approved.boolean = True | |
81 | |
82 def google_html(self): | |
83 """Returns a HTML <a> tag to the event if it exits.""" | |
84 if self.google_url: | |
85 return u'<a href="%s">On Google</a>' % self.google_url | |
86 return u'' | |
87 google_html.allow_tags = True | |
88 google_html.short_description = 'Google Link' | |
89 | |
90 def notify_on_calendar(self): | |
91 """ | |
92 This function should be called when the event has been added to the | |
93 Google calendar for the first time. This gives us a chance to perform | |
94 any first-time processing, like creating a forum thread. | |
95 """ | |
96 if self.create_forum_thread: | |
97 topic_name = '%s: %s' % (self.start_date.strftime('%m/%d/%Y'), | |
98 self.what) | |
99 post_body = "%s\n\n[Link to event on Google Calendar](%s)" % ( | |
100 self.description, self.google_url) | |
101 | |
102 forums.tools.create_topic( | |
103 forum_slug=GIG_FORUM_SLUG, | |
104 user=self.user, | |
105 topic_name=topic_name, | |
106 post_body=post_body) | |
107 | |
108 self.create_forum_thread = False | |
109 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 |