Mercurial > public > sg101
changeset 471:d83296cac940
For #227: only enqueue user profiles if the user has changed the content we have in the search index.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 17 Aug 2011 02:02:20 +0000 |
parents | d9b6c4ec1977 |
children | 7c3816d76c6c |
files | gpp/bio/search_indexes.py gpp/bio/signals.py gpp/bio/views.py |
diffstat | 3 files changed, 53 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/bio/search_indexes.py Wed Aug 17 01:29:27 2011 +0000 +++ b/gpp/bio/search_indexes.py Wed Aug 17 02:02:20 2011 +0000 @@ -4,6 +4,7 @@ from custom_search.indexes import CondQueuedSearchIndex from bio.models import UserProfile +from bio.signals import profile_content_update class UserProfileIndex(CondQueuedSearchIndex): @@ -16,6 +17,15 @@ def get_updated_field(self): return 'update_date' + def _setup_save(self, model): + profile_content_update.connect(self.enqueue_save) + + def _teardown_save(self, model): + profile_content_update.disconnect(self.enqueue_save) + + def enqueue_save(self, sender, **kwargs): + return self.enqueue('update', sender) + def can_index(self, instance): return instance.user.is_active
--- a/gpp/bio/signals.py Wed Aug 17 01:29:27 2011 +0000 +++ b/gpp/bio/signals.py Wed Aug 17 02:02:20 2011 +0000 @@ -1,8 +1,10 @@ """ -Signal handler(s) for the bio application. +Signal handlers & signals for the bio application. + """ from django.db.models.signals import post_save from django.contrib.auth.models import User +import django.dispatch import bio.badges from bio.models import UserProfile @@ -91,3 +93,19 @@ post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals') post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals') post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.signals') + +# Signals for the bio application +# +# This signal is sent whenever a profile has had its textual content updated. +# The provided arguments to the receiver function are: +# - sender - the profile model instance + +profile_content_update = django.dispatch.Signal(providing_args=[]) + + +def notify_profile_content_update(profile): + """ + Convenience function to send the profile content update signal. + + """ + profile_content_update.send_robust(profile)
--- a/gpp/bio/views.py Wed Aug 17 01:29:27 2011 +0000 +++ b/gpp/bio/views.py Wed Aug 17 02:02:20 2011 +0000 @@ -1,7 +1,7 @@ """ Views for the bio application. + """ - from django.shortcuts import render_to_response from django.shortcuts import get_object_or_404 from django.template import RequestContext @@ -27,6 +27,7 @@ from bio.forms import EditUserForm from bio.forms import EditUserProfileForm from bio.forms import SearchUsersForm +from bio.signals import notify_profile_content_update from core.paginator import DiggPaginator from core.functions import email_admins from core.functions import get_page @@ -78,12 +79,12 @@ profile=profile).select_related("badge") return render_to_response('bio/view_profile.html', { - 'subject': request.user, - 'profile': profile, + 'subject': request.user, + 'profile': profile, 'hide_email': False, 'this_is_me': True, 'badge_collection': badge_collection, - }, + }, context_instance = RequestContext(request)) ####################################################################### @@ -100,14 +101,14 @@ badge_collection = BadgeOwnership.objects.filter( profile=profile).select_related("badge") - + return render_to_response('bio/view_profile.html', { - 'subject': user, - 'profile': profile, + 'subject': user, + 'profile': profile, 'hide_email': hide_email, 'this_is_me': False, 'badge_collection': badge_collection, - }, + }, context_instance = RequestContext(request)) ####################################################################### @@ -125,6 +126,7 @@ 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() @@ -134,7 +136,7 @@ return render_to_response('bio/edit_profile.html', { 'user_form': user_form, 'profile_form': profile_form, - }, + }, context_instance = RequestContext(request)) ####################################################################### @@ -168,7 +170,7 @@ return render_to_response('bio/avatar.html', { 'form': form, - }, + }, context_instance = RequestContext(request)) ####################################################################### @@ -228,18 +230,25 @@ 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 elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'): delete_id = request.POST['delete_id'] + update_occurred = True if new_data.get('delete-sn-form'): request.user.social_network_profiles.get(id=delete_id).delete() elif new_data.get('delete-im-form'): request.user.instant_messenger_profiles.get(id=delete_id).delete() elif new_data.get('delete-w-form'): request.user.website_profiles.get(id=delete_id).delete() + else: + update_occurred = False + + if update_occurred: + notify_profile_content_update(request.user.get_profile()) return HttpResponseRedirect(request.path) @@ -254,10 +263,10 @@ w_form = WebsiteForm() return render_to_response('bio/edit_elsewhere.html', { - 'sn_form': sn_form, - 'im_form': im_form, + 'sn_form': sn_form, + 'im_form': im_form, 'w_form': w_form, - }, + }, context_instance=RequestContext(request)) ####################################################################### @@ -268,13 +277,13 @@ form = SearchUsersForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] - return HttpResponseRedirect(reverse("bio-view_profile", + return HttpResponseRedirect(reverse("bio-view_profile", kwargs={'username': username})) else: form = SearchUsersForm() return render_to_response('bio/member_search.html', { 'form': form, - }, + }, context_instance=RequestContext(request))