diff --git a/swh/web/ui/templates/directory.html b/swh/web/ui/templates/directory.html index 4cb9eca4..e5955f28 100644 --- a/swh/web/ui/templates/directory.html +++ b/swh/web/ui/templates/directory.html @@ -1,10 +1,10 @@ {% extends "layout.html" %} {% block title %}Browse directory{% endblock %} {% block content %} {{ message }} {% if ls is not none %} {% for e in files %} -

{{ e.name }}

+

{{ e.name }}

{% endfor %} {% endif %} {% endblock %} diff --git a/swh/web/ui/tests/test_utils.py b/swh/web/ui/tests/test_utils.py index a59e8768..963ce517 100644 --- a/swh/web/ui/tests/test_utils.py +++ b/swh/web/ui/tests/test_utils.py @@ -1,118 +1,121 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information import unittest from unittest.mock import patch from nose.tools import istest from swh.web.ui import utils class Rule(object): rule = "" endpoint = None methods = [] def __init__(self, rule, methods, endpoint): self.rule = rule self.endpoint = endpoint self.methods = methods class Map(object): _rules = [] def __init__(self, rules): self._rules = rules class UtilsTestCase(unittest.TestCase): def setUp(self): self.url_map = Map([Rule('/other/', methods=set(['GET', 'POST', 'HEAD']), endpoint='foo'), Rule('/some/old/url/', methods=set(['GET', 'POST']), endpoint='blablafn'), Rule('/other/old/url/', methods=set(['GET', 'HEAD']), endpoint='bar'), Rule('/other', methods=set([]), endpoint=None), Rule('/other2', methods=set([]), endpoint=None)]) @istest def filter_endpoints_1(self): # when actual_data = utils.filter_endpoints(self.url_map, '/some') # then self.assertEquals(actual_data, { '/some/old/url/': { 'methods': ['GET', 'POST'], 'endpoint': 'blablafn' } }) @istest def filter_endpoints_2(self): # when actual_data = utils.filter_endpoints(self.url_map, '/other', blacklist=['/other2']) # then # rules /other is skipped because its' exactly the prefix url # rules /other2 is skipped because it's blacklisted self.assertEquals(actual_data, { '/other/': { 'methods': ['GET', 'HEAD', 'POST'], 'endpoint': 'foo' }, '/other/old/url/': { 'methods': ['GET', 'HEAD'], 'endpoint': 'bar' } }) @patch('swh.web.ui.utils.flask') @istest def prepare_directory_listing(self, mock_flask): # given def mock_url_for(url_key, **kwds): if url_key == 'browse_directory': sha1_git = kwds['sha1_git'] return '/path/to/url/dir' + '/' + sha1_git else: sha1_git = kwds['q'] return '/path/to/url/file' + '/' + sha1_git mock_flask.url_for.side_effect = mock_url_for inputs = [{'type': 'dir', 'target': '123', 'name': 'some-dir-name'}, {'type': 'file', 'sha1': '654', 'name': 'some-filename'}, {'type': 'dir', 'target': '987', 'name': 'some-other-dirname'}] expected_output = [{'link': '/path/to/url/dir/123', - 'name': 'some-dir-name'}, + 'name': 'some-dir-name', + 'type': 'dir'}, {'link': '/path/to/url/file/654', - 'name': 'some-filename'}, + 'name': 'some-filename', + 'type': 'file'}, {'link': '/path/to/url/dir/987', - 'name': 'some-other-dirname'}] + 'name': 'some-other-dirname', + 'type': 'dir'}] # when actual_outputs = utils.prepare_directory_listing(inputs) # then self.assertEquals(actual_outputs, expected_output) diff --git a/swh/web/ui/utils.py b/swh/web/ui/utils.py index 0c39b53a..f6a75feb 100644 --- a/swh/web/ui/utils.py +++ b/swh/web/ui/utils.py @@ -1,52 +1,52 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information import flask def filter_endpoints(url_map, prefix_url_rule, blacklist=[]): """Filter endpoints by prefix url rule. Args: - url_map: Url Werkzeug.Map of rules - prefix_url_rule: prefix url string - blacklist: blacklist of some url Returns: Dictionary of url_rule with values methods and endpoint. The key is the url, the associated value is a dictionary of 'methods' (possible http methods) and 'endpoint' (python function) """ out = {} for r in url_map._rules: rule = r.rule if rule == prefix_url_rule or rule in blacklist: continue if rule.startswith(prefix_url_rule): out[r.rule] = {'methods': sorted(map(str, r.methods)), 'endpoint': r.endpoint} return out def prepare_directory_listing(files): """Given a list of dictionary files, return a view ready dictionary. """ ls = [] for entry in files: - new_entry = {} + new_entry = {'name': entry['name'], + 'type': entry['type']} if entry['type'] == 'dir': new_entry['link'] = flask.url_for('browse_directory', sha1_git=entry['target']) else: new_entry['link'] = flask.url_for('show_content', q=entry['sha1']) - new_entry['name'] = entry['name'] ls.append(new_entry) return ls