Example #1
0
def annotate(request, group, post_id):
    post = Post.objects.get(pk=post_id)
    thread = post.get_thread()
    if not thread.allow_annotating():
        raise PermissionDenied()

    annotation = None
    if post.is_annotated():
        try:
            annotation = post.annotation.get()
        except PostAnnotation.DoesNotExist:
            # Ignore for now ..
            pass

    if request.method == 'POST':
        form = AnnotateForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            if annotation is None:
                annotation = PostAnnotation(post=post, )
            annotation.body = data['body']
            annotation.hide_post = data['hide_post']
            annotation.markup = data['markup']
            annotation.save()
            messages.success(request,
                             message=ugettext(u'Annotated a users post.'))
            return HttpResponseRedirect(post.get_absolute_url())

    else:
        form = AnnotateForm()

    if annotation is not None:
        form.fields['body'].initial = annotation.body
        form.fields['hide_post'].initial = annotation.hide_post
        if 'markup' in form.fields:
            form.fields['markup'].initial = annotation.markup

    return render_to_response("sphene/sphboard/annotate.html", {
        'thread': thread,
        'post': post,
        'form': form,
    },
                              context_instance=RequestContext(request))
Example #2
0
def annotate(request, group, post_id):
    post = Post.objects.get( pk = post_id )
    thread = post.get_thread()
    if not thread.allow_moving():
        raise PermissionDenied()

    annotation = None
    if post.is_annotated():
        try:
            annotation = post.annotation.get()
        except PostAnnotation.DoesNotExist:
            # Ignore for now ..
            pass


    if request.method == 'POST':
        form = AnnotateForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            if annotation is None:
                annotation = PostAnnotation( post = post, )
            annotation.body = data['body']
            annotation.hide_post = data['hide_post']
            annotation.markup = data['markup']
            annotation.save()
            request.user.message_set.create( message = ugettext(u'Annotated a users post.') )
            return HttpResponseRedirect( post.get_absolute_url() )
        
    else:
        form = AnnotateForm()

    if annotation is not None:
        form.fields['body'].initial = annotation.body
        form.fields['hide_post'].initial = annotation.hide_post
        if 'markup' in form.fields:
            form.fields['markup'].initial = annotation.markup

    
    return render_to_response( "sphene/sphboard/annotate.html",
                               { 'thread': thread,
                                 'post': post,
                                 'form': form,
                                 },
                               context_instance = RequestContext(request) )
Example #3
0
def move(request, group, thread_id):
    thread = get_object_or_404(Post, pk = thread_id)
    if not thread.allow_moving():
        raise PermissionDenied()

    annotation = None
    if thread.is_annotated():
        try:
            annotation = thread.annotation.get()
        except PostAnnotation.DoesNotExist:
            # Ignore for now ..
            pass
    if request.method == 'POST':
        form = MoveAndAnnotateForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            newcategory = data['category']

            threadinfo = thread.get_threadinformation()
            threadinfo.thread_type = THREAD_TYPE_MOVED
            threadinfo.save()

            try:
                newthreadinfo = ThreadInformation.objects.get( root_post = thread,
                                                               category = newcategory,
                                                               thread_type = THREAD_TYPE_MOVED )
            except ThreadInformation.DoesNotExist:
                newthreadinfo = ThreadInformation( root_post = thread,
                                                   category = newcategory, )

            newthreadinfo.thread_type = THREAD_TYPE_DEFAULT
            newthreadinfo.update_cache()
            newthreadinfo.save()

            thread.set_annotated(True)

            if annotation is None:
                annotation = PostAnnotation( post = thread, )
            annotation.body = data['body']
            annotation.hide_post = False
            annotation.markup = data['markup']
            annotation.save()

            thread.category = newcategory
            thread.save()

            request.user.message_set.create( message = ugettext(u'Moved thread into new category.') )

            return HttpResponseRedirect( thread.get_absolute_url() )
        
    else:
        form = MoveAndAnnotateForm()

    if POST_MARKUP_CHOICES[0][0] == 'bbcode':
        category_name = '[url=%s]%s[/url].' % (thread.category.get_absolute_url(), thread.category.name)
    else:
        category_name = thread.category.name
    form.fields['body'].initial = _(u'This query was moved from the category %(category_name)s') % {'category_name':category_name}

    return render_to_response( "sphene/sphboard/move.html",
                               { 'thread': thread,
                                 'form': form,
                                 },
                               context_instance = RequestContext(request))
