def new_thread(request, forum_name_slug): forum = get_object_or_404(Forum, name_slug=forum_name_slug) user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports() if request.method == 'POST': form = NewThreadForm(request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): thread = Thread.objects.create(forum=forum, author=request.user, title=form.cleaned_data["title"]) may_be_spam = text_may_be_spam(form.cleaned_data["body"]) or \ text_may_be_spam(form.cleaned_data["title"]) if not request.user.posts.filter(moderation_state="OK").count() and may_be_spam: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data['body'], thread=thread) add_post_to_solr(post) set_to_moderation = False # Add first post to thread (first post will always be the same) # We need to reload thread object from DB, not so overwrite the object we created before when saving updated_thread = Thread.objects.get(id=thread.id) updated_thread.first_post = post updated_thread.save() if form.cleaned_data["subscribe"]: Subscription.objects.create(subscriber=request.user, thread=thread, is_active=True) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message(request, messages.INFO, "Your post won't be shown until it is manually " "approved by moderators") return HttpResponseRedirect(post.thread.forum.get_absolute_url()) else: form = NewThreadForm() if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message(request, messages.INFO, "You're not allowed to post in the forums because your account " "has been temporaly blocked after multiple spam reports") tvars = {'forum': forum, 'form': form} return render(request, 'forum/new_thread.html', tvars)
def new_thread(request, forum_name_slug): forum = get_object_or_404(Forum, name_slug=forum_name_slug) user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports() if request.method == 'POST': form = NewThreadForm(request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): thread = Thread.objects.create(forum=forum, author=request.user, title=form.cleaned_data["title"]) may_be_spam = text_may_be_spam(form.cleaned_data["body"]) or \ text_may_be_spam(form.cleaned_data["title"]) if not request.user.post_set.filter(moderation_state="OK").count() and may_be_spam: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data['body'], thread=thread) add_post_to_solr(post) set_to_moderation = False # Add first post to thread (first post will allways be the same) # We need to reload thread object from DB, not so overwrite the object we created before when saving updated_thread = Thread.objects.get(id=thread.id) updated_thread.first_post = post updated_thread.save() if form.cleaned_data["subscribe"]: Subscription.objects.create(subscriber=request.user, thread=thread, is_active=True) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message(request, messages.INFO, "Your post won't be shown until it is manually " "approved by moderators") return HttpResponseRedirect(post.thread.forum.get_absolute_url()) else: form = NewThreadForm() if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message(request, messages.INFO, "You're not allowed to post in the forums because your account " "has been temporaly blocked after multiple spam reports") return render(request, 'forum/new_thread.html', locals())
def new_thread(request, forum_name_slug): forum = get_object_or_404(Forum, name_slug=forum_name_slug) user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports() if request.method == 'POST': form = NewThreadForm(request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): thread = Thread.objects.create(forum=forum, author=request.user, title=form.cleaned_data["title"]) mayBeSpam = False text = form.cleaned_data["body"] if "http://" in form.cleaned_data["body"] or "https://" in form.cleaned_data["body"]: mayBeSpam = True if re.search("[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\s|$|\/|\]|\.)", form.cleaned_data["body"]): mayBeSpam = True if not request.user.post_set.all().count() and mayBeSpam: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data['body'], thread=thread) add_post_to_solr(post) set_to_moderation = False # Add first post to thread (this will never be changed) # First we have to "reload" thread object from DB, not so overwrite the object we created before when saving updated_thread = Thread.objects.get(id=thread.id) updated_thread.first_post = post updated_thread.save() if form.cleaned_data["subscribe"]: Subscription.objects.create(subscriber=request.user, thread=thread, is_active=True) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message(request, messages.INFO, "Your post won't be shown until it is manually approved by moderators") return HttpResponseRedirect(post.thread.forum.get_absolute_url()) else: form = NewThreadForm() if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message(request, messages.INFO, "You're not allowed to post in the forums because your account has been temporaly blocked after multiple spam reports") return render_to_response('forum/new_thread.html', locals(), context_instance=RequestContext(request))
def post_edit(request, post_id): post = get_object_or_404(Post, id=post_id) if post.author == request.user or request.user.has_perm('forum.change_post'): if request.method == 'POST': form = PostReplyForm(request, '', request.POST) if form.is_valid(): delete_post_from_solr(post) post.body = form.cleaned_data['body'] post.save() add_post_to_solr(post) # Update post in solr return HttpResponseRedirect( reverse('forums-post', args=[post.thread.forum.name_slug, post.thread.id, post.id])) else: form = PostReplyForm(request, '', {'body': post.body}) return render(request, 'forum/post_edit.html', locals()) else: raise Http404
def post_edit(request, post_id): post = get_object_or_404(Post, id=post_id) if post.author == request.user or request.user.has_perm('forum.change_post'): if request.method == 'POST': form = PostReplyForm(request, '', request.POST) if form.is_valid(): delete_post_from_solr(post) post.body = form.cleaned_data['body'] post.save() add_post_to_solr(post) # Update post in solr return HttpResponseRedirect( reverse('forums-post', args=[post.thread.forum.name_slug, post.thread.id, post.id])) else: form = PostReplyForm(request, '', {'body': post.body}) tvars = {'form': form} return render(request, 'forum/post_edit.html', tvars) else: raise Http404
def new_thread(request, forum_name_slug): forum = get_object_or_404(Forum, name_slug=forum_name_slug) user_can_post_in_forum, user_can_post_message = request.user.profile.can_post_in_forum( ) user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports( ) if request.method == 'POST': form = NewThreadForm(request.POST) if user_can_post_in_forum and not user_is_blocked_for_spam_reports: if form.is_valid(): post_title = form.cleaned_data["title"] post_body = form.cleaned_data["body"] thread = Thread.objects.create(forum=forum, author=request.user, title=post_title) may_be_spam = text_may_be_spam(post_body) or \ text_may_be_spam(post_title) post_body = remove_control_chars(post_body) if not request.user.posts.filter( moderation_state="OK").count() and may_be_spam: post = Post.objects.create(author=request.user, body=post_body, thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=post_body, thread=thread) add_post_to_solr(post.id) set_to_moderation = False # Add first post to thread (first post will always be the same) # We need to reload thread object from DB, not so overwrite the object we created before when saving # TODO: Ideally we would have a specific function to create a Post and add it to a thread immediately # so that we can use this functionality in tests too updated_thread = Thread.objects.get(id=thread.id) updated_thread.first_post = post updated_thread.save() if form.cleaned_data["subscribe"]: Subscription.objects.create(subscriber=request.user, thread=thread, is_active=True) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message( request, messages.INFO, "Your post won't be shown until it is manually " "approved by moderators") return HttpResponseRedirect( post.thread.forum.get_absolute_url()) else: form = NewThreadForm() if not user_can_post_in_forum: messages.add_message(request, messages.INFO, user_can_post_message) if user_is_blocked_for_spam_reports: messages.add_message( request, messages.INFO, "You're not allowed to post in the forums because your account " "has been temporaly blocked after multiple spam reports") tvars = {'forum': forum, 'form': form} return render(request, 'forum/new_thread.html', tvars)
def reply(request, forum_name_slug, thread_id, post_id=None): forum = get_object_or_404(Forum, name_slug=forum_name_slug) thread = get_object_or_404(Thread, id=thread_id, forum=forum, first_post__moderation_state="OK") if post_id: post = get_object_or_404(Post, id=post_id, thread__id=thread_id, thread__forum__name_slug=forum_name_slug) quote = loader.render_to_string('forum/quote_style.html', {'post': post}) else: post = None quote = "" latest_posts = Post.objects.select_related('author', 'author__profile', 'thread', 'thread__forum')\ .order_by('-created').filter(thread=thread, moderation_state="OK")[0:15] user_can_post_in_forum, user_can_post_message = request.user.profile.can_post_in_forum( ) user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports( ) if request.method == 'POST': form = PostReplyForm(request, quote, request.POST) if user_can_post_in_forum and not user_is_blocked_for_spam_reports: if form.is_valid(): may_be_spam = text_may_be_spam(form.cleaned_data.get("body", '')) or \ text_may_be_spam(form.cleaned_data.get("title", '')) if not request.user.posts.filter( moderation_state="OK").count() and may_be_spam: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread) add_post_to_solr(post.id) set_to_moderation = False if form.cleaned_data["subscribe"]: subscription, created = Subscription.objects.get_or_create( thread=thread, subscriber=request.user) if not subscription.is_active: subscription.is_active = True subscription.save() # figure out if there are active subscriptions in this thread if not set_to_moderation: users_to_notify = [] for subscription in Subscription.objects\ .filter(thread=thread, is_active=True).exclude(subscriber=request.user): users_to_notify.append(subscription.subscriber) subscription.is_active = False subscription.save() if users_to_notify and post.thread.get_status_display( ) != u'Sunk': send_mail_template( settings.EMAIL_SUBJECT_TOPIC_REPLY, "forum/email_new_post_notification.txt", { 'post': post, 'thread': thread, 'forum': forum }, extra_subject=thread.title, user_to=users_to_notify, email_type_preference_check="new_post") if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message( request, messages.INFO, "Your post won't be shown until it is manually " "approved by moderators") return HttpResponseRedirect(post.thread.get_absolute_url()) else: if quote: form = PostReplyForm(request, quote, {'body': quote}) else: form = PostReplyForm(request, quote) if not user_can_post_in_forum: messages.add_message(request, messages.INFO, user_can_post_message) if user_is_blocked_for_spam_reports: messages.add_message( request, messages.INFO, "You're not allowed to post in the forums because your account " "has been temporaly blocked after multiple spam reports") tvars = { 'forum': forum, 'thread': thread, 'form': form, 'latest_posts': latest_posts } return render(request, 'forum/reply.html', tvars)
def reply(request, forum_name_slug, thread_id, post_id=None): forum = get_object_or_404(Forum, name_slug=forum_name_slug) thread = get_object_or_404(Thread, id=thread_id, forum=forum, first_post__moderation_state="OK") is_survey = False if thread.title == "Freesound Survey": is_survey = True survey_text = """ 1) What do you use Freesound for? (what are your specific interests? what do you do with Freesound samples? ...) 2) Do you perceive some shared goals in Freesounds user community? If so, which ones? (is there a sense of community? and of long-term goals to be achieved? ...) 3) What kinds of sounds are you most interested in? (do you upload and/or download specific types of sounds? which ones? ...) 4) What makes Freesound different from other sound sharing sites? (you can compare with sites like Soundcloud, Looperman, CCMixter or others) """ if post_id: post = get_object_or_404(Post, id=post_id, thread__id=thread_id, thread__forum__name_slug=forum_name_slug) quote = loader.render_to_string('forum/quote_style.html', {'post': post}) else: post = None quote = "" latest_posts = Post.objects.select_related( 'author', 'author__profile', 'thread', 'thread__forum').order_by('-created').filter( thread=thread, moderation_state="OK")[0:15] user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports( ) if request.method == 'POST': form = PostReplyForm(request, quote, request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): mayBeSpam = text_may_be_spam(form.cleaned_data["body"]) if not request.user.post_set.filter( moderation_state="OK").count( ) and mayBeSpam: # first post has urls post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread) add_post_to_solr(post) set_to_moderation = False if form.cleaned_data["subscribe"]: subscription, created = Subscription.objects.get_or_create( thread=thread, subscriber=request.user) if not subscription.is_active: subscription.is_active = True subscription.save() # figure out if there are active subscriptions in this thread if not set_to_moderation: emails_to_notify = [] for subscription in Subscription.objects.filter( thread=thread, is_active=True).exclude(subscriber=request.user): emails_to_notify.append(subscription.subscriber.email) subscription.is_active = False subscription.save() if emails_to_notify: send_mail_template( u"topic reply notification - " + thread.title, "forum/email_new_post_notification.txt", dict(post=post, thread=thread, forum=forum), email_from=None, email_to=emails_to_notify) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message( request, messages.INFO, "Your post won't be shown until it is manually approved by moderators" ) return HttpResponseRedirect(post.thread.get_absolute_url()) else: if quote: form = PostReplyForm(request, quote, {'body': quote}) else: if is_survey: form = PostReplyForm(request, quote, {'body': survey_text}) else: form = PostReplyForm(request, quote) if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message( request, messages.INFO, "You're not allowed to post in the forums because your account has been temporaly blocked after multiple spam reports" ) return render_to_response('forum/reply.html', locals(), context_instance=RequestContext(request))
def reply(request, forum_name_slug, thread_id, post_id=None): forum = get_object_or_404(Forum, name_slug=forum_name_slug) thread = get_object_or_404(Thread, id=thread_id, forum=forum, first_post__moderation_state="OK") if post_id: post = get_object_or_404(Post, id=post_id, thread__id=thread_id, thread__forum__name_slug=forum_name_slug) quote = loader.render_to_string('forum/quote_style.html', {'post': post}) else: post = None quote = "" latest_posts = Post.objects.select_related('author', 'author__profile', 'thread', 'thread__forum')\ .order_by('-created').filter(thread=thread, moderation_state="OK")[0:15] user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports() if request.method == 'POST': form = PostReplyForm(request, quote, request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): may_be_spam = text_may_be_spam(form.cleaned_data.get("body", '')) or \ text_may_be_spam(form.cleaned_data.get("title", '')) if not request.user.posts.filter(moderation_state="OK").count() and may_be_spam: post = Post.objects.create( author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread) add_post_to_solr(post) set_to_moderation = False if form.cleaned_data["subscribe"]: subscription, created = Subscription.objects.get_or_create(thread=thread, subscriber=request.user) if not subscription.is_active: subscription.is_active = True subscription.save() # figure out if there are active subscriptions in this thread if not set_to_moderation: users_to_notify = [] for subscription in Subscription.objects\ .filter(thread=thread, is_active=True).exclude(subscriber=request.user): users_to_notify.append(subscription.subscriber) subscription.is_active = False subscription.save() if users_to_notify and post.thread.get_status_display() != u'Sunk': send_mail_template( u"topic reply notification - " + thread.title, "forum/email_new_post_notification.txt", dict(post=post, thread=thread, forum=forum), user_to=users_to_notify ) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message(request, messages.INFO, "Your post won't be shown until it is manually " "approved by moderators") return HttpResponseRedirect(post.thread.get_absolute_url()) else: if quote: form = PostReplyForm(request, quote, {'body': quote}) else: form = PostReplyForm(request, quote) if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message(request, messages.INFO, "You're not allowed to post in the forums because your account " "has been temporaly blocked after multiple spam reports") tvars = {'forum': forum, 'thread': thread, 'form': form, 'latest_posts': latest_posts} return render(request, 'forum/reply.html', tvars)
def new_thread(request, forum_name_slug): forum = get_object_or_404(Forum, name_slug=forum_name_slug) user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports( ) if request.method == 'POST': form = NewThreadForm(request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): thread = Thread.objects.create( forum=forum, author=request.user, title=form.cleaned_data["title"]) mayBeSpam = False text = form.cleaned_data["body"] if "http://" in form.cleaned_data[ "body"] or "https://" in form.cleaned_data["body"]: mayBeSpam = True if re.search("[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\s|$|\/|\]|\.)", form.cleaned_data["body"]): mayBeSpam = True if not request.user.post_set.all().count() and mayBeSpam: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data['body'], thread=thread) add_post_to_solr(post) set_to_moderation = False # Add first post to thread (this will never be changed) # First we have to "reload" thread object from DB, not so overwrite the object we created before when saving updated_thread = Thread.objects.get(id=thread.id) updated_thread.first_post = post updated_thread.save() if form.cleaned_data["subscribe"]: Subscription.objects.create(subscriber=request.user, thread=thread, is_active=True) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message( request, messages.INFO, "Your post won't be shown until it is manually approved by moderators" ) return HttpResponseRedirect( post.thread.forum.get_absolute_url()) else: form = NewThreadForm() if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message( request, messages.INFO, "You're not allowed to post in the forums because your account has been temporaly blocked after multiple spam reports" ) return render_to_response('forum/new_thread.html', locals(), context_instance=RequestContext(request))
def reply(request, forum_name_slug, thread_id, post_id=None): forum = get_object_or_404(Forum, name_slug=forum_name_slug) thread = get_object_or_404(Thread, id=thread_id, forum=forum, first_post__moderation_state="OK") is_survey = False if thread.title == "Freesound Survey": is_survey = True survey_text = """ 1) What do you use Freesound for? (what are your specific interests? what do you do with Freesound samples? ...) 2) Do you perceive some shared goals in Freesounds user community? If so, which ones? (is there a sense of community? and of long-term goals to be achieved? ...) 3) What kinds of sounds are you most interested in? (do you upload and/or download specific types of sounds? which ones? ...) 4) What makes Freesound different from other sound sharing sites? (you can compare with sites like Soundcloud, Looperman, CCMixter or others) """ if post_id: post = get_object_or_404(Post, id=post_id, thread__id=thread_id, thread__forum__name_slug=forum_name_slug) quote = loader.render_to_string('forum/quote_style.html', {'post':post}) else: post = None quote = "" latest_posts = Post.objects.select_related('author', 'author__profile', 'thread', 'thread__forum').order_by('-created').filter(thread=thread, moderation_state="OK")[0:15] user_can_post_in_forum = request.user.profile.can_post_in_forum() user_is_blocked_for_spam_reports = request.user.profile.is_blocked_for_spam_reports() if request.method == 'POST': form = PostReplyForm(request, quote, request.POST) if user_can_post_in_forum[0] and not user_is_blocked_for_spam_reports: if form.is_valid(): mayBeSpam = text_may_be_spam(form.cleaned_data["body"]) if not request.user.post_set.filter(moderation_state="OK").count() and mayBeSpam: # first post has urls post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread, moderation_state="NM") # DO NOT add the post to solr, only do it when it is moderated set_to_moderation = True else: post = Post.objects.create(author=request.user, body=form.cleaned_data["body"], thread=thread) add_post_to_solr(post) set_to_moderation = False if form.cleaned_data["subscribe"]: subscription, created = Subscription.objects.get_or_create(thread=thread, subscriber=request.user) if not subscription.is_active: subscription.is_active = True subscription.save() # figure out if there are active subscriptions in this thread if not set_to_moderation: emails_to_notify = [] for subscription in Subscription.objects.filter(thread=thread, is_active=True).exclude(subscriber=request.user): emails_to_notify.append(subscription.subscriber.email) subscription.is_active = False subscription.save() if emails_to_notify: send_mail_template(u"topic reply notification - " + thread.title, "forum/email_new_post_notification.txt", dict(post=post, thread=thread, forum=forum), email_from=None, email_to=emails_to_notify) if not set_to_moderation: return HttpResponseRedirect(post.get_absolute_url()) else: messages.add_message(request, messages.INFO, "Your post won't be shown until it is manually approved by moderators") return HttpResponseRedirect(post.thread.get_absolute_url()) else: if quote: form = PostReplyForm(request, quote, {'body':quote}) else: if is_survey: form = PostReplyForm(request, quote, {'body':survey_text}) else: form = PostReplyForm(request, quote) if not user_can_post_in_forum[0]: messages.add_message(request, messages.INFO, user_can_post_in_forum[1]) if user_is_blocked_for_spam_reports: messages.add_message(request, messages.INFO, "You're not allowed to post in the forums because your account has been temporaly blocked after multiple spam reports") return render_to_response('forum/reply.html', locals(), context_instance=RequestContext(request))