[damned-lies] Add list of teams JSON interface



commit e9ae0daa05ac448917dd1ce37eedf2519e705ffe
Author: Marcos Chavarría Teijeiro <chavarria1991 gmail com>
Date:   Mon Dec 2 10:55:02 2013 +0100

    Add list of teams JSON interface
    
    Fixes bug #719663.

 teams/tests.py                 |   46 ++++++++++++++++++++++++++++++++++++++++
 teams/urls.py                  |    2 +-
 teams/views.py                 |    4 +-
 templates/teams/team_list.json |   12 ++++++++++
 4 files changed, 61 insertions(+), 3 deletions(-)
---
diff --git a/teams/tests.py b/teams/tests.py
index 75e2c40..5e7f0fe 100644
--- a/teams/tests.py
+++ b/teams/tests.py
@@ -163,6 +163,52 @@ class TeamTest(TeamsAndRolesTests):
         team = Team.objects.get(name='fr')
         self.assertEquals(team.webpage_url, u"http://www.gnomefr.org/";)
 
+class JSONTeamsTest(TeamsAndRolesTests):
+    def setUp(self):
+        super(JSONTeamsTest, self).setUp()
+        t3 = Team.objects.create(name='gl', description='Galician')
+        coor1 = Person.objects.create(first_name='Marcos', last_name='Coordinator',
+            email='marc imthebigboss fr', username='marcos', svn_account='thesvnaccount')
+        coor2 = Person.objects.create(first_name='Pepe', last_name='Coordinator',
+            email='pepe imthebigboss es', username='pepe')
+        Role.objects.create(team=t3, person=coor1, role='coordinator')
+        Role.objects.create(team=t3, person=coor2, role='coordinator')
+
+    def test_json_teams(self):
+        """Test JSON teams interface"""
+        c = Client()
+        response = c.get(reverse('teams', args=['json']))
+        self.assertEqual(response.status_code, 200)
+        expected_JSON = """[
+            {
+                "id":"fr",
+                "description":"French",
+                "coordinators": [
+                {
+                    "name":"John Coordinator"
+                }]
+            },
+            {
+                "id":"gl",
+                "description":"Galician",
+                "coordinators": [
+                    {
+                        "name":"Marcos Coordinator",
+                        "vcs":"thesvnaccount"
+                    },
+                    {
+                        "name":"Pepe Coordinator"
+                    }
+                ]
+            },
+            {
+                "id":"pt",
+                "description":"Portuguese",
+                "coordinators": []
+            }
+            ]"""
+        self.assertJSONEqual(response.content, expected_JSON)
+
 
 class RoleTest(TeamsAndRolesTests):
 
diff --git a/teams/urls.py b/teams/urls.py
index 087405b..2a68afb 100644
--- a/teams/urls.py
+++ b/teams/urls.py
@@ -3,7 +3,7 @@ from django.conf.urls import patterns, url
 
 urlpatterns = patterns('teams.views',
     url(
-        regex = r'^(?P<format>(xml))?/?$',
+        regex = r'^(?P<format>(xml|json))?/?$',
         view = 'teams',
         name = 'teams'),
     url(
diff --git a/teams/views.py b/teams/views.py
index ce9f883..2bd5e62 100644
--- a/teams/views.py
+++ b/teams/views.py
@@ -32,8 +32,8 @@ from languages.models import Language
 def teams(request, format='html'):
     teams = Team.objects.all_with_coordinator()
     format = request.GET.get('format') or format
-    if format == 'xml':
-        return render(request, 'teams/team_list.xml', { 'teams' : teams },
+    if format in ('xml', 'json'):
+        return render(request, 'teams/team_list.%s' % format, {'teams': teams},
             content_type=utils.MIME_TYPES[format]
         )
     else:
diff --git a/templates/teams/team_list.json b/templates/teams/team_list.json
new file mode 100644
index 0000000..56880d8
--- /dev/null
+++ b/templates/teams/team_list.json
@@ -0,0 +1,12 @@
+[{% for team in teams %}
+  {
+    "id":"{{ team.name }}",
+    "description":"{{ team.description }}",
+    "coordinators": [{% for coordinator in team.get_coordinators %}
+      {
+        "name":"{{ coordinator.name }}"{% if coordinator.svn_account %},
+        "vcs":"{{ coordinator.svn_account }}"{% endif %}
+      }{% if not forloop.last %},
+    {% endif %}{% endfor %}]
+  }{% if not forloop.last %},{% endif %}{% endfor %}
+]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]