def username_autocomplete(request): if request.user.is_authenticated(): q = request.GET.get("q") friends = [] #Friendship.objects.friends_for_user(request.user) content = [] for friendship in friends: if friendship["friend"].username.lower().startswith(q): try: profile = friendship["friend"].get_profile() entry = "%s,,%s,,%s" % ( gravatar(friendship["friend"], 40), friendship["friend"].username, profile.location ) except Profile.DoesNotExist: pass content.append(entry) response = HttpResponse("\n".join(content)) else: response = HttpResponseForbidden() setattr(response, "djangologging.suppress_output", True) return response
def story_ajax(request, project_id, story_number, action=None): if not (request.method == 'POST' and request.is_ajax()): raise Http404 project = request.user.projects.get(pk=project_id) story = project.stories.get(number=story_number) if action == 'move': new_index = request.POST.get('index') new_index = int(new_index) if new_index else None new_phase_id = int(request.POST.get('phase', -1)) story.move(new_phase_id=new_phase_id, new_index=new_index) elif action == 'edit': story_form = StoryForm(project=project) def change_value(key): value = request.POST.get(key) value = story_form.fields[key].clean(value) setattr(story, key, value) story.save() if request.POST.has_key('name'): change_value('name') if request.POST.has_key('description'): change_value('description') return { 'html': render_to_string('agile/story/description.html', { 'story': story, }, RequestContext(request)), } if request.POST.has_key('owner') or request.POST.has_key('creator'): if request.POST.has_key('owner'): key = 'owner' elif request.POST.has_key('creator'): key = 'creator' change_value(key) user = getattr(story, key) return { 'html': gravatar(user, size=30) if user else '', } elif action == 'comment': comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = request.user comment.story = story comment.save() return { 'html': render_to_string('agile/story/comment.html', { 'comment': comment, }, RequestContext(request)), } else: errors = {} for field, error in comment_form.errors.iteritems(): errors[unicode(comment_form.fields[field].label)] = error return { 'success': False, 'error': errors, } elif action == 'delete': story.delete() elif action == 'time_entry': time_entries = request.user.time_entries.filter(stop_at=None) if not time_entries.exists(): time_entry = TimeEntry(user=request.user, story=story) time_entry.save() return { 'html': render_to_string('agile/story/time_entry.html', { 'time_entry': time_entry, }, RequestContext(request)), } else: story = time_entries[0].story story_url = story.get_url() return { 'story_number': story.number, 'story_url': story_url, 'story_name': story.name, }