Example #1
0
File: views.py Project: ella/esus
def table(request, category, table):
    category = get_object_or_404(Category, slug=category)
    table = get_object_or_404(Table, slug=table)

    access_manager = AccessManager(context={
        "user" : request.user,
        "table" : table,
        "category" : category,
    })

    comment_forms = None
    form = None

    if not access_manager.has_table_read():
        c = RequestContext(request, {
            "message" : "You have not enough privileges to read this table."
        })
        t = loader.get_template("esus/forbidden.html")
        return HttpResponseForbidden(t.render(c))
        #return HttpResponseForbidden(_("You have not enough privileges to read this table."))


    if request.method == "POST":
        # TODO: Abstract this logic into something more sensible, some kind of
        # action dispatcher would be nice
        if request.POST.has_key(u"Odeslat"):
            
            form = CommentCreationForm(request.POST)
            if form.is_valid():
                # posting new message
                if not access_manager.has_comment_create():
                    return HttpResponseForbidden()
                table.add_comment(
                    text = form.cleaned_data['text'],
                    author = request.user,
                )
                #TODO: Redirect to self avoid multiple posts
                form = CommentCreationForm()
        elif request.POST.has_key(u'control-action') \
            and request.POST[u'control-action']:
            comment_forms = formset_factory(CommentControlForm, can_delete=True)(request.POST)
            if comment_forms.is_valid():
                for comm_form in comment_forms.deleted_forms:
                    comment = Comment.objects.get(pk=comm_form.cleaned_data['pk'])
                    if access_manager.has_comment_delete(comment=comment):
                        comment.delete()
                    else:
                        #TODO: Display message or pass silently?
                        pass
            comment_forms = None

    comments = Comment.objects.filter(table=table).order_by('-date')

    if not comment_forms:
        comment_forms = formset_factory(CommentControlForm, can_delete=True)(
            initial = [
                {'pk' : comment.pk} for comment in comments
            ]
        )
    if not form:
        form = CommentCreationForm()

    return direct_to_template(request, "esus/table.html", {
        "category" : category,
        "table" : table,
        "form" : form,
        "formset" : comment_forms,
        "comments" : zip(comments, comment_forms.forms),
        'access' : access_manager,
    })
Example #2
0
def table(request, category, table, from_pk=None, page_number=None):
    category = get_object_or_404(Category, slug=category)
    table = get_object_or_404(Table, slug=table)
    try:
        page_number = int(page_number)
    except TypeError:
        page_number = 1

    access_manager = AccessManager(context={"user": request.user, "table": table, "category": category})

    comment_forms = None
    form = None

    if not access_manager.has_table_read():
        c = RequestContext(request, {"message": "You have not enough privileges to read this table."})
        t = loader.get_template("esus/forbidden.html")
        return HttpResponseForbidden(t.render(c))
        # return HttpResponseForbidden(_("You have not enough privileges to read this table."))

    if request.method == "POST":
        # TODO: Abstract this logic into something more sensible, some kind of
        # action dispatcher would be nice
        if request.POST.has_key(u"odeslat"):

            form = CommentCreationForm(request.POST)
            if form.is_valid():
                # posting new message
                if not access_manager.has_comment_create():
                    return HttpResponseForbidden()
                table.add_comment(text=form.cleaned_data["text"], author=request.user)
                return HttpResponseRedirect(
                    reverse("esus-phorum-table", kwargs={"category": category.slug, "table": table.slug})
                )
                form = CommentCreationForm()
        elif request.POST.has_key(u"control-action") and request.POST[u"control-action"]:
            comment_forms = formset_factory(CommentControlForm, can_delete=True)(request.POST)
            if comment_forms.is_valid():
                for comm_form in comment_forms.deleted_forms:
                    comment = Comment.objects.get(pk=comm_form.cleaned_data["pk"])
                    if access_manager.has_comment_delete(comment=comment):
                        comment.delete()
                    else:
                        # TODO: Display message or pass silently?
                        pass
            comment_forms = None

    # skip is costly, so pagination is done via range query trick
    # we could either depend on timestap not containing duplicities or on the
    # fact that ID is always-increasing positive integer.
    # because so many things in Django rely on PositiveIntegerField, I'd go for it;
    # when we switch to non-RDBMS backend, those strategies shall be reversed.

    # To be stateless, URI must also contain of the next_page as well as "page number"
    # parameter

    on_page = getattr(settings, "ESUS_DEFAULT_COMMENTS_ON_PAGE", 10)

    # list() querysets directly to support negative indexing fun, and we're
    # zipping it later in memory anyway
    if not from_pk:
        comments = list(Comment.objects.filter(table=table).order_by("-date")[: on_page + 1])
    else:
        comments = list(Comment.objects.filter(table=table, id__lte=from_pk).order_by("-date")[: on_page + 1])

    if len(comments) > on_page:
        next_page_pk = comments[-1:][0].pk
        comments = comments[:-1]
    else:
        next_page_pk = None

    if request.is_ajax():
        return serialize({"comments": comments, "page_number": str(page_number), "next_page_pk": next_page_pk})

    if not comment_forms:
        comment_forms = formset_factory(CommentControlForm, can_delete=True)(
            initial=[{"pk": comment.pk} for comment in comments]
        )
    if not form:
        form = CommentCreationForm()

    return direct_to_template(
        request,
        "esus/table.html",
        {
            "category": category,
            "table": table,
            "form": form,
            "formset": comment_forms,
            "comments": zip(comments, comment_forms.forms),
            "access": access_manager,
            "next_page_pk": next_page_pk,
            "page_number": page_number,
            "next_page_number": page_number + 1,
        },
    )