InboxView(as_view(option='UNREAD_MESSAGES') to get only unread messages

Issue #122 resolved
Former user created an issue

Following another ticket in my single-page application that header would like to send a query to get all unread messages. The template can filter out read ones but it would be much more efficient if the backend only sends unread messages.

It appears that option would be useful for this purpose but I only see option=OPTION_MESSAGE which returns all messages. I am wondering if something like

InboxView.as_view(template_name='postman/header_list.html', option='UNREAD_MESSAGES')

is already available or can be added. Thanks,

Comments (12)

  1. Patrick Samson repo owner
    • changed status to open

    The current option concept is not designed to be passed as an argument to as_view().

    I think more about an optional query string argument, something like: <a href="{% url 'postman:inbox' %}?unread">

    Would it be OK for your need?

  2. Bo Peng

    Yes, that would be perfect. Since we are at it, the inbox could also accept ?page for support of pagination.

  3. Patrick Samson repo owner

    The implementation is now committed in repository.
    Could you have a try before a public release?

  4. Bo Peng

    Thanks! I will test the new features tonight.

    What is your opinion about pagination? I am tempted to create a new Folder view just to add pagination to the message list. It looks like all that is needed is something like the following in FolderMixin:

        paginator = Paginator(msgs, self.paginate_by)
        page_number = request.GET.get('page')
        context.update(
          {'page_obj': paginator.get_page(page_number) 
          ...
          })
    

  5. Bo Peng

    This option works. However, I am using a separate template to get a list of new messages from the header and have to discard extra messages if there are more than 10 new messages (otherwise the list will get long and ugly). It would be helpful for this URL to accept pagination (page=1 to get only the first page) and/or a limit (?limit=10).

  6. Bo Peng

    Thanks for pointing me to that app, which I will have a look. For my simple application (limiting to first ten unread messages in a pop up window from the header), I can already filter out unneeded messages by controlling the for loop in my template, but with a paginator and queryset, supposedly only the first 10 would be retrieved from the database which could be more performant.

  7. Bo Peng

    Had a look, unfortunately we cannot use dj-paginator because we are using the jinja2 template engine. Since most of our views supports pagination and we already have a nice template for paginator, I suppose it is easier for us to fork django-postman than messing with the templates.

  8. Patrick Samson repo owner

    What about the idea of:

    <a href="{% url 'postman:inbox' %}?unread&limit=10">

    Anyway you don’t need a real navigation based on multiple pages.

  9. Bo Peng

    In our jinja2 template for the header notification message list we have

    {% for message in pm_messages[:10] %}
    

    which works fine with the only caveat that all messages will be retrieved from the database. An option such as limit=10 would be helpful if it can address the problem at the queryset level (not as an after effect after all messages are retrieved).

    We have also extended the InboxView with pagination for our main message interface since we cannot use dj-pagination. There would be no need for option limit if pagination support is added since ?limit=10 is equivalent to ?page=1. Personally I think page is more common for. a Django list view, more general than limit, and the amount of work is about the same.

  10. Log in to comment