Пример #1
0
def base_create_thread(request, forum, template):
    """
    Allows users to create a thread
    """
    if request.method == "POST":
        form = ThreadForm(request.POST)
        if form.is_valid():
            thread = form.save(commit=False)
            thread.forum = forum
            if request.user.is_authenticated():
                thread.author = request.user
            thread.ip_address = request.META.get("REMOTE_ADDR", "")
            thread.last_post = thread
            thread.save()

            # tell others who are interested that a thread has been created
            signals.thread_created.send(sender=Thread, instance=thread, request=request)

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

    data = {"forum": forum, "form": form}

    return render(request, template, data)
Пример #2
0
def show_post(request, path, post_id, template="vcboard/thread_detail.html"):
    """
    Allows users to view threads
    """
    data = {}

    return render(request, template, data)
Пример #3
0
def base_post_reply(request, forum, thread, template):
    """
    Does the work that allows a user to reply to a thread
    """
    if request.method == "POST":
        form = ReplyForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.parent = thread

            if request.user.is_authenticated():
                post.author = request.user
            else:
                post.is_draft = False

            post.ip_address = request.META.get("REMOTE_ADDR", "127.0.0.1")
            post.save()

            return HttpResponseRedirect(thread.get_absolute_url())
    else:
        subject = thread.subject
        if not subject.startswith("Re: "):
            subject = "Re: " + subject

        content = ""
        quoting = int(request.GET.get("quoting", 0))
        if quoting:
            post = Post.objects.valid().get(pk=quoting)
            content = '[quote id="%i"]%s[/quote]' % (post.id, post.content)

        form = ReplyForm(initial={"subject": subject, "content": content})

    data = {"form": form, "forum": forum, "thread": thread}
    return render(request, template, data)
Пример #4
0
def base_edit_thread(request, forum, thread, template):
    """
    Does the work that allows users to edit threads
    """
    data = {}

    return render(request, template, data)
Пример #5
0
def user_profile(request, username=None, template="vcboard/user_profile.html"):
    """
    Displays a user's profile
    """
    if not username:
        user = request.user
    else:
        user = get_object_or_404(User, username=username)

    data = {"user": user}

    return render(request, template, data)
Пример #6
0
def base_move_thread(request, forum, thread, template):
    """
    Does the work to move a thread
    """

    # find all forums in which this user is allowed to start threads
    postable = Forum.objects.active().exclude(pk=forum.id)
    postable = postable.filter(is_category=False)
    valid = []
    error = None
    for f in postable:
        perms = get_user_permissions(request.user, f)
        if perms["start_threads"]:
            valid.append(f)

    if request.method == "POST":
        forum_id = int(request.POST.get("move_to_forum", 0))
        f = Forum.objects.get(pk=forum_id)
        if f in postable:
            # reduce counts
            for p in thread.forum.hierarchy:
                p.thread_count -= 1
                p.post_count -= thread.posts.count() - 1
                p.save()

                # TODO: look into updating the last post if the moving thread
                # was the last post in this forum

            # move the thread
            thread.forum = f
            thread.save()

            # update counts again
            for n in thread.forum.hierarchy:
                n.thread_count += 1
                n.post_count += thread.posts.count() + 1

                if not n.last_post or n.last_post.date_created < thread.date_created:
                    n.last_post = thread

                n.save()

            return HttpResponseRedirect(thread.get_absolute_url())
        else:
            error = _("Invalid forum!")

    data = {"forum": forum, "thread": thread, "valid_forums": valid, "error": error}

    return render(request, template, data)
Пример #7
0
def base_show_thread(request, forum, thread, page, template):
    """
    Does the work that allows users to view a thread
    """
    paginator = Paginator(thread.posts.valid(), config("thread", "posts_per_page", int, 20))
    page_obj = paginator.page(page)

    # TODO: make the view count increment intelligently
    thread.view_count += 1
    thread.save()

    data = {"forum": forum, "thread": thread, "paginator": paginator, "page": page_obj}
    signals.object_shown.send(sender=Thread, instance=thread, request=request)

    return render(request, template, data)
