changeset 228:d77e0dc772ad

Implement #88; add option to create a new forum thread from a new calendar entry.
author Brian Neal <bgneal@gmail.com>
date Sun, 11 Jul 2010 21:03:41 +0000
parents 423c39ee44e0
children 08b30ac04580
files gpp/forums/tools.py gpp/forums/unread.py gpp/gcalendar/admin.py gpp/gcalendar/calendar.py gpp/gcalendar/forms.py gpp/gcalendar/models.py gpp/templates/gcalendar/event.html
diffstat 7 files changed, 86 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/tools.py	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/forums/tools.py	Sun Jul 11 21:03:41 2010 +0000
@@ -1,6 +1,8 @@
 """
 This module contains misc. utility functions for forum management.
 """
+import logging
+
 from forums.models import Post, Topic, Forum, ForumLastVisit, TopicLastVisit
 
 
@@ -67,3 +69,36 @@
     TopicLastVisit.objects.filter(user=user).delete()
     ForumLastVisit.objects.filter(user=user).delete()
 
+
+def create_topic(forum_slug, user, topic_name, post_body, ip='', sticky=False,
+        locked=False):
+    """Programmatically create a topic & first post in a given forum.
+
+    This function creates a new topic in the forum that has the slug
+    specified by the 'forum_slug' argument. Other arguments are as follows:
+    'user' - create the topic and post with this user as the owner
+    'topic_name' - topic name (title)
+    'post_body' - topic post body (as markup, not HTML)
+    'ip' - IP address for the post (as a string)
+    'sticky' - if True, the post will be stickied
+    'locked' - if True, the post will be locked
+
+    """
+    try:
+        forum = Forum.objects.get(slug=forum_slug)
+    except Forum.DoesNotExist:
+        logging.error('could not create_topic for forum_slug=%s' % forum_slug)
+        raise
+
+    topic = Topic(forum=forum,
+            name=topic_name,
+            user=user,
+            sticky=sticky,
+            locked=locked)
+    topic.save()
+
+    post = Post(topic=topic,
+            user=user,
+            body=post_body,
+            user_ip=ip)
+    post.save()
--- a/gpp/forums/unread.py	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/forums/unread.py	Sun Jul 11 21:03:41 2010 +0000
@@ -193,7 +193,6 @@
 
 def get_unread_topics(user):
     """Returns a list of topics the user hasn't read yet."""
-    #import pdb; pdb.set_trace()
 
     # This is only available to authenticated users
     if not user.is_authenticated():
--- a/gpp/gcalendar/admin.py	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/gcalendar/admin.py	Sun Jul 11 21:03:41 2010 +0000
@@ -12,12 +12,12 @@
 
 class EventAdmin(admin.ModelAdmin):
     list_display = ('what', 'user', 'start_date', 'where', 'date_submitted',
-            'status', 'is_approved')
+            'status', 'is_approved', 'google_html')
     list_filter = ('start_date', 'status')
     date_hierarchy = 'start_date'
     search_fields = ('what', 'where', 'description')
     raw_id_fields = ('user', )
-    exclude = ('html', 'google_id')
+    exclude = ('html', 'google_id', 'google_url')
     save_on_top = True
     actions = ('approve_events', )
 
--- a/gpp/gcalendar/calendar.py	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/gcalendar/calendar.py	Sun Jul 11 21:03:41 2010 +0000
@@ -72,11 +72,14 @@
 
             error = False
             if qs[i].status == Event.NEW_APRV or qs[i].status == Event.EDIT_APRV:
-                if (code == 201 and qs[i].status == Event.NEW_APRV) or \
-                        (code == 200 and qs[i].status == Event.EDIT_APRV):
+                if (code == 201 and qs[i].status == Event.NEW_APRV) or (
+                       code == 200 and qs[i].status == Event.EDIT_APRV):
                     qs[i].google_id = entry.id.text
+                    qs[i].google_url = entry.GetHtmlLink().href
                     qs[i].status = Event.ON_CAL
                     qs[i].save()
+                    if code == 201:
+                        qs[i].notify_on_calendar()
                 else:
                     error = True
             elif qs[i].status == Event.DEL_APRV:
--- a/gpp/gcalendar/forms.py	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/gcalendar/forms.py	Sun Jul 11 21:03:41 2010 +0000
@@ -80,7 +80,7 @@
     class Meta:
         model = Event
         fields = ('what', 'start_date', 'start_time', 'end_date', 'end_time',
-            'all_day', 'time_zone', 'where', 'description')
+            'all_day', 'time_zone', 'where', 'description', 'create_forum_thread')
 
     class Media:
         css = {
@@ -123,6 +123,10 @@
 
         super(EventEntryForm, self).__init__(*args, **kwargs)
 
+        # We don't want the user to create a forum thread on an existing event
+        if instance is not None:
+            del self.fields['create_forum_thread']
+
     def clean(self):
         start_date = self.cleaned_data.get('start_date')
         start_time = self.cleaned_data.get('start_time')
--- a/gpp/gcalendar/models.py	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/gcalendar/models.py	Sun Jul 11 21:03:41 2010 +0000
@@ -6,8 +6,11 @@
 from django.contrib.auth.models import User
 
 from core.markup import site_markup
+import forums.tools
 
 
+GIG_FORUM_SLUG = "gigs"
+
 class PendingEventManager(models.Manager):
     """A manager for pending events."""
 
@@ -37,14 +40,6 @@
         (ON_CAL, 'On Calendar'),
     )
 