def move_post_3(request, group, post_id, category_id, thread_id=None):
    """
        @post_id - moved post id
        @category_id - target category id
        @thread_id - target thread id (if exists)

        Post might be moved direclty into category - in this case post becomes
        thread or post might be inserted into another thread.
    """
    post = Post.objects.get(pk=post_id)

    # determine if moved post is root post of thread
    thread = post.get_thread()
    is_root_post = thread.pk == post.pk

    target_thread = None
    if thread_id:
        target_thread = get_object_or_404(Post, pk=thread_id)
    
    if not post.allow_moving_post() or thread == target_thread:
        raise PermissionDenied()

    target_category = Category.objects.get(pk=category_id)

    # moved post will be annotated
    annotation = None
    if post.is_annotated():
        try:
            annotation = post.annotation.get()
        except PostAnnotation.DoesNotExist:
            pass
    if request.method == 'POST':
        form = MovePostForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            body = data['body']

            # information about thread from which moved post is taken
            threadinfo = post.get_threadinformation()

            # if moved post is root post of the thread then detach rest of
            # the posts
            if is_root_post:
                posts = Post.objects.filter(thread=post).order_by('postdate')
                # if there are subsequent posts
                if posts.count()>0:
                    # prepare new root_post
                    new_root = posts[0]
                    new_root.thread = None
                    posts.exclude(pk=new_root.pk).update(thread=new_root)
                    new_root.save()
                    # new threadinfo is automatically created by signal while saving Post

                if target_thread:
                    # if root post is moved into another thread then
                    # remove threadinfo
                    threadinfo.delete()
                else:
                    # if post is moved to new category
                    # just update threadinfo
                    threadinfo.category=target_category
                    #threadinfo.update_cache()
                    #threadinfo.save()

            post.category = target_category
            post.thread = target_thread  # this might be None if post was moved into category
            if target_thread:
                # update postdate if necessary to achieve proper ordering (post is always added at the end)
                if target_thread.get_latest_post().postdate>post.postdate:
                    post.postdate = datetime.now()

            if body:
                post.set_annotated(True)
                if annotation is None:
                    annotation = PostAnnotation(post=post)
                annotation.body = body
                annotation.hide_post = False
                annotation.markup = data['markup']
                annotation.save()
            post.save()

            # update information about thread from which post was moved
            threadinfo.update_cache()
            threadinfo.save()

            if target_thread:
                request.user.message_set.create(message=ugettext(u'Post has been appended to thread.'))
            else:
                request.user.message_set.create(message=ugettext(u'Post has been moved into category.'))

            return HttpResponseRedirect(post.get_absolute_url())
    else:
        form = MovePostForm()

    if POST_MARKUP_CHOICES[0][0] == 'bbcode':
        category_name = '[url=%s]%s[/url].' % (post.category.get_absolute_url(),
                                               post.category.name)
        thread_name = '[url=%s]%s[/url].' % (thread.get_absolute_url(),
                                             thread.subject)
    else:
        category_name = post.category.name
        thread_name = thread.subject

    if not is_root_post:
        form.fields['body'].initial = _(u'This post was moved from the thread %(thread_name)s') % {'thread_name':thread_name}
    else:
        form.fields['body'].initial = _(u'This post was moved from the category %(category_name)s') % {'category_name':category_name}

    return render_to_response( "sphene/sphboard/move_post_3.html",
                               { 'thread': thread,
                                 'form': form,
                                 'post':post,
                                 'target_thread':target_thread,
                                 'is_root_post':is_root_post,
                                 'category':target_category
                                 },
                               context_instance = RequestContext(request))
Example #5
0
def move(request, group, thread_id):
    thread = get_object_or_404(Post, pk=thread_id)
    if not thread.allow_moving():
        raise PermissionDenied()

    annotation = None
    if thread.is_annotated():
        try:
            annotation = thread.annotation.get()
        except PostAnnotation.DoesNotExist:
            # Ignore for now ..
            pass
    if request.method == 'POST':
        form = MoveAndAnnotateForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            newcategory = data['category']
            info_link = data['info_link']

            threadinfo = thread.get_threadinformation()

            if info_link:
                threadinfo.thread_type = THREAD_TYPE_MOVED
                threadinfo.save()
            else:
                threadinfo.delete()

            try:
                newthreadinfo = ThreadInformation.objects.get(
                    root_post=thread,
                    category=newcategory,
                    thread_type=THREAD_TYPE_MOVED)
            except ThreadInformation.DoesNotExist:
                newthreadinfo = ThreadInformation(
                    root_post=thread,
                    category=newcategory,
                )

            newthreadinfo.thread_type = THREAD_TYPE_DEFAULT
            newthreadinfo.update_cache()
            newthreadinfo.save()

            thread.set_annotated(True)

            if annotation is None:
                annotation = PostAnnotation(post=thread, )
            annotation.body = data['body']
            annotation.hide_post = False
            annotation.markup = data['markup']
            annotation.save()

            thread.category = newcategory
            thread.save()

            # update category of thread's posts
            thread.replies().update(category=newcategory)

            messages.success(
                request, message=ugettext(u'Moved thread into new category.'))

            return HttpResponseRedirect(thread.get_absolute_url())

    else:
        form = MoveAndAnnotateForm()

    if POST_MARKUP_CHOICES[0][0] == 'bbcode':
        category_name = '[url=%s]%s[/url].' % (
            thread.category.get_absolute_url(), thread.category.name)
    else:
        category_name = thread.category.name
    form.fields['body'].initial = _(
        u'This thread was moved from the category %(category_name)s') % {
            'category_name': category_name
        }

    return render_to_response("sphene/sphboard/move.html", {
        'thread': thread,
        'form': form,
    },
                              context_instance=RequestContext(request))
