예제 #1
0
def post_reply(request, topic_id):
    form = ReplyForm()
    topic = Topic.objects.get(pk=topic_id)

    if request.method == 'POST':
        form = PostForm(request.POST)

        if form.is_valid():

            post = Post()
            post.topic = topic
            #post.title = form.cleaned_data['title']
            post.body = form.cleaned_data['body']
            post.creator = request.user
            post.user_ip = request.META['REMOTE_ADDR']

            post.save()

            return HttpResponseRedirect(
                reverse('forum:topic-detail', args=(topic.id, )))

    return render(request, 'forum/reply.html', {
        'form': form,
        'topic': topic,
    })
예제 #2
0
def reply(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect("%s?next=%s" % (LOGIN_URL, request.path))
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            body = form.cleaned_data["body"]
            p = Post(thread=t, author=request.user, body=body, time=datetime.now())
            p.save()

            # Send notifications (if installed)
            if notification:
                notification.send(
                    User.objects.filter(forum_post_set__thread=t).distinct(),
                    "forum_new_reply",
                    {"post": p, "thread": t, "site": Site.objects.get_current()},
                )

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()

    return render_to_response(
        "forum/reply.html", RequestContext(request, {"form": form, "forum": t.forum, "thread": t})
    )
예제 #3
0
def reply(request, thread):
    """
    回复帖子子
    条件:
    1、帖子允许回复,没有关闭
    2、当前用户登录
    """
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return Http404
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return Http404

    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t, 
                author=request.user,
                body=body,
                time=datetime.now(),
                )
            p.save()

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()

    return render_to_response('forum/reply.html',
                              RequestContext(request, {'form': form,
                                                       'forum': t.forum,
                                                       'thread': t,
                                                       }))
예제 #4
0
def thread(request, thread):
    """
    Increments the viewed count on a thread then displays the 
    posts for that thread, in chronological order.
    """
    t = get_object_or_404(Thread, pk=thread)
    p = t.post_set.all().order_by('time')
    s = t.subscription_set.filter(author=request.user)

    # If the user is not authorized to view, we redirect them
    if t.forum.is_private and request.user.is_staff != True:
         return HttpResponseRedirect('/forum')

    # Process reply form if it was sent
    if (request.method == 'POST'):
        if not request.user.is_authenticated() or t.closed:
            return HttpResponseServerError()
        reply_form = ReplyForm(request.POST)
        if reply_form.is_valid():
            new_post = reply_form.save(commit = False)
            new_post.author = request.user
            new_post.thread = t
            new_post.time=datetime.now()
            new_post.save()
            # Change subscription
            if reply_form.cleaned_data['subscribe']:
                Subscription.objects.get_or_create(thread=t,
                    author=request.user)
            else:
                Subscription.objects.filter(thread=t, author=request.user).delete()
            # Send email
            forum_email_notification(new_post)
            return HttpResponseRedirect(new_post.get_absolute_url())
    else:
        reply_form = ReplyForm(initial={'subscribe': s})

    # Pagination
    paginator = Paginator(p, settings.FORUM_PAGINATE)
    page = int(request.GET.get('page', paginator.num_pages))
    try:
        posts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        posts = paginator.page(paginator.num_pages)

    t.views += 1
    t.save()
    #{'object_list' : artistic.object_list, 'page_range' : paginator.page_range, 'page' : page, 'letter' : letter, 'al': alphalist}, \
    return render_to_response('forum/thread.html',
        RequestContext(request, {
            'forum': t.forum,
            'thread': t,
            'posts': posts.object_list,
	    'page_range': paginator.page_range,
            'page': page,
            'reply_form': reply_form
        }))
예제 #5
0
파일: views.py 프로젝트: niutool/niuforum
 def post(self, request, *args, **kwargs):
     topic_id = self.kwargs['topic_id']
     topic = get_object_or_404(Topic, id=topic_id)
     form = ReplyForm(self.request.POST)
     if form.is_valid():
         md = form.cleaned_data['content']
         rendered = render_markdown(md)
         reply = Reply(topic=topic, author=request.user, markdown=md, content=rendered)
         
         mentioned = get_metioned_user(request.user, md)
         self._commit_changes(topic, reply, mentioned)
     
     return HttpResponseRedirect(reverse('topic_view', kwargs={'topic_id':topic_id}))