-    REPEAT_CHOICES = (
-        ('none', 'Does not repeat'),
-        ('daily', 'Daily'),
-        ('weekly', 'Weekly'),
-        ('monthly', 'Monthly'),
-        ('yearly', 'Yearly')
-    )
-
     user = models.ForeignKey(User)
     what = models.CharField(max_length=255)
     start_date = models.DateField()
@@ -53,33 +48,16 @@
     end_time = models.TimeField(null=True, blank=True)
     time_zone = models.CharField(max_length=64, blank=True)
     all_day = models.BooleanField(default=False)
-    repeat = models.CharField(max_length=7, choices=REPEAT_CHOICES)
-    repeat_interval = models.IntegerField(null=True, blank=True,
-        help_text='Only valid for repeating events.')
-    until_date = models.DateField(null=True, blank=True,
-        help_text='Only valid for repeating events; leave blank for no end date.')
-    weekly_sun = models.BooleanField(default=False, verbose_name='Weekly on Sun',
-        help_text='Only valid for weekly repeats.')
-    weekly_mon = models.BooleanField(default=False, verbose_name='Weekly on Mon',
-        help_text='Only valid for weekly repeats.')
-    weekly_tue = models.BooleanField(default=False, verbose_name='Weekly on Tue',
-        help_text='Only valid for weekly repeats.')
-    weekly_wed = models.BooleanField(default=False, verbose_name='Weekly on Wed',
-        help_text='Only valid for weekly repeats.')
-    weekly_thu = models.BooleanField(default=False, verbose_name='Weekly on Thu',
-        help_text='Only valid for weekly repeats.')
-    weekly_fri = models.BooleanField(default=False, verbose_name='Weekly on Fri',
-        help_text='Only valid for weekly repeats.')
-    weekly_sat = models.BooleanField(default=False, verbose_name='Weekly on Sat',
-        help_text='Only valid for weekly repeats.')
-    monthly_by_day = models.BooleanField(default=False,
-        help_text='Only valid for monthly repeats; Checked: By day of the month, Unchecked: By day of the week.')
     where = models.CharField(max_length=255, blank=True)
     description = models.TextField(blank=True)
     html = models.TextField(blank=True)
     date_submitted = models.DateTimeField(auto_now_add=True)
     google_id = models.CharField(max_length=255, blank=True)
-    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW, db_index=True)
+    google_url = models.URLField(verify_exists=False, max_length=255,
+            blank=True)
+    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW,
+            db_index=True)
+    create_forum_thread = models.BooleanField(default=False)
 
     objects = models.Manager()
     pending_events = PendingEventManager()
@@ -98,3 +76,31 @@
         return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ)
     is_approved.boolean = True
 
+    def google_html(self):
+        """Returns a HTML <a> tag to the event if it exits."""
+        if self.google_url:
+            return u'<a href="%s">On Google</a>' % self.google_url
+        return u''
+    google_html.allow_tags = True
+    google_html.short_description = 'Google Link'
+
+    def notify_on_calendar(self):
+        """
+        This function should be called when the event has been added to the
+        Google calendar for the first time. This gives us a chance to perform
+        any first-time processing, like creating a forum thread.
+        """
+        if self.create_forum_thread:
+            topic_name = '%s: %s' % (self.start_date.strftime('%m/%d/%Y'),
+                    self.what)
+            post_body = "%s\n\n[Link to event on Google Calendar](%s)" % (
+                    self.description, self.google_url)
+
+            forums.tools.create_topic(
+                forum_slug=GIG_FORUM_SLUG,
+                user=self.user, 
+                topic_name=topic_name,
+                post_body=post_body)
+
+            self.create_forum_thread = False
+            self.save()
--- a/gpp/templates/gcalendar/event.html	Tue Jul 06 03:02:20 2010 +0000
+++ b/gpp/templates/gcalendar/event.html	Sun Jul 11 21:03:41 2010 +0000
@@ -36,7 +36,9 @@
 </td></tr>
 <tr><th>Where:</th><td>{{ form.where.errors }}{{ form.where }}</td></tr>
 <tr><th>Details:</th><td>{{ form.description.errors }}{{ form.description }}</td></tr>
-{# form.as_p #}
+{% if form.create_forum_thread %}
+<tr><td>&nbsp;</td><td>{{ form.create_forum_thread }} <strong>Create a forum thread for this event?</strong></td></tr>
+{% endif %}
 <tr><td>&nbsp;</td><td>{% comment_dialogs %}<input type="submit" name="submit_button" value="Submit" /></td></tr>
 </table>
 </form>