Example #6
0
def move_post_3(request, group, post_id, category_id, thread_id=None):
    """
        @post_id - moved post id
        @category_id - target category id
        @thread_id - target thread id (if exists)

        Post might be moved direclty into category - in this case post becomes
        thread or post might be inserted into another thread.
    """
    post = Post.objects.get(pk=post_id)

    # determine if moved post is root post of thread
    thread = post.get_thread()
    is_root_post = thread.pk == post.pk

    target_thread = None
    if thread_id:
        target_thread = get_object_or_404(Post, pk=thread_id)

    if not post.allow_moving_post() or thread == target_thread:
        raise PermissionDenied()

    target_category = Category.objects.get(pk=category_id)

    # moved post will be annotated
    annotation = None
    if post.is_annotated():
        try:
            annotation = post.annotation.get()
        except PostAnnotation.DoesNotExist:
            pass
    if request.method == 'POST':
        form = MovePostForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            body = data['body']
            move_all_posts = data['move_all_posts']

            # information about thread from which moved post is taken
            threadinfo = post.get_threadinformation()
            next_posts = None
            if move_all_posts:
                next_posts = Post.objects.filter(
                    thread=post.get_thread(),
                    postdate__gt=post.postdate).order_by('postdate')

            # if moved post is root post of the thread then detach rest of
            # the posts
            if is_root_post:
                posts = Post.objects.filter(thread=post).order_by('postdate')
                # if there are subsequent posts
                if posts.count() > 0 and not move_all_posts:
                    # prepare new root_post
                    new_root = posts[0]
                    new_root.thread = None
                    posts.exclude(pk=new_root.pk).update(thread=new_root)
                    new_root.save()
                    # new threadinfo is automatically created by signal while saving Post

                if target_thread:
                    # if root post is moved into another thread then
                    # remove threadinfo
                    threadinfo.delete()
                    threadinfo = None
                else:
                    # if post is moved to new category
                    # just update threadinfo
                    threadinfo.category = target_category
                    #threadinfo.update_cache()
                    #threadinfo.save()

            post.category = target_category
            post.thread = target_thread  # this might be None if post was moved into category
            if target_thread:
                # update postdate if necessary to achieve proper ordering (post is always added at the end)
                if target_thread.get_latest_post().postdate > post.postdate:
                    post.postdate = timezone.now()

            if body:
                post.set_annotated(True)
                if annotation is None:
                    annotation = PostAnnotation(post=post)
                annotation.body = body
                annotation.hide_post = False
                annotation.markup = data['markup']
                annotation.save()
            post.save()

            if not next_posts is None:
                # if posts were moved to new thread update postdate to place them at the end
                if target_thread:
                    cnt = 1
                    for p in next_posts:
                        p.thread = post.get_thread()
                        if post.postdate > p.postdate:
                            p.postdate = timezone.now() + timedelta(
                                microseconds=cnt)
                        p.save()
                        cnt += 1
                elif not is_root_post:  # posts moved to category
                    next_posts.update(thread=post.get_thread())
                    # clear caches
                    for p in next_posts:
                        cache.delete(p._cache_key_absolute_url())

            # update information about thread from which post was moved
            if threadinfo:
                threadinfo.update_cache()
                threadinfo.save()

            if target_thread:
                messages.success(
                    request,
                    message=ugettext(u'Post has been appended to thread.'))
            else:
                messages.success(
                    request,
                    message=ugettext(u'Post has been moved into category.'))

            return HttpResponseRedirect(post.get_absolute_url())
    else:
        form = MovePostForm()

    if POST_MARKUP_CHOICES[0][0] == 'bbcode':
        category_name = '[url=%s]%s[/url].' % (
            post.category.get_absolute_url(), post.category.name)
        thread_name = '[url=%s]%s[/url].' % (thread.get_absolute_url(),
                                             thread.subject)
    else:
        category_name = post.category.name
        thread_name = thread.subject

    if not is_root_post:
        form.fields['body'].initial = _(
            u'This post was moved from the thread %(thread_name)s') % {
                'thread_name': thread_name
            }
    else:
        form.fields['body'].initial = _(
            u'This post was moved from the category %(category_name)s') % {
                'category_name': category_name
            }

    return render_to_response("sphene/sphboard/move_post_3.html", {
        'thread': thread,
        'form': form,
        'post': post,
        'target_thread': target_thread,
        'is_root_post': is_root_post,
        'category': target_category
    },
                              context_instance=RequestContext(request))