Skip to content

Commit

Permalink
User topics view added (including some tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellmetha committed Apr 11, 2015
1 parent bf2bb98 commit feae38d
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 6 deletions.
2 changes: 2 additions & 0 deletions machina/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BoardApp(Application):
forum_app = get_class('forum.app', 'application')
conversation_app = get_class('forum_conversation.app', 'application')
feeds_app = get_class('forum_feeds.app', 'application')
member_app = get_class('forum_member.app', 'application')
moderation_app = get_class('forum_moderation.app', 'application')
search_app = get_class('forum_search.app', 'application')
tracking_app = get_class('forum_tracking.app', 'application')
Expand All @@ -27,6 +28,7 @@ def get_urls(self):
url(r'', include(self.forum_app.urls)),
url(r'', include(self.conversation_app.urls)),
url(_(r'^feeds/'), include(self.feeds_app.urls)),
url(_(r'^member/'), include(self.member_app.urls)),
url(_(r'^moderation/'), include(self.moderation_app.urls)),
url(_(r'^search/'), include(self.search_app.urls)),
url(_(r'^tracking/'), include(self.tracking_app.urls)),
Expand Down
26 changes: 26 additions & 0 deletions machina/apps/forum_member/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-

# Standard library imports
# Third party imports
from django.conf.urls import patterns
from django.conf.urls import url
from django.utils.translation import ugettext_lazy as _

# Local application / specific library imports
from machina.core.app import Application
from machina.core.loading import get_class


class MemberApp(Application):
name = 'forum-member'

user_topics_view = get_class('forum_member.views', 'UserTopicsView')

def get_urls(self):
urls = [
url(_(r'^ego/topics/$'), self.user_topics_view.as_view(), name='user-topics'),
]
return patterns('', *urls)


application = MemberApp()
37 changes: 37 additions & 0 deletions machina/apps/forum_member/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

# Standard library imports
# Third party imports
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import ListView

# Local application / specific library imports
from machina.conf import settings as machina_settings
from machina.core.db.models import get_model
from machina.core.loading import get_class

Forum = get_model('forum', 'Forum')
Topic = get_model('forum_conversation', 'Topic')

PermissionHandler = get_class('forum_permission.handler', 'PermissionHandler')
perm_handler = PermissionHandler()


class UserTopicsView(ListView):
template_name = 'forum_member/user_topics_list.html'
context_object_name = 'topics'
paginate_by = machina_settings.FORUM_TOPICS_NUMBER_PER_PAGE

def get_queryset(self):
forums = perm_handler.forum_list_filter(
Forum.objects.all(), self.request.user)
topics = Topic.objects.filter(
forum__in=forums,
posts__poster_id=self.request.user.id,
posts__approved=True)
return topics.order_by('-updated')

@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(UserTopicsView, self).dispatch(request, *args, **kwargs)
2 changes: 1 addition & 1 deletion machina/static/machina/css/board_theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#main_container {
margin-bottom: 40px;
}
#main_container .moderation-link-wrapper {
#main_container .controls-link-wrapper {
margin-top: -20px;
}
.preview .post-preview .post-content-wrapper hr {
Expand Down
2 changes: 1 addition & 1 deletion machina/static/machina/less/board_pages/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#main_container{
margin-bottom: 40px;

.moderation-link-wrapper{
.controls-link-wrapper{
margin-top: -20px;
}
}
12 changes: 8 additions & 4 deletions machina/templates/machina/board_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load static %}
{% load i18n %}
{% load compress %}
{% load forum_member_tags %}
{% load forum_moderation_tags %}

{% block title %}{{ MACHINA_FORUM_NAME|default:"Forum" }} — {% block sub-title %}{% endblock sub-title %}{% endblock title %}
Expand Down Expand Up @@ -57,11 +58,14 @@
{% block breadcrumb %}
{% include "machina/partials/breadcrumb.html" %}
{% endblock breadcrumb %}
{% if request.user|can_access_moderation_panel %}
<div class="pull-right moderation-link-wrapper">
<div class="pull-right controls-link-wrapper">
{% if not request.user|is_anonymous %}
<a href="{% url 'forum-member:user-topics' %}" class="btn btn-link"><i class="fa fa-comments-o ">&nbsp;</i>{% trans "View my messages" %}</a>
{% endif %}
{% if request.user|can_access_moderation_panel %}
<a href="#" class="btn btn-link"><i class="fa fa-gavel">&nbsp;</i>{% trans "Moderation panel" %}</a>
</div>
{% endif %}
{% endif %}
</div>
</div>
</div>
<div class="row">
Expand Down
1 change: 1 addition & 0 deletions machina/templates/machina/forum/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{% block sub-title %}{% trans "Index" %}{% endblock sub-title %}

