diff options
author | Andy Doan <andy.doan@linaro.org> | 2015-07-17 18:51:12 -0500 |
---|---|---|
committer | Andy Doan <andy.doan+gerrit@linaro.org> | 2015-10-16 16:02:41 +0000 |
commit | 777d98cead01e096ba27615950e29beade907783 (patch) | |
tree | 8e34cb84e5efd7c87008e324120ffba2c10558b4 | |
parent | 6d95fa1cb784406d6088c6bd30fc33ddffe8b778 (diff) |
linaro-metrics: Add simple view for a team
Unfortunately the monkey-patching is required so that we don't have to
maintain our own copy of patch-list.html which is 300 lines of complex
rendering.
Change-Id: Ifb1d6c478d9f9acab8be3bb565a1c6df056e1417
-rw-r--r-- | linaro_metrics/templates/linaro_metrics/team.html | 21 | ||||
-rw-r--r-- | linaro_metrics/templates/linaro_metrics/teams.html | 2 | ||||
-rw-r--r-- | linaro_metrics/tests/test_views.py | 27 | ||||
-rw-r--r-- | linaro_metrics/urls.py | 1 | ||||
-rw-r--r-- | linaro_metrics/views.py | 66 |
5 files changed, 115 insertions, 2 deletions
diff --git a/linaro_metrics/templates/linaro_metrics/team.html b/linaro_metrics/templates/linaro_metrics/team.html new file mode 100644 index 0000000..3f0de50 --- /dev/null +++ b/linaro_metrics/templates/linaro_metrics/team.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} + +{% block title %}Patches credited to the "{{team.name}}" team{% endblock %} +{% block heading %}Patches credited to the "{{team.name}}" team{% endblock %} + +{% block body %} + <h3>Current Members</h3> + <ul> + {% for m in memberships %} + <li> + {% if m.user.first_name %} + {{m.user.first_name}} {{m.user.last_name}} + {% else %} + {{m.user}} + {% endif %} + </li> + {% endfor %} + </ul> +</div> +{% include "patchwork/patch-list.html" %} +{% endblock %} diff --git a/linaro_metrics/templates/linaro_metrics/teams.html b/linaro_metrics/templates/linaro_metrics/teams.html index 6f96b98..6f1697a 100644 --- a/linaro_metrics/templates/linaro_metrics/teams.html +++ b/linaro_metrics/templates/linaro_metrics/teams.html @@ -9,7 +9,7 @@ <ul> {% for t in teams %} <li> - {{t.name}} + <a href="{% url 'linaro_metrics.views.team_view' team=t.name %}">{{t.name}}</a> </li> {% endfor %} </ul> diff --git a/linaro_metrics/tests/test_views.py b/linaro_metrics/tests/test_views.py new file mode 100644 index 0000000..023caf7 --- /dev/null +++ b/linaro_metrics/tests/test_views.py @@ -0,0 +1,27 @@ +from django.test import Client, TestCase + +from linaro_metrics.models import Team +from linaro_metrics.sync_teams import sync_crowd +from linaro_metrics.tests.test_sync_teams import FakeCrowd + + +class TestTeamsView(TestCase): + fixtures = ['default_states'] + + def setUp(self): + # Create team memberships and project for a patch we'll import. + t = Team.objects.create(name='foo') + crowd = FakeCrowd() + foo_group = [ + ('user@foo.com', 'user name'), + ('user2@foo.com', 'user2 name'), + ('zoltan.kiss@linaro.org', 'Zoltan Kiss'), + ] + crowd.add_group('foo', foo_group) + sync_crowd(crowd, [t]) + + def test_members_show(self): + client = Client() + resp = client.get('/team/foo/') + self.assertEqual(resp.status_code, 200) + self.assertIn('<h3>Current Members</h3>', resp.content) diff --git a/linaro_metrics/urls.py b/linaro_metrics/urls.py index e6d18a1..f71c98e 100644 --- a/linaro_metrics/urls.py +++ b/linaro_metrics/urls.py @@ -12,4 +12,5 @@ urlpatterns = patterns( # Now provide our own urls. (r'^team/$', 'linaro_metrics.views.team_overview'), + (r'^team/(?P<team>[^/]+)/$', 'linaro_metrics.views.team_view'), ) diff --git a/linaro_metrics/views.py b/linaro_metrics/views.py index dac2217..d0abb82 100644 --- a/linaro_metrics/views.py +++ b/linaro_metrics/views.py @@ -1,8 +1,18 @@ -from django.shortcuts import render_to_response +import mock + +import django.template.base + +from django.shortcuts import get_object_or_404, render_to_response from django.template import RequestContext +from patchwork.filters import DelegateFilter +from patchwork.models import Patch +from patchwork.views import generic_list +from patchwork.templatetags.person import personify + from linaro_metrics.models import ( Team, + TeamMembership, ) @@ -11,3 +21,57 @@ def team_overview(request): 'teams': Team.objects.filter(active=True), }) return render_to_response('linaro_metrics/teams.html', context) + + +# The patchwork version of the "personify" filter requires a project. We don't +# have a project for "team". Although personify is a simple function, we can't +# directly monkey-patch it due to the way django templates load filters. This +# tricks django into using loading our version of "personify". Our version will +# call the original if possible, but in the case there is no project, we'll +# just return the person's name +def hack_add_lib(self, lib): + def _non_project_personify(person, project): + if project: + return personify(person, project) + return person.name + if 'personify' in lib.filters: + lib.filters['personify'] = _non_project_personify + return real_add_lib(self, lib) +real_add_lib = django.template.base.Parser.add_library +django.template.base.Parser.add_library = hack_add_lib + + +def _non_project_ctx(request, view, view_args, patches): + # generic_list requires a Project object. A team has no specific project, + # so we just use Mock to pass dummy object through. We set two key values + # to influence the function: + # is_editable: user can't modify things + # tags: tags aren't applicable to this use case + project = mock.Mock() + project.is_editable.return_value = False + project.tags = [] + + context = generic_list( + request, project, view, view_args=view_args, patches=patches) + + # The DelegateFilter won't work for because its tied to a project/user. + context.filters._filters = [ + x for x in context.filters._filters + if not isinstance(x, DelegateFilter)] + return context + + +def team_view(request, team): + team = get_object_or_404(Team, name=team) + patches = Patch.objects.filter(teamcredit__team=team) + + view_args = {'team': team} + + context = _non_project_ctx( + request, 'linaro_metrics.views.team_view', view_args, patches) + context.update({ + 'team': team, + 'memberships': TeamMembership.objects.filter(team=team), + 'project': None, # Prevent trying to show project details in header. + }) + return render_to_response('linaro_metrics/team.html', context) |