예제 #6
0
    def post(self, request, *args, **kwargs):
        topic_id = self.kwargs['topic_id']
        topic = get_object_or_404(Topic, id=topic_id)
        form = ReplyForm(self.request.POST)
        if form.is_valid():
            md = form.cleaned_data['content']
            rendered = render_markdown(md)
            reply = Reply(topic=topic, author=request.user, markdown=md, content=rendered)

            mentioned = get_metioned_user(request.user, md)
            self._commit_changes(topic, reply, mentioned)

        return HttpResponseRedirect(reverse('topic_view', kwargs={'topic_id': topic_id}))
예제 #7
0
def submit_reply(request, slug):
    parent = Post.objects.get(slug = slug)

    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            reply = form.save(commit = False)
            reply.parent = parent
            #add number of parent children to not clash titles
            reply.title = 'Re('+ str(len(parent.children.all())) + '): ' + parent.title 
            if request.user.is_authenticated():
                reply.author = request.user            
            reply.save()            
        return HttpResponseRedirect('/forum/post/' + parent.slug + '/')

    else:
        form = ReplyForm()
    return render(request, 'forum/submit_reply.html',{'form': form,
                                                     'slug': parent.slug})
예제 #8
0
def reply(request, thread, extra_context=None):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    preview = False
    p = None

    if request.method == "POST":
        form = ReplyForm(request.POST)
        preview = request.POST.get('preview')
        p = Post(
            thread=t, 
            author=request.user,
            time=datetime.now(),
            )
        if form.is_valid():
            p.body = form.cleaned_data['body']
            
            if not preview:
                p.save()

            if not preview:
                return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()
    
    return render_to_response('forum/reply.html',
        RequestContext(request, {
            'form': form,
            'forum': t.forum,
            'post': p,
            'preview': preview,
            'thread': t,
        }))
예제 #9
0
def reply(request, thread):
    """
    回复帖子子
    条件:
    1、帖子允许回复,没有关闭
    2、当前用户登录
    """
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return Http404
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return Http404

    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t,
                author=request.user,
                body=body,
                time=datetime.now(),
            )
            p.save()

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()

    return render_to_response(
        'forum/reply.html',
        RequestContext(request, {
            'form': form,
            'forum': t.forum,
            'thread': t,
        }))
예제 #10
0
def reply(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseServerError()
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t, 
                author=request.user,
                body=body,
                time=datetime.now(),
                )
            p.save()

            sub = Subscription.objects.filter(thread=t, author=request.user)
            if form.cleaned_data.get('subscribe',False):
                if not sub:
                    s = Subscription(
                        author=request.user,
                        thread=t
                        )
                    s.save()
            else:
                if sub:
                    sub.delete()

            # Subscriptions are updated now send mail to all the authors subscribed in
            # this thread.
            mail_subject = ''
            try:
                mail_subject = settings.FORUM_MAIL_PREFIX 
            except AttributeError:
                mail_subject = '[Forum]'

            mail_from = ''
            try:
                mail_from = settings.FORUM_MAIL_FROM
            except AttributeError:
                mail_from = settings.DEFAULT_FROM_EMAIL

            mail_tpl = loader.get_template('forum/notify.txt')
            c = Context({
                'body': wordwrap(striptags(body), 72),
                'site' : Site.objects.get_current(),
                'thread': t,
                })

            email = EmailMessage(
                    subject=mail_subject+' '+striptags(t.title),
                    body= mail_tpl.render(c),
                    from_email=mail_from,
                    to=[mail_from],
                    bcc=[s.author.email for s in t.subscription_set.all()],)
            email.send(fail_silently=True)

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()
    
    return render_to_response('forum/reply.html',
        RequestContext(request, {
            'form': form,
            'forum': t.forum,
            'thread': t,
        }))
