changeset 562:98b373ca09f3

For bitbucket issue #3, ensure that changes to Profile, Post & Topic models via the admin cause the search index to be updated.
author Brian Neal <bgneal@gmail.com>
date Wed, 08 Feb 2012 18:58:57 -0600
parents 8f3b7f0d4d13
children 93f049a241ff
files gpp/antispam/utils.py gpp/bio/admin.py gpp/bio/models.py gpp/bio/search_indexes.py gpp/bio/signals.py gpp/bio/views.py gpp/forums/admin.py gpp/forums/views/main.py gpp/forums/views/spam.py
diffstat 9 files changed, 59 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/antispam/utils.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/antispam/utils.py	Wed Feb 08 18:58:57 2012 -0600
@@ -60,7 +60,7 @@
     profile = user.get_profile()
     profile.status = STA_SUSPENDED
     profile.status_date = datetime.datetime.now()
-    profile.save()
+    profile.save(content_update=False)
 
 
 def _get_spam_phrases():
--- a/gpp/bio/admin.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/bio/admin.py	Wed Feb 08 18:58:57 2012 -0600
@@ -54,7 +54,7 @@
             profile.user.save()
             profile.status = status
             profile.status_date = now
-            profile.save()
+            profile.save(content_update=False)
 
         count = len(qs)
         msg = "1 user" if count == 1 else "%d users" % count
--- a/gpp/bio/models.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/bio/models.py	Wed Feb 08 18:58:57 2012 -0600
@@ -13,6 +13,7 @@
 
 from core.markup import SiteMarkup
 
