annotate gcalendar/models.py @ 999:8386a8ebcbc7

WIP News v2.0 admin changes.
author Brian Neal <bgneal@gmail.com>
date Sat, 21 Nov 2015 14:53:29 -0600
parents 9165edfb1709
children 68c3343f3318
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@857 72 self.html = site_markup(self.description, relative_urls=False)
gremmie@1 73 super(Event, self).save(*args, **kwargs)
gremmie@1 74
bgneal@139 75 def is_approved(self):
bgneal@139 76 return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ)
bgneal@139 77 is_approved.boolean = True
gremmie@1 78
bgneal@228 79 def google_html(self):
bgneal@228 80 """Returns a HTML <a> tag to the event if it exits."""
bgneal@228 81 if self.google_url:
bgneal@228 82 return u'<a href="%s">On Google</a>' % self.google_url
bgneal@228 83 return u''
bgneal@228 84 google_html.allow_tags = True
bgneal@228 85 google_html.short_description = 'Google Link'
bgneal@228 86
bgneal@228 87 def notify_on_calendar(self):
bgneal@228 88 """
bgneal@228 89 This function should be called when the event has been added to the
bgneal@228 90 Google calendar for the first time. This gives us a chance to perform
bgneal@228 91 any first-time processing, like creating a forum thread.
bgneal@228 92 """
bgneal@228 93 if self.create_forum_thread:
bgneal@228 94 topic_name = '%s: %s' % (self.start_date.strftime('%m/%d/%Y'),
bgneal@228 95 self.what)
bgneal@228 96 post_body = "%s\n\n[Link to event on Google Calendar](%s)" % (
bgneal@228 97 self.description, self.google_url)
bgneal@228 98
bgneal@228 99 forums.tools.create_topic(
bgneal@228 100 forum_slug=GIG_FORUM_SLUG,
bgneal@456 101 user=self.user,
bgneal@228 102 topic_name=topic_name,
bgneal@228 103 post_body=post_body)
bgneal@228 104
bgneal@228 105 self.create_forum_thread = False
bgneal@228 106 self.save()