Ejemplo n.º 1
0
def forum(request, slug):
    """
    Displays a list of threads within a forum.
    Threads are sorted by their sticky flag, followed by their
    most recent post.
    """
    f = get_object_or_404(Forum, slug=slug)

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

    # Process new thread form if data was sent
    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseServerError()
        thread_form = ThreadForm(request.POST)

        r = check_muted(request)
        if r: return r

        if thread_form.is_valid():
            new_thread = thread_form.save(commit = False)
            new_thread.forum = f
            new_thread.save()
            
            Post.objects.create(thread=new_thread, author=request.user,
                body=thread_form.cleaned_data['body'],
                time=datetime.now())
            
            if new_thread.is_visible(None):
                if NOTIFY_THREAD and not f.is_private:
                    wm.send_notification("%s created a new thread \"<a href='%s'>%s</a>\" in forum \"%s\"" % (
                        escape(request.user.username),
                        new_thread.get_absolute_url(),
                        escape(new_thread.title),
                        escape(new_thread.forum.title),
                    ), None, 1)
            
            if (thread_form.cleaned_data['subscribe'] == True):
                Subscription.objects.create(author=request.user,
                    thread=new_thread)
            return HttpResponseRedirect(new_thread.get_absolute_url())
    else:
        thread_form = ThreadForm()

    # Pagination
    t = f.thread_set.all()
    paginator = Paginator(t, settings.FORUM_PAGINATE)
    try:
        page = int(request.GET.get('page', 1))
    except:
        page = 1
    try:
        threads = paginator.page(page)
    except (EmptyPage, InvalidPage):
        threads = paginator.page(paginator.num_pages)

    return j2shim.r2r('forum/thread_list.html',
        {
            'forum': f,
            'threads': threads.object_list,
            'page_range': paginator.page_range,
            'page': page,
            'thread_form': thread_form
        }, request)
Ejemplo n.º 2
0
def forum(request, slug):
    """
    Displays a list of threads within a forum.
    Threads are sorted by their sticky flag, followed by their
    most recent post.
    """
    f = get_object_or_404(Forum, slug=slug)

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

    # Process new thread form if data was sent
    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseServerError()
        thread_form = ThreadForm(request.POST)

        r = check_muted(request)
        if r: return r

        if thread_form.is_valid():
            new_thread = thread_form.save(commit=False)
            new_thread.forum = f
            new_thread.save()
            if NOTIFY_THREAD and not f.is_private:
                wm.send_notification(
                    "%s created a new thread \"<a href='%s'>%s</a>\" in forum \"%s\""
                    % (
                        escape(request.user.username),
                        new_thread.get_absolute_url(),
                        escape(new_thread.title),
                        escape(new_thread.forum.title),
                    ), None, 1)
            Post.objects.create(thread=new_thread,
                                author=request.user,
                                body=thread_form.cleaned_data['body'],
                                time=datetime.now())
            if (thread_form.cleaned_data['subscribe'] == True):
                Subscription.objects.create(author=request.user,
                                            thread=new_thread)
            return HttpResponseRedirect(new_thread.get_absolute_url())
    else:
        thread_form = ThreadForm()

    # Pagination
    t = f.thread_set.all()
    paginator = Paginator(t, settings.FORUM_PAGINATE)
    try:
        page = int(request.GET.get('page', 1))
    except:
        page = 1
    try:
        threads = paginator.page(page)
    except (EmptyPage, InvalidPage):
        threads = paginator.page(paginator.num_pages)

    return j2shim.r2r(
        'forum/thread_list.html', {
            'forum': f,
            'threads': threads.object_list,
            'page_range': paginator.page_range,
            'page': page,
            'thread_form': thread_form
        }, request)
Ejemplo n.º 3
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')
    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)
Ejemplo n.º 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')
    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
            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)