{% block content %}
<br />
<div class="row">
<div class="forumlist-top-controls col-xs-12">
{% if request.user.is_authenticated %}
Expand Down
38 changes: 38 additions & 0 deletions machina/templates/machina/forum_member/user_topics_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends 'board_base.html' %}
{% load i18n %}

{% block sub-title %}{% trans "View my messages" %}{% endblock sub-title %}


{% block content %}
<div class="row">
<div class="col-xs-12">
<h1>{% trans "View my messages" %}</h1>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-4 forum-actions-block">
{% if paginator.count > 0 %}
<p class="text-muted">
{% blocktrans with paginator.count as topic_length %}{{ topic_length }} topics found{% endblocktrans %}
</p>
{% endif %}
</div>
<div class="col-xs-12 col-md-8 pagination-block">
{% with "pagination-sm" as pagination_size %}
{% include "machina/partials/pagination.html" %}
{% endwith %}
</div>
</div>
{% trans "Not topics found." as empty_message %}
{% with topic_list_title="Topics" empty_message=empty_message %}
{% include "machina/forum_conversation/topic_list.html" %}
{% endwith %}
<div class="row">
<div class="col-xs-12 col-md-12 pagination-block">
{% with "pagination-sm" as pagination_size %}
{% include "machina/partials/pagination.html" %}
{% endwith %}
</div>
</div>
{% endblock content %}
Empty file.
90 changes: 90 additions & 0 deletions tests/functional/member/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-

# Standard library imports
# Third party imports
from django.core.urlresolvers import reverse
from guardian.shortcuts import assign_perm

# Local application / specific library imports
from machina.core.loading import get_class
from machina.test.factories import create_category_forum
from machina.test.factories import create_forum
from machina.test.factories import create_topic
from machina.test.factories import GroupFactory
from machina.test.factories import PostFactory
from machina.test.factories import UserFactory
from machina.test.testcases import BaseClientTestCase

PermissionHandler = get_class('forum_permission.handler', 'PermissionHandler')


class TestUserTopicsView(BaseClientTestCase):
def setUp(self):
super(TestUserTopicsView, self).setUp()

# Add some users
self.u1 = UserFactory.create()
self.g1 = GroupFactory.create()
self.u1.groups.add(self.g1)
self.user.groups.add(self.g1)

# Permission handler
self.perm_handler = PermissionHandler()

self.top_level_cat_1 = create_category_forum()

self.forum_1 = create_forum(parent=self.top_level_cat_1)
self.forum_2 = create_forum(parent=self.top_level_cat_1)
self.forum_3 = create_forum(parent=self.top_level_cat_1)

self.topic_1 = create_topic(forum=self.forum_2, poster=self.u1)
PostFactory.create(topic=self.topic_1, poster=self.u1)
PostFactory.create(topic=self.topic_1, poster=self.user)

self.topic_2 = create_topic(forum=self.forum_1, poster=self.user)
PostFactory.create(topic=self.topic_2, poster=self.user)
PostFactory.create(topic=self.topic_2, poster=self.u1)

self.topic_3 = create_topic(forum=self.forum_2, poster=self.u1)
PostFactory.create(topic=self.topic_3, poster=self.u1)

self.topic_4 = create_topic(forum=self.forum_2, poster=self.user)
PostFactory.create(topic=self.topic_4, poster=self.user)

# Assign some permissions
assign_perm('can_read_forum', self.g1, self.top_level_cat_1)
assign_perm('can_read_forum', self.g1, self.forum_1)
assign_perm('can_read_forum', self.g1, self.forum_2)

def test_browsing_works(self):
# Setup
correct_url = reverse('forum-member:user-topics')
# Run
response = self.client.get(correct_url, follow=True)
# Check
self.assertIsOk(response)


def test_insert_only_topics_where_the_considered_user_participated_in_the_context(self):
# Setup
correct_url = reverse('forum-member:user-topics')
# Run
response = self.client.get(correct_url, follow=True)
# Check
self.assertIsOk(response)
self.assertQuerysetEqual(
response.context_data['topics'],
[self.topic_4, self.topic_2, self.topic_1, ])

def test_does_not_consider_non_approved_posts(self):
# Setup
topic_5 = create_topic(forum=self.forum_2, poster=self.user)
PostFactory.create(topic=topic_5, poster=self.user, approved=False)
correct_url = reverse('forum-member:user-topics')
# Run
response = self.client.get(correct_url, follow=True)
# Check
self.assertIsOk(response)
self.assertQuerysetEqual(
response.context_data['topics'],
[self.topic_4, self.topic_2, self.topic_1, ])

0 comments on commit feae38d

Please sign in to comment.