def call(self, **kwargs): result = self.request.session.get('search_results') if not result: return error404(self.request, _("No search results were found.")) items = result['search_results'] items_total = len(items); try: pagination = make_pagination(kwargs.get('page', 0), items_total, 12) except Http404: return redirect(reverse('search_results')) return render_to_response('search/results.html', { 'search_in': result.get('search_in'), 'search_route': result.get('search_route'), 'search_query': result['search_query'], 'search_author': result.get('search_author'), 'search_thread_titles': result.get('search_thread_titles'), 'search_thread': result.get('search_thread'), 'results': Post.objects.filter(id__in=items).select_related('forum', 'thread', 'user').order_by('-pk')[pagination['start']:pagination['stop']], 'items_total': items_total, 'pagination': pagination, }, context_instance=RequestContext(self.request))
def posts(request, user, page=0): queryset = user.post_set.filter(forum_id__in=Forum.objects.readable_forums(request.acl)).filter(deleted=False).filter(moderated=False) count = queryset.count() try: pagination = make_pagination(page, count, 12) except Http404: return redirect(reverse('user_posts', kwargs={'user': user.id, 'username': user.username_slug})) cache_key = 'user_profile_posts_graph_%s' % user.pk graph = cache.get(cache_key, 'nada') if graph == 'nada': if user.posts: graph = user.timeline(queryset.filter(date__gte=timezone.now()-timedelta(days=100))) else: graph = [0 for x in range(100)] cache.set(cache_key, graph, 14400) return render_to_response('profiles/posts.html', context_instance=RequestContext(request, { 'profile': user, 'tab': 'posts', 'graph_max': max(graph), 'graph': (str(i) for i in graph), 'items_total': count, 'items': queryset.select_related('thread', 'forum').order_by('-id')[pagination['start']:pagination['stop']], 'pagination': pagination, }));
def watched_threads(request, page=0, new=False): # Find mode and fetch threads readable_forums = Forum.objects.readable_forums(request.acl, True) if not request.settings['enable_private_threads']: readable_forums.remove(Forum.objects.special_pk('private_threads')) queryset = WatchedThread.objects.filter(user=request.user).filter(forum_id__in=readable_forums).select_related('thread').filter(thread__moderated=False).filter(thread__deleted=False) if new: queryset = queryset.filter(last_read__lt=F('thread__last')) count = queryset.count() pagination = make_pagination(page, count, request.settings.threads_per_page) queryset = queryset.order_by('-thread__last') if request.settings.threads_per_page < count: queryset = queryset[pagination['start']:pagination['stop']] queryset.prefetch_related('thread__forum', 'thread__start_poster', 'thread__last_poster') threads = [] for thread in queryset: thread.thread.send_email = thread.email thread.thread.is_read = thread.thread.last <= thread.last_read threads.append(thread.thread) # Display page return request.theme.render_to_response('watched.html', { 'items_total': count, 'pagination': pagination, 'new': new, 'threads': threads, 'message': request.messages.get_message('threads'), }, context_instance=RequestContext(request))
def call(self, **kwargs): result = self.request.session.get('search_results') if not result: form = QuickSearchForm(request=self.request) return self.render_to_response('error', form, {'message': _("No search results were found.")}) items = result['search_results'] items_total = len(items); try: pagination = make_pagination(kwargs.get('page', 0), items_total, 12) except Http404: return redirect(reverse('search_results')) form = QuickSearchForm(request=self.request, initial={'search_query': result['search_query']}) return self.render_to_response('results', form, { 'search_query': result['search_query'], 'search_in': result.get('search_in'), 'search_author': result.get('search_author'), 'search_thread_titles': result.get('search_thread_titles'), 'search_thread': result.get('search_thread'), 'results': items[pagination['start']:pagination['stop']], 'items_total': items_total, 'pagination': pagination, })
def fetch_threads(self): qs_announcements, qs_threads = self.threads_queryset() self.count = qs_threads.count() self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, self.request.settings.threads_per_page) tracker_forum = ThreadsTracker(self.request, self.forum) for thread in list(chain(qs_announcements, qs_threads[self.pagination['start']:self.pagination['stop']])): thread.is_read = tracker_forum.is_read(thread) self.threads.append(thread)
def posts(request, user, page=0): queryset = user.post_set.filter(forum_id__in=Forum.objects.readable_forums(request.acl)).filter(deleted=False).filter(moderated=False).select_related('thread', 'forum').order_by('-id') count = queryset.count() pagination = make_pagination(page, count, 12) return request.theme.render_to_response('profiles/posts.html', context_instance=RequestContext(request, { 'profile': user, 'tab': 'posts', 'items_total': count, 'items': queryset[pagination['start']:pagination['stop']], 'pagination': pagination, }));
def fetch_posts(self): self.count = self.request.acl.threads.filter_posts(self.request, self.thread, Post.objects.filter(thread=self.thread)).count() self.posts = self.request.acl.threads.filter_posts(self.request, self.thread, Post.objects.filter(thread=self.thread)).prefetch_related('user', 'user__rank') self.posts = self.posts.order_by('id') try: self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, settings.posts_per_page) except Http404: return redirect(reverse(self.type_prefix, kwargs={'thread': self.thread.pk, 'slug': self.thread.slug})) checkpoints_boundary = None if self.pagination['total'] > 1: self.posts = self.posts[self.pagination['start']:self.pagination['stop'] + 1] posts_len = len(self.posts) if self.pagination['page'] < self.pagination['total']: checkpoints_boundary = self.posts[posts_len - 1].date self.posts = self.posts[0:(posts_len - 1)] self.read_date = self.tracker.read_date(self.thread) ignored_users = [] if self.request.user.is_authenticated(): ignored_users = self.request.user.ignored_users() posts_dict = {} for post in self.posts: posts_dict[post.pk] = post post.message = self.request.messages.get_message('threads_%s' % post.pk) post.is_read = post.date <= self.read_date or (post.pk != self.thread.start_post_id and post.moderated) post.karma_vote = None post.ignored = self.thread.start_post_id != post.pk and not self.thread.pk in self.request.session.get('unignore_threads', []) and post.user_id in ignored_users if post.ignored: self.ignored = True self.thread.add_checkpoints_to_posts(self.request.acl.threads.can_see_all_checkpoints(self.forum), self.posts, (self.posts[0].date if self.pagination['page'] > 1 else None), checkpoints_boundary) last_post = self.posts[len(self.posts) - 1] if not self.tracker.is_read(self.thread): self.tracker_update(last_post) if self.watcher and last_post.date > self.watcher.last_read: self.watcher.last_read = timezone.now() self.watcher.save(force_update=True) if self.request.user.is_authenticated(): for karma in Karma.objects.filter(post_id__in=posts_dict.keys()).filter(user=self.request.user): posts_dict[karma.post_id].karma_vote = karma
def watched_threads(request, page=0, new=False): # Find mode and fetch threads readable_forums = Forum.objects.readable_forums(request.acl, True) starter_readable_forums = Forum.objects.starter_readable_forums(request.acl) if not readable_forums and not readable_forums: return error403(request, _("%(username), you cannot read any forums.") % {'username': request.user.username}) private_threads_pk = Forum.objects.special_pk('private_threads') if not settings.enable_private_threads and private_threads_pk in readable_forums: readable_forums.remove(private_threads_pk) queryset = WatchedThread.objects.filter(user=request.user).filter(thread__moderated=False).filter(thread__deleted=False).select_related('thread') if starter_readable_forums and readable_forums: queryset = queryset.filter(Q(forum_id__in=readable_forums) | Q(forum_id__in=starter_readable_forums, starter_id=request.user.pk)) elif starter_readable_forums: queryset = queryset.filter(starter_id__in=request.user.pk).filter(forum_id__in=starter_readable_forums) else: queryset = queryset.filter(forum_id__in=readable_forums) if settings.avatars_on_threads_list: queryset = queryset.prefetch_related('thread__last_poster') if new: queryset = queryset.filter(last_read__lt=F('thread__last')) count = queryset.count() try: pagination = make_pagination(page, count, settings.threads_per_page) except Http404: if new: return redirect(reverse('watched_threads_new')) return redirect(reverse('watched_threads')) queryset = queryset.order_by('-thread__last') if settings.threads_per_page < count: queryset = queryset[pagination['start']:pagination['stop']] queryset.prefetch_related('thread__forum', 'thread__start_poster', 'thread__last_poster') threads = [] for thread in queryset: thread.thread.send_email = thread.email thread.thread.is_read = thread.thread.last <= thread.last_read threads.append(thread.thread) # Display page return render_to_response('watched.html', { 'items_total': count, 'pagination': pagination, 'new': new, 'threads': threads, 'prefixes': ThreadPrefix.objects.all_prefixes(), 'message': request.messages.get_message('threads'), }, context_instance=RequestContext(request))
def followers(request, user, page=0): queryset = user.follows_set.order_by('username_slug') count = queryset.count() pagination = make_pagination(page, count, 24) return request.theme.render_to_response('profiles/followers.html', context_instance=RequestContext(request, { 'profile': user, 'tab': 'followers', 'items_total': count, 'items': queryset[pagination['start']:pagination['stop']], 'pagination': pagination, }));
def fetch_threads(self): qs_threads = self.threads_queryset() # Add in first and last poster if self.request.settings.avatars_on_threads_list: qs_threads = qs_threads.prefetch_related('start_poster', 'last_poster') self.count = qs_threads.count() self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, self.request.settings.threads_per_page) tracker_forum = ThreadsTracker(self.request, self.forum) for thread in qs_threads[self.pagination['start']:self.pagination['stop']]: thread.is_read = tracker_forum.is_read(thread) self.threads.append(thread)
def follows(request, user, page=0): queryset = user.follows.order_by('username_slug') count = queryset.count() try: pagination = make_pagination(page, count, 24) except Http404: return redirect(reverse('user_follows', kwargs={'user': user.id, 'username': user.username_slug})) return render_to_response('profiles/follows.html', context_instance=RequestContext(request, { 'profile': user, 'tab': 'follows', 'items_total': count, 'items': queryset[pagination['start']:pagination['stop']], 'pagination': pagination,}));
def threads(request, user, page=0): queryset = user.thread_set.filter(forum_id__in=Forum.objects.readable_forums(request.acl)).filter(deleted=False).filter(moderated=False).select_related('start_post', 'forum').order_by('-id') count = queryset.count() try: pagination = make_pagination(page, count, 12) except Http404: return redirect(reverse('user_threads', kwargs={'user': user.id, 'username': user.username_slug})) return request.theme.render_to_response('profiles/threads.html', context_instance=RequestContext(request, { 'profile': user, 'tab': 'threads', 'items_total': count, 'items': queryset[pagination['start']:pagination['stop']], 'pagination': pagination, }));
def new_threads(request, page=0): queryset = Thread.objects.filter(forum_id__in=Forum.objects.readable_forums(request.acl)).filter(deleted=False).filter(moderated=False).filter(start__gte=(timezone.now() - timedelta(days=2))) items_total = queryset.count(); pagination = make_pagination(page, items_total, 30) queryset = queryset.order_by('-start').prefetch_related('forum')[pagination['start']:pagination['stop']]; if request.settings['avatars_on_threads_list']: queryset = queryset.prefetch_related('start_poster', 'last_poster') return request.theme.render_to_response('new_threads.html', { 'items_total': items_total, 'threads': Thread.objects.with_reads(queryset, request.user), 'pagination': pagination, }, context_instance=RequestContext(request));
def watched_threads(request, page=0, new=False): # Find mode and fetch threads readable_forums = Forum.objects.readable_forums(request.acl, True) if not request.settings["enable_private_threads"]: readable_forums.remove(Forum.objects.special_pk("private_threads")) queryset = ( WatchedThread.objects.filter(user=request.user) .filter(forum_id__in=readable_forums) .select_related("thread") .filter(thread__moderated=False) .filter(thread__deleted=False) ) if request.settings["avatars_on_threads_list"]: queryset = queryset.prefetch_related("thread__last_poster") if new: queryset = queryset.filter(last_read__lt=F("thread__last")) count = queryset.count() try: pagination = make_pagination(page, count, request.settings.threads_per_page) except Http404: if new: return redirect(reverse("watched_threads_new")) return redirect(reverse("watched_threads")) queryset = queryset.order_by("-thread__last") if request.settings.threads_per_page < count: queryset = queryset[pagination["start"] : pagination["stop"]] queryset.prefetch_related("thread__forum", "thread__start_poster", "thread__last_poster") threads = [] for thread in queryset: thread.thread.send_email = thread.email thread.thread.is_read = thread.thread.last <= thread.last_read threads.append(thread.thread) # Display page return request.theme.render_to_response( "watched.html", { "items_total": count, "pagination": pagination, "new": new, "threads": threads, "message": request.messages.get_message("threads"), }, context_instance=RequestContext(request), )
def popular_threads(request, page=0): queryset = Thread.objects.filter(forum_id__in=Forum.objects.readable_forums(request.acl)).filter(deleted=False).filter(moderated=False) items_total = queryset.count(); try: pagination = make_pagination(page, items_total, 30) except Http404: return redirect(reverse('popular_threads')) queryset = queryset.order_by('-score', '-last').prefetch_related('forum')[pagination['start']:pagination['stop']]; if request.settings['avatars_on_threads_list']: queryset = queryset.prefetch_related('start_poster', 'last_poster') return request.theme.render_to_response('popular_threads.html', { 'items_total': items_total, 'threads': Thread.objects.with_reads(queryset, request.user), 'pagination': pagination, }, context_instance=RequestContext(request));
def warnings(request, user, page=0): request.acl.warnings.allow_member_warns_view(request.user, user) queryset = user.warning_set count = queryset.count() try: pagination = make_pagination(page, count, 12) except Http404: return redirect(reverse('user_warnings', kwargs={'user': user.id, 'username': user.username_slug})) return render_to_response('profiles/warnings.html', context_instance=RequestContext(request, { 'profile': user, 'tab': 'warnings', 'items_total': count, 'warning_level': user.get_current_warning_level(), 'warnings_tracker': WarningsTracker(user.warning_level - pagination['start']), 'items': queryset.order_by('-id')[pagination['start']:pagination['stop']], 'pagination': pagination, }));
def fetch_posts(self): self.count = self.request.acl.threads.filter_posts(self.request, self.thread, Post.objects.filter(thread=self.thread)).count() self.posts = self.request.acl.threads.filter_posts(self.request, self.thread, Post.objects.filter(thread=self.thread)).prefetch_related('checkpoint_set', 'user', 'user__rank') if self.thread.merges > 0: self.posts = self.posts.order_by('merge', 'pk') else: self.posts = self.posts.order_by('pk') self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, self.request.settings.posts_per_page) if self.request.settings.posts_per_page < self.count: self.posts = self.posts[self.pagination['start']:self.pagination['stop']] self.read_date = self.tracker.read_date(self.thread) ignored_users = [] if self.request.user.is_authenticated(): ignored_users = self.request.user.ignored_users() posts_dict = {} for post in self.posts: posts_dict[post.pk] = post post.message = self.request.messages.get_message('threads_%s' % post.pk) post.is_read = post.date <= self.read_date or (post.pk != self.thread.start_post_id and post.moderated) post.karma_vote = None post.ignored = self.thread.start_post_id != post.pk and not self.thread.pk in self.request.session.get('unignore_threads', []) and post.user_id in ignored_users if post.ignored: self.ignored = True last_post = self.posts[len(self.posts) - 1] if not self.tracker.is_read(self.thread): self.tracker_update(last_post) if self.watcher and last_post.date > self.watcher.last_read: self.watcher.last_read = timezone.now() self.watcher.save(force_update=True) if self.request.user.is_authenticated(): for karma in Karma.objects.filter(post_id__in=posts_dict.keys()).filter(user=self.request.user): posts_dict[karma.post_id].karma_vote = karma
def new_threads(request, page=0): queryset = Thread.objects.filter(forum_id__in=Forum.objects.readable_forums(request.acl)).filter(deleted=False).filter(moderated=False) items_total = queryset.count(); if items_total > (settings.threads_per_page * 3): items_total = settings.threads_per_page * 3 try: pagination = make_pagination(page, items_total, settings.threads_per_page) except Http404: return redirect(reverse('new_threads')) queryset = queryset.order_by('-start').prefetch_related('forum')[pagination['start']:pagination['stop']]; if settings.avatars_on_threads_list: queryset = queryset.prefetch_related('start_poster', 'last_poster') return render_to_response('new_threads.html', { 'items_total': items_total, 'threads': Thread.objects.with_reads(queryset, request.user), 'prefixes': ThreadPrefix.objects.all_prefixes(), 'pagination': pagination, }, context_instance=RequestContext(request));
def threads(request, user, page=0): queryset = ( user.thread_set.filter(forum_id__in=Forum.objects.readable_forums(request.acl)) .filter(deleted=False) .filter(moderated=False) ) count = queryset.count() try: pagination = make_pagination(page, count, 12) except Http404: return redirect(reverse("user_threads", kwargs={"user": user.id, "username": user.username_slug})) cache_key = "user_profile_threads_graph_%s" % user.pk graph = cache.get(cache_key, "nada") if graph == "nada": if user.posts: graph = user.timeline(queryset.filter(start__gte=timezone.now() - timedelta(days=100))) else: graph = [0 for x in range(100)] cache.set(cache_key, graph, 14400) return render_to_response( "profiles/threads.html", context_instance=RequestContext( request, { "profile": user, "tab": "threads", "graph_max": max(graph), "graph": (str(i) for i in graph), "items_total": count, "items": queryset.select_related("start_post", "forum").order_by("-id")[ pagination["start"] : pagination["stop"] ], "pagination": pagination, }, ), )
def fetch_threads(self): qs_announcements, qs_threads = self.threads_queryset() self.count = qs_threads.count() try: self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, self.request.settings.threads_per_page) except Http404: return self.threads_list_redirect() tracker_forum = ThreadsTracker(self.request, self.forum) unresolved_count = 0 for thread in list(chain(qs_announcements, qs_threads[self.pagination['start']:self.pagination['stop']])): thread.original_weight = thread.weight if thread.weight == 2: unresolved_count += 1 thread.is_read = tracker_forum.is_read(thread) thread.report_forum = None if thread.report_for_id: thread.report_forum = Forum.objects.forums_tree.get(thread.report_for.forum_id) self.threads.append(thread) if int(self.request.monitor['reported_posts']) != unresolved_count: self.request.monitor['reported_posts'] = unresolved_count
def call(self, **kwargs): result = self.request.session.get('search_results') if not result: return error404(self.request, _("No search results were found.")) items = result['search_results'] items_total = len(items) try: pagination = make_pagination(kwargs.get('page', 0), items_total, 12) except Http404: return redirect(reverse('search_results')) return render_to_response('search/results.html', { 'search_in': result.get('search_in'), 'search_route': result.get('search_route'), 'search_query': result['search_query'], 'search_author': result.get('search_author'), 'search_thread_titles': result.get('search_thread_titles'), 'search_thread': result.get('search_thread'), 'results': Post.objects.filter(id__in=items).select_related( 'forum', 'thread', 'user').order_by('-pk') [pagination['start']:pagination['stop']], 'items_total': items_total, 'pagination': pagination, }, context_instance=RequestContext( self.request))
def fetch_threads(self): qs_announcements, qs_threads = self.threads_queryset() self.count = qs_threads.count() try: self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, settings.threads_per_page) except Http404: return self.threads_list_redirect() tracker_forum = ThreadsTracker(self.request, self.forum) unresolved_count = 0 for thread in list(chain(qs_announcements, qs_threads[self.pagination['start']:self.pagination['stop']])): thread.original_weight = thread.weight if thread.weight == 2: unresolved_count += 1 thread.is_read = tracker_forum.is_read(thread) thread.report_forum = None if thread.report_for_id: thread.report_forum = Forum.objects.forums_tree.get(thread.report_for.forum_id) self.threads.append(thread) if monitor['reported_posts'] != unresolved_count: with UpdatingMonitor() as cm: monitor['reported_posts'] = unresolved_count
def list(request, slug=None, page=1): ranks = Rank.objects.filter(as_tab=1).order_by('order') # Find active rank default_rank = False active_rank = None if slug: for rank in ranks: if rank.slug == slug: active_rank = rank if not active_rank: return error404(request) if ranks and active_rank.slug == ranks[0].slug: return redirect(reverse('users')) elif ranks: default_rank = True active_rank = ranks[0] # Empty Defaults message = None users = [] items_total = 0 pagination = None in_search = False # Users search? if request.method == 'POST': if not request.acl.users.can_search_users(): return error403(request) in_search = True active_rank = None search_form = QuickFindUserForm(request.POST, request=request) if search_form.is_valid(): # Direct hit? username = search_form.cleaned_data['username'] try: user = User.objects.get(username__iexact=username) return redirect(reverse('user', args=(user.username_slug, user.pk))) except User.DoesNotExist: pass # Looks like well have to find near match if len(username) > 6: username = username[0:-3] elif len(username) > 5: username = username[0:-2] elif len(username) > 4: username = username[0:-1] username = slugify(username.strip()) # Go for rought match if len(username) > 0: users = User.objects.filter(username_slug__startswith=username).order_by('username_slug')[:10] elif search_form.non_field_errors()[0] == 'form_contains_errors': message = Message(_("To search users you have to enter username in search field."), 'error') else: message = Message(search_form.non_field_errors()[0], 'error') else: search_form = QuickFindUserForm(request=request) if active_rank: users = User.objects.filter(rank=active_rank) items_total = users.count() pagination = make_pagination(page, items_total, request.settings['profiles_per_list']) users = users.order_by('username_slug')[pagination['start']:pagination['stop']] return request.theme.render_to_response('profiles/list.html', { 'message': message, 'search_form': FormFields(search_form).fields, 'in_search': in_search, 'active_rank': active_rank, 'default_rank': default_rank, 'items_total': items_total, 'ranks': ranks, 'users': users, 'pagination': pagination, }, context_instance=RequestContext(request));
def fetch_posts(self): self.count = self.request.acl.threads.filter_posts( self.request, self.thread, Post.objects.filter(thread=self.thread)).count() self.posts = self.request.acl.threads.filter_posts( self.request, self.thread, Post.objects.filter(thread=self.thread)).prefetch_related( 'user', 'user__rank') self.posts = self.posts.order_by('id') try: self.pagination = make_pagination( self.kwargs.get('page', 0), self.count, self.request.settings.posts_per_page) except Http404: return redirect( reverse(self.type_prefix, kwargs={ 'thread': self.thread.pk, 'slug': self.thread.slug })) checkpoints_range = None if self.request.settings.posts_per_page < self.count: self.posts = self.posts[self.pagination['start']:self. pagination['stop'] + 1] posts_len = len(self.posts) checkpoints_range = self.posts[posts_len - 1].date self.posts = self.posts[0:(posts_len - 2)] self.read_date = self.tracker.read_date(self.thread) ignored_users = [] if self.request.user.is_authenticated(): ignored_users = self.request.user.ignored_users() posts_dict = {} for post in self.posts: posts_dict[post.pk] = post post.message = self.request.messages.get_message('threads_%s' % post.pk) post.is_read = post.date <= self.read_date or ( post.pk != self.thread.start_post_id and post.moderated) post.karma_vote = None post.ignored = self.thread.start_post_id != post.pk and not self.thread.pk in self.request.session.get( 'unignore_threads', []) and post.user_id in ignored_users if post.ignored: self.ignored = True self.thread.set_checkpoints( self.request.acl.threads.can_see_all_checkpoints(self.forum), self.posts, checkpoints_range) last_post = self.posts[len(self.posts) - 1] if not self.tracker.is_read(self.thread): self.tracker_update(last_post) if self.watcher and last_post.date > self.watcher.last_read: self.watcher.last_read = timezone.now() self.watcher.save(force_update=True) if self.request.user.is_authenticated(): for karma in Karma.objects.filter( post_id__in=posts_dict.keys()).filter( user=self.request.user): posts_dict[karma.post_id].karma_vote = karma
def get_pagination(self, total, page): if not self.pagination or total < 0: # Dont do anything if we are not paging return None return make_pagination(page, total, self.pagination)
def redirect_to_post(self, post): pagination = make_pagination(0, self.request.acl.threads.filter_posts(self.request, self.thread, self.thread.post_set).filter(id__lte=post.pk).count(), self.request.settings.posts_per_page) if pagination['total'] > 1: return redirect(reverse(self.type_prefix, kwargs={'thread': self.thread.pk, 'slug': self.thread.slug, 'page': pagination['total']}) + ('#post-%s' % post.pk)) return redirect(reverse(self.type_prefix, kwargs={'thread': self.thread.pk, 'slug': self.thread.slug}) + ('#post-%s' % post.pk))
def list(request, slug=None, page=0): ranks = Rank.objects.filter(as_tab=1).order_by('order') # Find active rank default_rank = False active_rank = None if slug: for rank in ranks: if rank.slug == slug: active_rank = rank if not active_rank: return error404(request) if ranks and active_rank.slug == ranks[0].slug: return redirect(reverse('users')) elif ranks: default_rank = True active_rank = ranks[0] # Empty Defaults message = None users = [] items_total = 0 pagination = None in_search = False # Users search? if request.method == 'POST': if not request.acl.users.can_search_users(): return error403(request) in_search = True active_rank = None search_form = QuickFindUserForm(request.POST, request=request) if search_form.is_valid(): # Direct hit? username = search_form.cleaned_data['username'] try: user = User.objects if settings.PROFILE_EXTENSIONS_PRELOAD: user = user.select_related( *settings.PROFILE_EXTENSIONS_PRELOAD) user = user.get(username__iexact=username) return redirect( reverse('user', args=(user.username_slug, user.pk))) except User.DoesNotExist: pass # Looks like well have to find near match if len(username) > 6: username = username[0:-3] elif len(username) > 5: username = username[0:-2] elif len(username) > 4: username = username[0:-1] username = slugify(username.strip()) # Go for rought match if len(username) > 0: users = User.objects if settings.PROFILE_EXTENSIONS_PRELOAD: users = users.select_related( *settings.PROFILE_EXTENSIONS_PRELOAD) users = users.filter(username_slug__startswith=username ).order_by('username_slug')[:10] elif search_form.non_field_errors()[0] == 'form_contains_errors': message = Message( _("To search users you have to enter username in search field." ), 'error') else: message = Message(search_form.non_field_errors()[0], 'error') else: search_form = QuickFindUserForm(request=request) if active_rank: users = User.objects.filter(rank=active_rank) items_total = users.count() try: pagination = make_pagination( page, items_total, request.settings['profiles_per_list']) except Http404: if not default_rank and active_rank: return redirect( reverse('users', kwargs={'slug': active_rank.slug})) return redirect(reverse('users')) if settings.PROFILE_EXTENSIONS_PRELOAD: users = users.select_related( *settings.PROFILE_EXTENSIONS_PRELOAD) users = users.order_by( 'username_slug')[pagination['start']:pagination['stop']] return request.theme.render_to_response( 'profiles/list.html', { 'message': message, 'search_form': FormFields(search_form).fields, 'in_search': in_search, 'active_rank': active_rank, 'default_rank': default_rank, 'items_total': items_total, 'ranks': ranks, 'users': users, 'pagination': pagination, }, context_instance=RequestContext(request))