예제 #11
0
def reply(request, thread, flag_partner=False):
	"""
	If a thread isn't closed, and the user is logged in, post a reply
	to a thread. Note we don't have "nested" replies at this stage.
	"""
	if flag_partner:
		if request.user.is_authenticated() and request.user.get_profile().is_partner():pass
		else:
			messages.add_message(request, messages.ERROR, 'Пожалуйста, авторизируйтесь как партнер.')
			return redirect('/accounts/login/?next=%s' % request.path)
			
	if not request.user.is_authenticated():
		return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path))
	t = get_object_or_404(Thread, pk=thread)
	if t.closed:
		return HttpResponseServerError()
	if not Forum.objects.has_access(t.forum, request.user.groups.all()):
		return HttpResponseForbidden()

	if request.method == "POST":
		form = ReplyForm(request.POST)
		formset = AttachFileFormset(request.POST, request.FILES)
		if form.is_valid() and formset.is_valid():
			p = Post(
				thread=t,
				author=request.user,
				body=form.cleaned_data['body'],
				time=datetime.now(),
			)
			p.save()
			
			formset.instance = p
			formset.save()

			sub = Subscription.objects.filter(thread=t, author=request.user)
			if form.cleaned_data.get('subscribe',False):
				if not sub:
					s = Subscription(author=request.user, thread=t)
					s.save()
			else:
				if sub: sub.delete()

			if t.subscription_set.count() > 0:
				mail_subject = ''
				try: mail_subject = settings.FORUM_MAIL_PREFIX 
				except AttributeError: mail_subject = '[Forum]'

				mail_from = ''
				try: mail_from = settings.FORUM_MAIL_FROM
				except AttributeError: mail_from = settings.DEFAULT_FROM_EMAIL

				mail_tpl = loader.get_template('forum/notify.txt')
				body = form.cleaned_data['body']
				c = Context({
					'body': wordwrap(striptags(body), 72),
					'site' : Site.objects.get_current(),
					'thread': t,
				})

				email = EmailMessage(
					subject=mail_subject+' '+striptags(t.title),
					body= mail_tpl.render(c),
					from_email=mail_from,
					bcc=[s.author.email for s in t.subscription_set.all()],)
				email.send(fail_silently=True)

			return HttpResponseRedirect(p.get_absolute_url())
	else:
		form = ReplyForm()
		formset = AttachFileFormset()
    
	return render_to_response('forum/reply.html',
		RequestContext(request, {
			'form': form,
			'formset': formset,
			'forum': t.forum,
			'thread': t,
			'flag_partner':flag_partner
		}))
예제 #12
0
파일: views.py 프로젝트: cknave/demovibes
def thread(request, thread):
    """
    Increments the viewed count on a thread then displays the
    posts for that thread, in chronological order.
    """
    t = get_object_or_404(Thread, pk=thread)
    p = t.post_set.all().order_by('time')
    if request.user.is_authenticated():
        s = t.subscription_set.filter(author=request.user)
    else:
        s = False

    # If the user is not authorized to view, we redirect them
    if t.forum.is_private and request.user.is_staff != True:
        return HttpResponseRedirect('/forum')

    # Process reply form if it was sent
    if (request.method == 'POST'):
        if not request.user.is_authenticated() or t.closed:
            return HttpResponseServerError()
        reply_form = ReplyForm(request.POST)
        if reply_form.is_valid():
            new_post = reply_form.save(commit=False)
            new_post.author = request.user
            new_post.thread = t
            new_post.time = datetime.now()
            new_post.save()
            # Change subscription
            if reply_form.cleaned_data['subscribe']:
                Subscription.objects.get_or_create(thread=t,
                                                   author=request.user)
            else:
                Subscription.objects.filter(thread=t,
                                            author=request.user).delete()
            # Send email
            forum_email_notification(new_post)
            return HttpResponseRedirect(new_post.get_absolute_url())
    else:
        reply_form = ReplyForm(initial={'subscribe': s})

    # Pagination
    paginator = Paginator(p, settings.FORUM_PAGINATE)
    try:
        page = int(request.GET.get('page', paginator.num_pages))
    except:
        page = paginator.num_pages

    try:
        posts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        posts = paginator.page(paginator.num_pages)

    t.views += 1
    t.save()
    #{'object_list' : artistic.object_list, 'page_range' : paginator.page_range, 'page' : page, 'letter' : letter, 'al': alphalist}, \
    return j2shim.r2r(
        'forum/thread.html', {
            'forum': t.forum,
            'thread': t,
            'posts': posts.object_list,
            'page_range': paginator.page_range,
            'page': page,
            'reply_form': reply_form
        }, request)
