Skip to content

Commit

Permalink
Merge pull request #12 from conwetlab/develop
Browse files Browse the repository at this point in the history
Show comments number
  • Loading branch information
aitormagan committed Jun 24, 2015
2 parents 023f0a6 + 7cee3ab commit 6fdeed8
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 5 deletions.
7 changes: 7 additions & 0 deletions ckanext/datarequests/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def get_ordered_by_date(cls, **kw):
query = model.Session.query(cls).autoflush(False)
return query.filter_by(**kw).order_by(cls.time.desc()).all()

@classmethod
def get_datarequest_comments(cls, **kw):
'''
Returned the number of comments of a data request
'''
return model.Session.query(func.count(cls.id)).filter_by(**kw).scalar()

Comment = _Comment

# FIXME: References to the other tables...
Expand Down
33 changes: 33 additions & 0 deletions ckanext/datarequests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2015 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of CKAN Data Requests Extension.

# CKAN Data Requests Extension is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# CKAN Data Requests Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with CKAN Data Requests Extension. If not, see <http://www.gnu.org/licenses/>.

import ckan.model as model
import ckan.plugins.toolkit as tk
import db


def get_comments_number(datarequest_id):
# DB can be not intialized
db.init_db(model)
return db.Comment.get_datarequest_comments(datarequest_id=datarequest_id)


def get_comments_badge(datarequest_id):
return tk.render_snippet('datarequests/snippets/comments_badge.html',
{'comments_count': get_comments_number(datarequest_id)})
7 changes: 6 additions & 1 deletion ckanext/datarequests/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import auth
import actions
import constants
import helpers

from pylons import config

Expand Down Expand Up @@ -169,4 +170,8 @@ def before_map(self, m):
######################################################################

def get_helpers(self):
return {'show_comments_tab': lambda: self.comments_enabled}
return {
'show_comments_tab': lambda: self.comments_enabled,
'get_comments_number': helpers.get_comments_number,
'get_comments_badge': helpers.get_comments_badge
}
9 changes: 9 additions & 0 deletions ckanext/datarequests/public/datarequests.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@
font-size: 12px;
white-space: nowrap;
margin-top: 8px;
}

.divider {
display: inline-block;
margin-left: 10px;
}

.datarequest-properties {
margin-top: 8px;
}
2 changes: 1 addition & 1 deletion ckanext/datarequests/templates/datarequests/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{ h.build_nav_icon('datarequest_show', _('Data Request'), id=datarequest_id) }}

{% if h.show_comments_tab() %}
{{ h.build_nav_icon('datarequest_comment', _('Comments'), id=datarequest_id) }}
{{ h.build_nav_icon('datarequest_comment', _('Comments') + ' ' + h.get_comments_badge(datarequest_id), id=datarequest_id) }}
{% endif %}
{% endblock %}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span class="badge">{{ comments_count }}</span>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ <h3 class="dataset-heading">
{% if description %}
<div>{{ description }}</div>
{% endif %}
<div class="date-datarequests">{{ h.time_ago_from_timestamp(datarequest.open_time) }}</div>
<div class="datarequest-properties">
{% if h.show_comments_tab() %}
<a href="{{ h.url_for(controller='ckanext.datarequests.controllers.ui_controller:DataRequestsUI', action='comment', id=datarequest.get('id','')) }}" class="label"><i class="icon-comment"></i> {{ h.get_comments_number(datarequest.get('id', '')) }}</span></a>
{% endif %}
<div class="divider"/>
<span class="date-datarequests">{{ h.time_ago_from_timestamp(datarequest.open_time) }}</span>
</div>
</div>
{% endblock %}
</li>
34 changes: 34 additions & 0 deletions ckanext/datarequests/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,37 @@ def test_comment_get(self):

def test_comment_get_ordered_by_date(self):
self._test_get_ordered_by_date('Comment', 'time')

def test_get_datarequests_comments(self):

n_comments = 7
count = 'example'

db.func = MagicMock()
db.func.count.return_value = count

filter_by = MagicMock()
filter_by.scalar.return_value = n_comments

