# HG changeset patch # User Brian Neal # Date 1260767248 0 # Node ID ab7830b067b3caa4d507acd5c59b8c6046d659d1 # Parent 35a0e63458157a09385bb072ceed0ffdcbae881b Implement ticket #40. Added a simple way to search for usernames and then view their profile. Moved this ajax username search feature out of the messages app and into core. diff -r 35a0e6345815 -r ab7830b067b3 gpp/bio/forms.py --- a/gpp/bio/forms.py Sun Dec 13 21:57:34 2009 +0000 +++ b/gpp/bio/forms.py Mon Dec 14 05:07:28 2009 +0000 @@ -15,6 +15,7 @@ from django.contrib.auth.models import User from bio.models import UserProfile +from core.widgets import AutoCompleteUserInput class EditUserForm(forms.ModelForm): @@ -112,3 +113,17 @@ def get_filename(self): return self.cleaned_data['avatar_file'].name + +class SearchUsersForm(forms.Form): + """ + A form to search for users. + """ + username = forms.CharField(max_length=30, widget=AutoCompleteUserInput()) + + def clean_username(self): + username = self.cleaned_data['username'] + try: + User.objects.get(username=username, is_active=True) + except User.DoesNotExist: + raise forms.ValidationError("That username does not exist.") + return username diff -r 35a0e6345815 -r ab7830b067b3 gpp/bio/urls.py --- a/gpp/bio/urls.py Sun Dec 13 21:57:34 2009 +0000 +++ b/gpp/bio/urls.py Mon Dec 14 05:07:28 2009 +0000 @@ -5,6 +5,7 @@ url(r'^members/(?Puser|date)/page/(?P\d+)/$', 'member_list', name='bio-members_full'), + url(r'^members/search/$', 'member_search', name='bio-member_search'), url(r'^me/$', 'my_profile', name='bio-me'), url(r'^view/(?P\w{1,30})/$', 'view_profile', name='bio-view_profile'), url(r'^edit/$', 'edit_profile', name='bio-edit_profile'), diff -r 35a0e6345815 -r ab7830b067b3 gpp/bio/views.py --- a/gpp/bio/views.py Sun Dec 13 21:57:34 2009 +0000 +++ b/gpp/bio/views.py Mon Dec 14 05:07:28 2009 +0000 @@ -23,6 +23,7 @@ from bio.forms import UploadAvatarForm from bio.forms import EditUserForm from bio.forms import EditUserProfileForm +from bio.forms import SearchUsersForm from core.paginator import DiggPaginator from core.functions import email_admins @@ -240,3 +241,21 @@ }, context_instance=RequestContext(request)) +####################################################################### + +@login_required +def member_search(request): + if request.method == "POST": + form = SearchUsersForm(request.POST) + if form.is_valid(): + username = form.cleaned_data['username'] + 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)) + diff -r 35a0e6345815 -r ab7830b067b3 gpp/core/urls.py --- a/gpp/core/urls.py Sun Dec 13 21:57:34 2009 +0000 +++ b/gpp/core/urls.py Mon Dec 14 05:07:28 2009 +0000 @@ -5,4 +5,5 @@ urlpatterns = patterns('core.views', url(r'^markdown_help/$', 'markdown_help', name='core-markdown_help'), + url(r'^ajax/users/$', 'ajax_users', name='core-ajax_users'), ) diff -r 35a0e6345815 -r ab7830b067b3 gpp/core/views.py --- a/gpp/core/views.py Sun Dec 13 21:57:34 2009 +0000 +++ b/gpp/core/views.py Mon Dec 14 05:07:28 2009 +0000 @@ -2,6 +2,10 @@ Views for the core application. These are mainly shared, common views used by multiple applications. """ +from django.contrib.auth.models import User +from django.http import HttpResponse +from django.http import HttpResponseBadRequest +from django.http import HttpResponseForbidden from django.shortcuts import render_to_response from django.template import RequestContext from django.contrib.auth.decorators import login_required @@ -15,3 +19,24 @@ to be called via AJAX. """ return render_to_response('core/markdown_help.html') + + +def ajax_users(request): + """ + If the user is authenticated, return a string of usernames whose names start with + the 'q' GET parameter, limited by the 'limit' GET parameters. The names are separated + by newlines. Only active usernames are returned. + If the user is not authenticated, return an empty string. + """ + q = request.GET.get('q', None) + if q is None: + return HttpResponseBadRequest() + + if request.user.is_authenticated(): + q = request.GET.get('q', ' ') + limit = int(request.GET.get('limit', 10)) + users = User.objects.filter(is_active=True, + username__istartswith=q).values_list('username', flat=True)[:limit] + user_list = u"\n".join(users) + return HttpResponse(user_list) + return HttpResponseForbidden() diff -r 35a0e6345815 -r ab7830b067b3 gpp/core/widgets.py --- a/gpp/core/widgets.py Sun Dec 13 21:57:34 2009 +0000 +++ b/gpp/core/widgets.py Mon Dec 14 05:07:28 2009 +0000 @@ -16,7 +16,7 @@ js = settings.GPP_THIRD_PARTY_JS['jquery-autocomplete'] def render(self, name, value, attrs=None): - url = reverse('messages-ajax_users') + url = reverse('core-ajax_users') output = super(AutoCompleteUserInput, self).render(name, value, attrs) return output + mark_safe(u'''\