def reply(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path))
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t,
                author=request.user,
                body=body,
                time=datetime.now(),
            )
            p.save()

            sub = Subscription.objects.filter(thread=t, author=request.user)
            if form.cleaned_data.get('subscribe', False):
                if not sub:
                    s = Subscription(author=request.user, thread=t)
                    s.save()
            else:
                if sub:
                    sub.delete()

            if t.subscription_set.count() > 0:
                # Subscriptions are updated now send mail to all the authors subscribed in
                # this thread.
                mail_subject = ''
                try:
                    mail_subject = settings.FORUM_MAIL_PREFIX
                except AttributeError:
                    mail_subject = '[Forum]'

                mail_from = ''
                try:
                    mail_from = settings.FORUM_MAIL_FROM
                except AttributeError:
                    mail_from = settings.DEFAULT_FROM_EMAIL

                mail_tpl = loader.get_template('forum/notify.txt')
                c = Context({
                    'body': wordwrap(striptags(body), 72),
                    'site': Site.objects.get_current(),
                    'thread': t,
                })

                email = EmailMessage(
                    subject=mail_subject + ' ' + striptags(t.title),
                    body=mail_tpl.render(c),
                    from_email=mail_from,
                    bcc=[s.author.email for s in t.subscription_set.all()],
                )
                email.send(fail_silently=True)

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()

    return render_to_response(
        'forum/reply.html',
        RequestContext(request, {
            'form': form,
            'forum': t.forum,
            'thread': t,
        }))
예제 #14
0
def reply(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseServerError()
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t,
                author=request.user,
                body=body,
                time=datetime.now(),
                )
            p.save()

            sub = Subscription.objects.filter(thread=t, author=request.user)
            if form.cleaned_data.get('subscribe',False):
                if not sub:
                    s = Subscription(
                        author=request.user,
                        thread=t
                        )
                    s.save()
            else:
                if sub:
                    sub.delete()

            # Subscriptions are updated now send mail to all the authors subscribed in
            # this thread.
            mail_subject = ''
            try:
                mail_subject = settings.FORUM_MAIL_PREFIX
            except AttributeError:
                mail_subject = '[Forum]'

            mail_from = ''
            try:
                mail_from = settings.FORUM_MAIL_FROM
            except AttributeError:
                mail_from = settings.DEFAULT_FROM_EMAIL

            mail_tpl = loader.get_template('forum/notify.txt')
            c = Context({
                'body': wordwrap(striptags(body), 72),
                'site' : Site.objects.get_current(),
                'thread': t,
                })

            email = EmailMessage(
                    subject=mail_subject+' '+striptags(t.title),
                    body= mail_tpl.render(c),
                    from_email=mail_from,
                    to=[mail_from],
                    bcc=[s.author.email for s in t.subscription_set.all()],)
            email.send(fail_silently=True)

##            data={'title': t.title,
##                'summary': t.title,
##                'attached_timestamp':datetime.now()
##                }
##            attachment_form=AttachmentForm(data=data,files=form.files)
##            a=attachment_form.errors
##            content_type =ContentType.objects.get_for_model(Post)
##            object_id = p.id
##            ffs=form.files
##            debug()
##            if attachment_form.is_valid():
##                attachment = attachment_form.save(commit=False)
##                attachment.content_type = content_type
##                attachment.object_id = object_id
##                attachment.attached_by = request.user
##                attachment.save()

