def search(request): """ Shows forum search results as post list """ if request.GET: search_form = ForumSearchForm(request.GET) search_term = '' post_list = [] if search_form.is_valid(): search_term = search_form.cleaned_data['term'] group = Group.group_for_user(request.user) visible_forums_ids = list(group.permission_set.values_list('forum_id', flat=True)) posts = Post.objects.get_visible_posts().\ filter(topic__forum__in=visible_forums_ids, message__icontains=search_term).\ select_related('profile', 'profile__user', 'profile__group', 'topic', 'topic__first_post').\ order_by('-date') post_list = paginate(request, posts, settings.POSTS_PER_PAGE) return { 'search_form': search_form, 'search_term': search_term, 'post_list': post_list, } return HttpResponseRedirect(reverse('forum'))
def get_group_perms_or_404(user, forum): """ Returns user's group and group permissions for the specific forum """ group = Group.group_for_user(user) try: perms = group.permission_set.get(forum=forum) except ObjectDoesNotExist: raise Http404 return group, perms
def forum_list(request): """ Shows the list of forums that the user is permitted to view """ group = Group.group_for_user(request.user) visible_forums_ids = list(group.permission_set.values_list('forum_id', flat=True)) forum_list = Forum.objects.filter(id__in=visible_forums_ids).order_by('priority').\ select_related('last_post', 'last_post__topic', 'last_post__profile', 'last_post__topic__last_post') mark_unread_forums(request.user, forum_list) search_form = ForumSearchForm() # Popular topics popular_topics = Topic.objects.filter(post__date__gte=datetime.today()-settings.POPULAR_TOPICS_PERIOD, forum__id__in=visible_forums_ids, post__is_removed=False).\ annotate(count=Count('post')).\ order_by('-count')[:settings.POPULAR_TOPICS_COUNT] # New topics new_topics = Topic.objects.get_visible_topics().filter(forum__id__in=visible_forums_ids).order_by('-id')[:settings.NEW_TOPICS_COUNT] # Forum statistics total_posts_count = sum([forum.posts_count for forum in forum_list]) total_topics_count = sum([forum.topics_count for forum in forum_list]) total_users_count = UserProfile.objects.filter(user__is_active=True).count() novice_profile = User.objects.filter(is_active=True).latest('date_joined').get_profile() return { 'forum_list': forum_list, 'search_form': search_form, 'popular_topics': popular_topics, 'new_topics': new_topics, 'total_posts_count': total_posts_count, 'total_topics_count': total_topics_count, 'total_users_count': total_users_count, 'novice_profile': novice_profile, }
def __init__(self, *args, **kwargs): self.user = kwargs.pop("user") self.topic_src = kwargs.pop("topic_src") super(PostMoveForm, self).__init__(*args, **kwargs) # Create the topic list based on user permissions group = Group.group_for_user(self.user) visible_forums_ids = list(group.permission_set.values_list("forum_id", flat=True)) topic_list = ( Topic.objects.get_visible_topics().filter(forum__in=visible_forums_ids).order_by("forum__priority", "name") ) topic_dest_choices = [] for forum, topics in groupby(topic_list, lambda obj: obj.forum): topic_dest_choices.append((forum.name, [(topic.id, topic) for topic in topics])) self.fields["topic_dest"].choices = topic_dest_choices # Create the list of posts posts = self.topic_src.posts.order_by("date").select_related( "profile", "profile__user", "profile__group", "topic", "topic__first_post" ) self.fields["posts"].choices = [(post.id, post) for post in posts]