def message_edit_view(request, message_id): try: message = Message.objects.get(pk=message_id) except Message.DoesNotExist: return unknown_message(request) thread = message.thread if not message.author == request.user: return insufficient_permission(request) form = PostReplyForm(initial={'content': message.content}) if request.method == 'POST': form = PostReplyForm(request.POST) if form.is_valid(): content = form.cleaned_data['content'] message.content = content message.date_edited = timezone.now() message.save() return redirect(thread_view, thread_title=thread.title) return render(request, 'forum/message-edit.html', { 'form': form, 'thread': thread, 'message': message })
def thread_post_view(request, thread_title): try: thread = Thread.objects.get(title=thread_title) except Thread.DoesNotExist: return unknown_thread(request) if thread.locked and not request.user.has_perm("locked_thread_reply"): return insufficient_permission(request) form = PostReplyForm() if request.method == 'POST': form = PostReplyForm(request.POST) if form.is_valid(): content = form.cleaned_data['content'] message = Message.objects.create(thread=thread, content=content, author=request.user) message.save() mentioned = [] # Loop thru all the words and check if any user is mentioned pattern = re.compile("@[A-z0-9_]+") for match in pattern.findall(message.content): try: user = User.objects.get(username=match.replace("@", "")) Alert.objects.create(user=user, type=Alert.MENTION, caused_by=request.user, thread=thread) mentioned.append(user) except User.DoesNotExist: continue # Send an alert to all the participants (if already mentioned skip) for participant in thread.get_participants(): if participant == request.user or participant in mentioned: continue Alert.objects.create(user=participant, type=Alert.RESPOND, caused_by=request.user, thread=thread) message.author.userprofile.check_add_achievements( Achievement.POST_COUNT) return redirect(thread_view, thread_title=thread.title) return render(request, 'forum/thread.html', { 'form': form, 'thread': thread, })
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(): post.body = form.cleaned_data['body'] post.save() 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_to_response('forum/post_edit.html', locals(), context_instance=RequestContext(request)) 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 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(): post.body = remove_control_chars(form.cleaned_data['body']) post.save() add_post_to_solr(post.id) # 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 thread_view(request, thread_title): try: thread = Thread.objects.get(title=thread_title) except Thread.DoesNotExist: return unknown_thread(request) paginator = Paginator(thread.get_messages(), 10) page = paginator.get_page(request.GET.get('page', 1)) return render(request, 'forum/thread.html', { 'thread': thread, 'page': page, 'form': PostReplyForm() })
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 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))