#            for attachedfilefield in form.files:
#                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
#                attachment_file = form.files[attachedfilefield]
#                file_path =os.path.join(ATTACHMENT_DIR, randomfilename(attachment_file.name))
#                (mimetype, encoding) = mimetypes.guess_type(file_path)
#
#                try:
#                    mime_type = mimetype
#                except:
#                    mime_type = 'text/plain'
#
#                attach=Attachment(
#                    content_type =ContentType.objects.get_for_model(Post),
#                    object_id = p.id,
#                    title = attachment_file.name,
#                    summary = t.title,
#                    attached_by = request.user,
#                    )
#                attach.save_uploaded_file(attachment_file)
#                attach.save()


            for attachedfilefield in form.files:
                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
                attachment_file = form.files[attachedfilefield]
                attach=Attachment()
                attach.handle_uploaded_attachment(p,
                    attachment_file,
                    request.user,
                    attachment_file.name,
                    t.title
                    )

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()

    return render_to_response('forum/reply.html',
        RequestContext(request, {
            'form': form,
            'forum': t.forum,
            'thread': t,
        }))
예제 #15
0
파일: views.py 프로젝트: zeograd/demovibes
def thread(request, thread):
    """
    Increments the viewed count on a thread then displays the
    posts for that thread, in chronological order.
    """
    t = get_object_or_404(Thread, pk=thread)
    p = t.post_set.all().order_by('time')
    if request.user.is_authenticated():
        s = t.subscription_set.filter(author=request.user)
    else:
        s = False

    # If the user is not authorized to view, we redirect them
    if t.forum.is_private and request.user.is_staff != True:
         return HttpResponseRedirect('/forum')

    # Process reply form if it was sent
    if (request.method == 'POST'):
        if not request.user.is_authenticated() or t.closed:
            return HttpResponseServerError()

        r = check_muted(request)
        if r: return r

        reply_form = ReplyForm(request.POST)
        if reply_form.is_valid():
            new_post = reply_form.save(commit = False)
            new_post.author = request.user
            new_post.thread = t
            new_post.time=datetime.now()
            new_post.save()
            # Change subscription
            if reply_form.cleaned_data['subscribe']:
                Subscription.objects.get_or_create(thread=t,
                    author=request.user)
            else:
                Subscription.objects.filter(thread=t, author=request.user).delete()
            # Send email
            
            if new_post.is_visible(None):
                forum_email_notification(new_post)
                if NOTIFY_POST and not t.forum.is_private:
                    wm.send_notification("%s posted a reply to \"<a href='%s#post%s'>%s</a>\" in forum \"%s\"" % (
                        escape(request.user.username),
                        new_post.thread.get_absolute_url(),
                        new_post.id,
                        escape(new_post.thread.title),
                        escape(new_post.thread.forum.title),
                    ), None, 1)
            return HttpResponseRedirect(new_post.get_absolute_url())
    else:
        reply_form = ReplyForm(initial={'subscribe': s})

    # Pagination
    paginator = Paginator(p, settings.FORUM_PAGINATE)
    try:
        page = int(request.GET.get('page', paginator.num_pages))
    except:
        page = paginator.num_pages

    try:
        posts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        posts = paginator.page(paginator.num_pages)

    t.views += 1
    t.save()
    #{'object_list' : artistic.object_list, 'page_range' : paginator.page_range, 'page' : page, 'letter' : letter, 'al': alphalist}, \
    return j2shim.r2r('forum/thread.html',
            {
            'forum': t.forum,
            'thread': t,
            'posts': posts.object_list,
            'page_range': paginator.page_range,
            'page': page,
            'reply_form': reply_form
        }, request)