+
 # These are the secondary user status enumeration values. 
 (STA_ACTIVE,        # User is a full member in good standing.
  STA_RESIGNED,      # User has voluntarily asked to be removed.
@@ -103,12 +104,29 @@
         ordering = ('user__username', )
 
     def save(self, *args, **kwargs):
-        self.update_date = datetime.datetime.now()
-        sm = SiteMarkup()
-        self.profile_html = sm.convert(self.profile_text)
-        self.signature_html = sm.convert(self.signature)
+        """
+        Custom profile save() function.
+        If content_update is True (default), then it is assumed that major
+        fields are being updated and that the profile_content_update signal
+        should be signalled. When content_update is False, the update_date is
+        not updated, expensive markup conversions are not performed, and the
+        signal is not signalled. This is useful for updating the
+        forum_post_count, for example.
+
+        """
+        content_update = kwargs.pop('content_update', True)
+
+        if content_update:
+            self.update_date = datetime.datetime.now()
+            sm = SiteMarkup()
+            self.profile_html = sm.convert(self.profile_text)
+            self.signature_html = sm.convert(self.signature)
+            cache.delete('avatar_' + self.user.username)
+
         super(UserProfile, self).save(*args, **kwargs)
-        cache.delete('avatar_' + self.user.username)
+
+        if content_update:
+            notify_profile_content_update(self)
 
     @models.permalink
     def get_absolute_url(self):
@@ -193,3 +211,6 @@
         if self.count == 1:
             return u"1 %s" % self.badge.name
         return u"%d %ss" % (self.count, self.badge.name)
+
+# Put down here to avoid a circular import
+from bio.signals import notify_profile_content_update
--- a/gpp/bio/search_indexes.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/bio/search_indexes.py	Wed Feb 08 18:58:57 2012 -0600
@@ -26,8 +26,5 @@
     def enqueue_save(self, sender, **kwargs):
         return self.enqueue('update', sender)
 
-    def can_index(self, instance):
-        return instance.user.is_active
-
 
 site.register(UserProfile, UserProfileIndex)
--- a/gpp/bio/signals.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/bio/signals.py	Wed Feb 08 18:58:57 2012 -0600
@@ -6,8 +6,6 @@
 from django.contrib.auth.models import User
 import django.dispatch
 
-import bio.badges
-from bio.models import UserProfile
 from donations.models import Donation
 from weblinks.models import Link
 from downloads.models import Download
@@ -25,7 +23,7 @@
     created = kwargs['created']
     if created:
         user = kwargs['instance']
-        profile = bio.models.UserProfile()
+        profile = UserProfile()
         profile.user = user
         profile.save()
 
@@ -109,3 +107,8 @@
 
     """
     profile_content_update.send_robust(profile)
+
+
+# To avoid circular imports
+import bio.badges
+from bio.models import UserProfile
--- a/gpp/bio/views.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/bio/views.py	Wed Feb 08 18:58:57 2012 -0600
@@ -10,6 +10,7 @@
 from django.http import HttpResponse
 from django.http import HttpResponseBadRequest
 from django.http import HttpResponseRedirect
+from django.http import HttpResponseServerError
 from django.http import Http404
 from django.core.paginator import InvalidPage
 from django.core.urlresolvers import reverse
@@ -126,7 +127,6 @@
             profile = profile_form.save(commit=False)
             profile.user = request.user
             profile.save()
-            notify_profile_content_update(profile)
             return HttpResponseRedirect(reverse('bio.views.my_profile'))
     else:
         profile = request.user.get_profile()
@@ -230,7 +230,6 @@
                 profile = form.save(commit=False)
                 profile.user = request.user
                 profile.save()
-                notify_profile_content_update(request.user.get_profile())
                 return HttpResponseRedirect(request.path)
 
         # Delete forms
--- a/gpp/forums/admin.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/forums/admin.py	Wed Feb 08 18:58:57 2012 -0600
@@ -10,6 +10,9 @@
 from forums.models import FlaggedPost
 from forums.models import ForumLastVisit
 from forums.models import TopicLastVisit
+from forums.signals import (notify_new_topic, notify_updated_topic,
+        notify_new_post, notify_updated_post)
+
 import bio.badges
 
 
@@ -38,6 +41,15 @@
     list_filter = ('creation_date', 'update_date', )
     save_on_top = True
 
+    # override save_model() to update the search index 
+    def save_model(self, request, obj, form, change):
+        obj.save()
+
+        if change:
+            notify_updated_topic(obj)
+        else:
+            notify_new_topic(obj)
+
 
 class PostAdmin(admin.ModelAdmin):
     list_display = ('user', 'creation_date', 'update_date', 'user_ip', 'summary')
@@ -52,6 +64,15 @@
     def queryset(self, request):
         return Post.objects.select_related('user')
 
+    # override save_model() to update the search index 
+    def save_model(self, request, obj, form, change):
+        obj.save()
+
+        if change:
+            notify_updated_post(obj)
+        else:
+            notify_new_post(obj)
+
 
 class FlaggedPostAdmin(admin.ModelAdmin):
     list_display = ['__unicode__', 'flag_date', 'get_post_url']
--- a/gpp/forums/views/main.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/forums/views/main.py	Wed Feb 08 18:58:57 2012 -0600
@@ -34,7 +34,6 @@
 from forums.unread import (get_forum_unread_status, get_topic_unread_status,
         get_post_unread_status, get_unread_topics)
 
-from forums.attachments import AttachmentProcessor
 import forums.permissions as perms
 from forums.signals import (notify_new_topic, notify_updated_topic,
         notify_new_post, notify_updated_post)
@@ -527,7 +526,7 @@
     profile = post.user.get_profile()
     if profile.forum_post_count > 0:
         profile.forum_post_count -= 1
-        profile.save()
+        profile.save(content_update=False)
 
     # If this post is the last_post in a topic, we need to update
     # both the topic and parent forum's last post fields. If we don't
@@ -999,7 +998,7 @@
     """
     profile = user.get_profile()
     profile.forum_post_count += 1
-    profile.save()
+    profile.save(content_update=False)
 
 
 def _quote_message(who, message):
--- a/gpp/forums/views/spam.py	Mon Feb 06 20:37:02 2012 -0600
+++ b/gpp/forums/views/spam.py	Wed Feb 08 18:58:57 2012 -0600
@@ -8,7 +8,6 @@
 from django.contrib.auth.decorators import login_required
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseRedirect
-from django.http import HttpResponseForbidden
 from django.shortcuts import get_object_or_404
 from django.shortcuts import render_to_response
 from django.template import RequestContext
@@ -52,7 +51,7 @@
     if user.is_active and profile.status == bio.models.STA_STRANGER:
         profile.status = bio.models.STA_ACTIVE
         profile.status_date = datetime.datetime.now()
-        profile.save()
+        profile.save(content_update=False)
 
 
 @login_required