Пример #8
0
def show_forum(request, path, page=1, template="vcboard/forum_detail.html"):
    """
    Displays a forum with its subforums and topics, if any
    """
    forum = Forum.objects.with_path(path)
    if not forum:
        raise Http404

    threads_per_page = forum.threads_per_page
    if threads_per_page == 0:
        threads_per_page = config("forum", "threads_per_page", int, 20)
    paginator = Paginator(forum.threads.all(), threads_per_page)
    page_obj = paginator.page(page)

    data = {"forum": forum, "paginator": paginator, "page": page_obj}

    signals.object_shown.send(sender=Forum, instance=forum, request=request)

    return render(request, template, data)
Пример #9
0
def edit_post(request, path, post_id, template="vcboard/post_form.html"):
    """
    Allows the user to edit a reply to a thread
    """
    data = {}
    return render(request, template, data)
Пример #10
0
def forum_home(request, template="vcboard/forum_home.html"):
    """
    Displays all of the top-level forums and other random forum info
    """
    data = {"forums": Forum.objects.top_level()}
    return render(request, template, data)
Пример #11
0
def permission_matrix(request, obj_type=None, id=None, template='admin/permission_matrix.html'):
    """
    Allows the user to quickly adjust permissions for forums, groups, ranks
    and individual users
    """

    site = Site.objects.get_current()
    default_perms = False
    klass, matrix_type = {
        'forum': (Forum, ForumPermission),
        'group': (UserGroup, GroupPermission),
        'rank': (Rank, RankPermission),
        'user': (User, UserPermission),
    }.get(obj_type, (None, None))

    if klass and id:
        obj = get_object_or_404(klass, pk=id)
        permissions = matrix_type.objects.filter(**{
                            'site': site,
                            str(obj_type + '__id'): obj.id})
    else:
        obj = None
        default_perms = True
        permissions = ForumPermission.objects.filter(site=site)
        matrix_type = ForumPermission

    # make the forum permission matrix view slightly different
    forum = None
    if isinstance(obj, Forum):
        forum = obj
    
    if request.method == 'POST':
        form = PermissionMatrixForm(request.POST, forum=forum)
        if form.is_valid():
            forums = {}
            perms = {}

            for field_name in form.fields.keys():
                forum_id, permission_id = re.findall('f_(\d+)_p_(\d+)', field_name)[0]

                # retrieve the forum
                f = forums.get(forum_id, None)
                if not f:
                    f = Forum.objects.get(pk=forum_id)
                    forums[forum_id] = f

                # retrieve the permission
                p = perms.get(permission_id, None)
                if not p:
                    p = Permission.objects.get(pk=permission_id)
                    perms[permission_id] = p

                params = {'site': site, 'forum': f, 'permission': p}
                if not forum and not default_perms:
                    params[str(obj_type)] = obj

                perm, c = matrix_type.objects.get_or_create(**params)
                value = form.cleaned_data[field_name]
                if perm.has_permission != value:
                    perm.has_permission = value
                    perm.save()

            # remove previously cached permissions for this forum.  This makes
            # it so the permissions have to be refreshed
            perms = cache.get('perms_for_forums', {})
            cached_perm_dict = cache.get('vcboard_user_perms', {})
            for forum_id in forums.keys():
                if perms.has_key(forum_id):
                    for key in perms[forum_id]:
                        del cached_perm_dict[key]
                perms[str(forum_id)] = []
            cache.set('vcboard_user_perms', cached_perm_dict)
            cache.set('perms_for_forums', perms)

            request.user.message_set.create(message='Permissions have been saved.')
    else:
        form = PermissionMatrixForm(permissions=permissions, forum=forum)

    data = {
        'object': obj,
        'form': form,
        'default_perms': default_perms
    }

    return render(request, template, data)