예제 #16
0
파일: views.py 프로젝트: silky/mldata
def reply(request, thread):
    """Post a reply.

    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.

    @param thread: thread id to reply to
    @type thread: integer
    @return: a view to post a reply
    @rtype: Django response
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect("%s?next=%s" % (reverse("user_signin"), request.path))
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            if request.POST.has_key("preview"):
                preview = {"body": form.cleaned_data["body"]}
            else:
                body = form.cleaned_data["body"]
                p = Post(thread=t, author=request.user, body=body, time=datetime.now())
                p.save()

                sub = Subscription.objects.filter(thread=t, author=request.user)
                if form.cleaned_data.get("subscribe", False):
                    if not sub:
                        s = Subscription(author=request.user, thread=t)
                        s.save()
                else:
                    if sub:
                        sub.delete()

                if t.subscription_set.count() > 0:
                    # Subscriptions are updated now send mail to all the authors subscribed in
                    # this thread.
                    mail_subject = ""
                    try:
                        mail_subject = settings.FORUM_MAIL_PREFIX
                    except AttributeError:
                        mail_subject = "[Forum]"

                    mail_from = ""
                    try:
                        mail_from = settings.FORUM_MAIL_FROM
                    except AttributeError:
                        mail_from = settings.DEFAULT_FROM_EMAIL

                    mail_tpl = loader.get_template("forum/notify.txt")
                    c = RequestContext(
                        {"body": wordwrap(striptags(body), 72), "site": Site.objects.get_current(), "thread": t}
                    )

                    email = EmailMessage(
                        subject=mail_subject + " " + striptags(t.title),
                        body=mail_tpl.render(c),
                        from_email=mail_from,
                        bcc=[s.author.email for s in t.subscription_set.all()],
                    )
                    email.send(fail_silently=True)

                return HttpResponseRedirect(p.get_absolute_url())
    else:
        preview = False
        form = ReplyForm()

    return render_to_response(
        "forum/reply.html",
        RequestContext(request, {"form": form, "forum": t.forum, "thread": t, "preview": preview, "section": "forum"}),
    )
예제 #17
0
def edit(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseServerError()
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t,
                author=request.user,
                body=body,
                time=datetime.now(),
                )
            p.save()

            sub = Subscription.objects.filter(thread=t, author=request.user)
            if form.cleaned_data.get('subscribe',False):
                if not sub:
                    s = Subscription(
                        author=request.user,
                        thread=t
                        )
                    s.save()
            else:
                if sub:
                    sub.delete()

            # Subscriptions are updated now send mail to all the authors subscribed in
            # this thread.
            mail_subject = ''
            try:
                mail_subject = settings.FORUM_MAIL_PREFIX
            except AttributeError:
                mail_subject = '[Forum]'

            mail_from = ''
            try:
                mail_from = settings.FORUM_MAIL_FROM
            except AttributeError:
                mail_from = settings.DEFAULT_FROM_EMAIL

            mail_tpl = loader.get_template('forum/notify.txt')
            c = Context({
                'body': wordwrap(striptags(body), 72),
                'site' : Site.objects.get_current(),
                'thread': t,
                })

            email = EmailMessage(
                    subject=mail_subject+' '+striptags(t.title),
                    body= mail_tpl.render(c),
                    from_email=mail_from,
                    to=[mail_from],
                    bcc=[s.author.email for s in t.subscription_set.all()],)
            email.send(fail_silently=True)

            for attachedfilefield in form.files:
                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
                attachment_file = form.files[attachedfilefield]
                attach=Attachment()
                attach.handle_uploaded_attachment(p,
                    attachment_file,
                    request.user,
                    attachment_file.name,
                    t.title
                    )

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        post_id = request.GET.get('pid', 0)
        if post_id ==0:
            return HttpResponseServerError(_("The Post Do Not Exist"))
        try:
            p = Post.objects.all().get(id=post_id)
        except Post.DoesNotExist:
            raise Http404(_("Not Found"))
        
        body=p.body
        initial = {'subscribe': True,
            'body':body}
        form = ReplyForm(initial=initial)

    return render_to_response('forum/edit.html',
        RequestContext(request, {
            'form': form,
            'post': p
        }))