annotate gcalendar/models.py @ 1155:0733a0450afe

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