def open_close_topic(request, topic_id, action): topic = get_object_or_404(Topic, pk=topic_id) if forum_moderated_by(topic, request.user) or can_close_topic(request.user, topic): if action == 'c': topic.closed = True messages.success(request, _("Topic closed.")) elif action == 'o': topic.closed = False messages.success(request, _("Topic opened.")) topic.save() return HttpResponseRedirect(topic.get_absolute_url())
def open_close_topic(request, topic_id, action): topic = get_object_or_404(Topic, pk=topic_id) if forum_moderated_by(topic, request.user) or can_close_topic( request.user, topic): if action == 'c': topic.closed = True messages.success(request, _("Topic closed.")) elif action == 'o': topic.closed = False messages.success(request, _("Topic opened.")) topic.save() return HttpResponseRedirect(topic.get_absolute_url())
def show_topic(request, topic_id, full=True): """ * Display a topic * save a reply * save a poll vote """ post_request = request.method == "POST" user_is_authenticated = request.user.is_authenticated() if post_request and not user_is_authenticated: # Info: only user that are logged in should get forms in the page. return HttpResponseForbidden() topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) if not topic.forum.category.has_access(request.user): return HttpResponseForbidden() Topic.objects.filter(pk=topic.id).update(views=F('views') + 1) last_post = topic.last_post if request.user.is_authenticated(): topic.update_read(request.user) # without specifying, following query wouldn't select related properly posts = topic.posts.select_related('user__userprofile', 'user__forum_profile', 'updated_by', 'user').prefetch_related('user__groups').all() edit_start = timezone.now() - timedelta(minutes=1) edit_end = timezone.now() editable = posts.filter(created__range=(edit_start, edit_end)).filter(user_id=request.user.id) can_edit = request.user.has_perm('djangobb_forum.change_post') try: page_url_parameter = int(request.GET.get('page') or 1) except: page_url_parameter = 1 first_post_number = int(forum_settings.TOPIC_PAGE_SIZE) * (page_url_parameter - 1) can_close = can_close_topic(request.user, topic) moderator = request.user.is_superuser or request.user in topic.forum.moderators.all() if user_is_authenticated and request.user in topic.subscribers.all(): subscribed = True else: subscribed = False # reply form reply_form = None form_url = None back_url = None if user_is_authenticated and (not topic.closed or moderator): form_url = request.path + '?' + request.META['QUERY_STRING'] + '#reply' # if form validation failed: browser should scroll down to reply form ;) back_url = request.path ip = get_client_ip(request) post_form_kwargs = { "topic":topic, "user":request.user, "ip":ip, 'is_ip_banned': request.is_ip_banned, } if post_request and AddPostForm.FORM_NAME in request.POST: reply_form = AddPostForm(request.POST, request.FILES, **post_form_kwargs) if reply_form.is_valid(): post = reply_form.save() messages.success(request, _("Reply saved.")) return HttpResponseRedirect(post.get_absolute_url() if full else post.get_mobile_url()) else: reply_form = AddPostForm( initial={ 'markup': request.user.forum_profile.markup, 'subscribe': False, }, **post_form_kwargs ) group_titles = {} for post in posts: if post.user.is_superuser: group_titles[post.user] = "Scratch Team" else: temp_names = [group.name for group in post.user.groups.all()] if "Forum Moderators" in temp_names: group_titles[post.user] = "Forum Moderator" elif "Scratchers" in temp_names: group_titles[post.user] = "Scratcher" elif "New Scratchers" in temp_names: group_titles[post.user] = "New to Scratch" else: group_titles[post.user] = "Ungrouped" # handle poll, if exists poll_form = None polls = topic.poll_set.all() if not polls: poll = None else: poll = polls[0] if user_is_authenticated: # Only logged in users can vote poll.auto_deactivate() has_voted = request.user in poll.users.all() if not post_request or not VotePollForm.FORM_NAME in request.POST: # It's not a POST request or: The reply form was send and not a poll vote if poll.active and not has_voted: poll_form = VotePollForm(poll) else: if not poll.active: messages.error(request, _("This poll is not active.")) return HttpResponseRedirect(topic.get_absolute_url()) elif has_voted: messages.error(request, _("You already voted on this poll.")) return HttpResponseRedirect(topic.get_absolute_url()) poll_form = VotePollForm(poll, request.POST) if poll_form.is_valid(): ids = poll_form.cleaned_data["choice"] queryset = poll.choices.filter(id__in=ids) queryset.update(votes=F('votes') + 1) poll.users.add(request.user) # save that this user has vote messages.success(request, _("Vote saved.")) return HttpResponseRedirect(topic.get_absolute_url()) highlight_word = request.GET.get('hl', '') if full: return render(request, 'djangobb_forum/topic.html', {'categories': Category.objects.all(), 'topic': topic, 'last_post': last_post, 'form_url': form_url, 'reply_form': reply_form, 'back_url': back_url, 'moderator': moderator, 'subscribed': subscribed, 'posts': posts, 'first_post_number': first_post_number, 'highlight_word': highlight_word, 'poll': poll, 'poll_form': poll_form, 'editable': editable, 'can_edit': can_edit, 'can_close': can_close, 'group_titles': group_titles, 'simple_user_agent': simple_user_agent(request), }) else: return render(request, 'djangobb_forum/mobile/topic.html', {'categories': Category.objects.all(), 'topic': topic, 'posts': posts, 'poll': poll, 'poll_form': poll_form, 'reply_form': reply_form, })
def show_topic(request, topic_id, full=True): """ * Display a topic * save a reply * save a poll vote """ post_request = request.method == "POST" user_is_authenticated = request.user.is_authenticated() if post_request and (not user_is_authenticated or not request.user.has_perm('userprofiles.is_social')): # Info: only user that are logged in should get forms in the page. return HttpResponseForbidden() topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) if not topic.forum.category.has_access(request.user): return HttpResponseForbidden() last_post = topic.last_post update_topic_on_view.delay(user_id=request.user.id, topic_id=topic.id, is_authenticated=user_is_authenticated) # without specifying, following query wouldn't select related properly posts = topic.posts.select_related( 'user__userprofile', 'user__forum_profile', 'updated_by', 'user', 'poststatus').prefetch_related('user__groups').all() edit_start = timezone.now() - timedelta(minutes=1) edit_end = timezone.now() editable = posts.filter(created__range=(edit_start, edit_end)).filter( user_id=request.user.id) can_edit = request.user.has_perm('djangobb_forum.change_post') try: page_url_parameter = int(request.GET.get('page') or 1) except: page_url_parameter = 1 first_post_number = int( forum_settings.TOPIC_PAGE_SIZE) * (page_url_parameter - 1) can_close = can_close_topic(request.user, topic) moderator = request.user.is_superuser or request.user in topic.forum.moderators.all( ) if user_is_authenticated and request.user in topic.subscribers.all(): subscribed = True else: subscribed = False # reply form reply_form = None form_url = None back_url = None if user_is_authenticated and (not topic.closed or moderator): form_url = request.path + '?' + request.META[ 'QUERY_STRING'] + '#reply' # if form validation failed: browser should scroll down to reply form ;) back_url = request.path ip = get_client_ip(request) post_form_kwargs = { "topic": topic, "user": request.user, "ip": ip, 'is_ip_banned': request.is_ip_banned, "request_data": request.META, "url": "http://scratch.mit.edu%s" % back_url, } if post_request and AddPostForm.FORM_NAME in request.POST: reply_form = AddPostForm(request.POST, request.FILES, **post_form_kwargs) if reply_form.is_valid(): post = reply_form.save() messages.success(request, _("Reply saved.")) return HttpResponseRedirect( post.get_absolute_url() if full else post.get_mobile_url()) else: reply_form = AddPostForm(initial={ 'markup': request.user.forum_profile.markup, 'subscribe': False, }, **post_form_kwargs) group_titles = {} for post in posts: if post.user.is_superuser: group_titles[post.user] = "Scratch Team" else: temp_names = [group.name for group in post.user.groups.all()] if "Forum Moderators" in temp_names: group_titles[post.user] = "Forum Moderator" elif "Scratchers" in temp_names: group_titles[post.user] = "Scratcher" elif "New Scratchers" in temp_names: group_titles[post.user] = "New to Scratch" else: group_titles[post.user] = "Ungrouped" # handle poll, if exists poll_form = None polls = topic.poll_set.all() if not polls: poll = None else: poll = polls[0] if user_is_authenticated: # Only logged in users can vote poll.auto_deactivate() has_voted = request.user in poll.users.all() if not post_request or not VotePollForm.FORM_NAME in request.POST: # It's not a POST request or: The reply form was send and not a poll vote if poll.active and not has_voted: poll_form = VotePollForm(poll) else: if not poll.active: messages.error(request, _("This poll is not active.")) return HttpResponseRedirect(topic.get_absolute_url()) elif has_voted: messages.error(request, _("You already voted on this poll.")) return HttpResponseRedirect(topic.get_absolute_url()) poll_form = VotePollForm(poll, request.POST) if poll_form.is_valid(): ids = poll_form.cleaned_data["choice"] queryset = poll.choices.filter(id__in=ids) queryset.update(votes=F('votes') + 1) poll.users.add( request.user) # save that this user has vote messages.success(request, _("Vote saved.")) return HttpResponseRedirect(topic.get_absolute_url()) highlight_word = request.GET.get('hl', '') if full: return render( request, 'djangobb_forum/topic.html', { 'categories': Category.objects.all(), 'topic': topic, 'last_post': last_post, 'form_url': form_url, 'reply_form': reply_form, 'back_url': back_url, 'moderator': moderator, 'subscribed': subscribed, 'posts': posts, 'first_post_number': first_post_number, 'highlight_word': highlight_word, 'poll': poll, 'poll_form': poll_form, 'editable': editable, 'can_edit': can_edit, 'can_close': can_close, 'group_titles': group_titles, 'simple_user_agent': simple_user_agent(request), }) else: return render( request, 'djangobb_forum/mobile/topic.html', { 'categories': Category.objects.all(), 'topic': topic, 'posts': posts, 'poll': poll, 'poll_form': poll_form, 'reply_form': reply_form, })