query = MagicMock()
query.filter_by = MagicMock(return_value=filter_by)

model = MagicMock()
model.DomainObject = object
model.Session.query = MagicMock(return_value=query)

# Init the database
db.init_db(model)

# Call the method
params = {
'datarequest_id': 'example_uuid_v4'
}
db.Comment.id = 'id'
result = db.Comment.get_datarequest_comments(**params)

# Assertions
self.assertEquals(n_comments, result)
query.filter_by.assert_called_once_with(**params)
model.Session.query.assert_called_once_with(count)
db.func.count.assert_called_once_with(db.Comment.id)
71 changes: 71 additions & 0 deletions ckanext/datarequests/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2015 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of CKAN Data Requests Extension.

# CKAN Data Requests Extension is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# CKAN Data Requests Extension is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with CKAN Data Requests Extension. If not, see <http://www.gnu.org/licenses/>.

import ckanext.datarequests.helpers as helpers
import unittest

from mock import MagicMock


class HelpersTest(unittest.TestCase):

def setUp(self):
self._tk = helpers.tk
helpers.tk = MagicMock()

self._model = helpers.model
helpers.model = MagicMock()

self._db = helpers.db
helpers.db = MagicMock()

def tearDown(self):
helpers.tk = self._tk
helpers.model = self._model
helpers.db = self._db

def test_get_comments_number(self):
# Mocking
n_comments = 3
helpers.db.Comment.get_datarequest_comments.return_value = 3

# Call the function
datarequest_id = 'example_uuidv4'
result = helpers.get_comments_number(datarequest_id)

# Assertions
helpers.db.init_db.assert_called_once_with(helpers.model)
helpers.db.Comment.get_datarequest_comments.assert_called_once_with(datarequest_id=datarequest_id)
self.assertEquals(result, n_comments)

def test_get_comments_badge(self):
# Mocking
n_comments = 3
helpers.db.Comment.get_datarequest_comments.return_value = 3

# Call the function
datarequest_id = 'example_uuidv4'
result = helpers.get_comments_badge(datarequest_id)

# Assertions
helpers.db.init_db.assert_called_once_with(helpers.model)
helpers.db.Comment.get_datarequest_comments.assert_called_once_with(datarequest_id=datarequest_id)
self.assertEquals(result, helpers.tk.render_snippet.return_value)
helpers.tk.render_snippet.assert_called_once_with('datarequests/snippets/comments_badge.html',
{'comments_count': n_comments})
11 changes: 9 additions & 2 deletions ckanext/datarequests/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
ACTIONS_NO_COMMENTS = TOTAL_ACTIONS - COMMENTS_ACTIONS


class DataRequestPlutinTest(unittest.TestCase):
class DataRequestPluginTest(unittest.TestCase):

def setUp(self):
self._actions = plugin.actions
Expand All @@ -44,6 +44,9 @@ def setUp(self):
self._config = plugin.config
plugin.config = MagicMock()

self._helpers = plugin.helpers
plugin.helpers = MagicMock()

# plg = plugin
self.datarequest_create = constants.DATAREQUEST_CREATE
self.datarequest_show = constants.DATAREQUEST_SHOW
Expand All @@ -60,6 +63,7 @@ def tearDown(self):
plugin.actions = self._actions
plugin.auth = self._auth
plugin.tk = self._tk
plugin.helpers = self._helpers

@parameterized.expand([
('True',),
Expand Down Expand Up @@ -204,4 +208,7 @@ def test_helpers(self, comments_enabled):

# Check result
expected_result = True if comments_enabled == 'True' else False
self.assertEquals(self.plg_instance.get_helpers()['show_comments_tab'](), expected_result)
helpers = self.plg_instance.get_helpers()
self.assertEquals(helpers['show_comments_tab'](), expected_result)
self.assertEquals(helpers['get_comments_number'], plugin.helpers.get_comments_number)
self.assertEquals(helpers['get_comments_badge'], plugin.helpers.get_comments_badge)

0 comments on commit 6fdeed8

Please sign in to comment.