Repository: nephila/python-taiga
Pull request #9 (2015-06-13 15:17:50)

Feature/roles


Patch:
diff --git a/demo.py b/demo.py
index 0745ef6..ff6706a 100644
--- a/demo.py
+++ b/demo.py
@@ -44,12 +44,14 @@ newissue = new_project.add_issue(
 ).attach('README.md')
 
 projects = api.projects.list()
+
 print (projects)
 
-for user in projects[0].users:
+for user in new_project.users:
     print (user)
 
 stories = api.user_stories.list()
+
 print (stories)
 
 projects[0].star()
@@ -59,10 +61,12 @@ api.milestones.list()
 projects = api.projects.list()
 print (projects)
 
-new_project = projects.get(name='TEST PROJECT 3')
-print (new_project)
+another_new_project = projects.get(name='TEST PROJECT 3')
+
+print (another_new_project)
 
 users = api.users.list()
+
 print (users)
 
 print (api.search(projects.get(name='TEST PROJECT 3').id, 'New').user_stories[0].subject)
@@ -70,3 +74,12 @@ print (api.search(projects.get(name='TEST PROJECT 3').id, 'New').user_stories[0]
 print new_project.add_issue_attribute(
     'Device', description='(iPad, iPod, iPhone, Desktop, etc.)'
 )
+
+print(new_project.roles)
+
+memberships = new_project.list_memberships()
+new_project.add_role('New role', permissions=["add_issue", "modify_issue"])
+
+new_project.add_membership('stagi.andrea@gmail.com', new_project.roles[0].id)
+for membership in memberships:
+    print (membership.role_name)
diff --git a/taiga/client.py b/taiga/client.py
index 64e1b32..332e14a 100644
--- a/taiga/client.py
+++ b/taiga/client.py
@@ -9,6 +9,7 @@ from .models import Milestones
 from .models import Points
 from .models import UserStoryStatuses
 from .models import Severities
+from .models import Roles
 from .models import Priorities
 from .models import IssueStatuses
 from .models import IssueAttributes
@@ -48,6 +49,7 @@ class TaigaAPI:
         self.tasks = Tasks(self.raw_request)
         self.milestones = Milestones(self.raw_request)
         self.severities = Severities(self.raw_request)
+        self.roles = Roles(self.raw_request)
         self.points = Points(self.raw_request)
         self.issue_statuses = IssueStatuses(self.raw_request)
         self.issue_attributes = IssueAttributes(self.raw_request)
diff --git a/taiga/models/__init__.py b/taiga/models/__init__.py
index 73229e4..5698008 100644
--- a/taiga/models/__init__.py
+++ b/taiga/models/__init__.py
@@ -18,6 +18,8 @@ from .models import UserStoryStatus
 from .models import UserStoryStatuses
 from .models import Severity
 from .models import Severities
+from .models import Role
+from .models import Roles
 from .models import Priority
 from .models import Priorities
 from .models import IssueStatus
@@ -44,5 +46,5 @@ __all__ = [
     'IssueStatuses', 'TaskStatus', 'TaskStatuses', 'WikiPage', 'WikiPages',
     'WikiLink', 'WikiLinks', 'IssueAttribute', 'IssueAttributes',
     'TaskAttribute', 'TaskAttributes', 'UserStoryAttribute',
-    'UserStoryAttributes', 'Membership', 'Memberships'
+    'UserStoryAttributes', 'Membership', 'Memberships', 'Role', 'Roles'
 ]
diff --git a/taiga/models/models.py b/taiga/models/models.py
index f428959..b3e5472 100644
--- a/taiga/models/models.py
+++ b/taiga/models/models.py
@@ -451,6 +451,22 @@ class Severities(ListResource):
         return self._new_resource(payload=attrs)
 
 
+class Role(InstanceResource):
+
+    endpoint = 'roles'
+
+    allowed_params = ['name', 'slug', 'order', 'computable']
+
+
+class Roles(ListResource):
+
+    instance = Role
+
+    def create(self, project, name, **attrs):
+        attrs.update({'project': project, 'name': name})
+        return self._new_resource(payload=attrs)
+
+
 class Project(InstanceResource):
 
     endpoint = 'projects'
@@ -470,6 +486,7 @@ class Project(InstanceResource):
         'issue_types': IssueTypes,
         'task_statuses': TaskStatuses,
         'severities': Severities,
+        'roles': Roles,
         'points': Points,
         'us_statuses': UserStoryStatuses
     }
@@ -560,6 +577,12 @@ class Project(InstanceResource):
     def list_severities(self):
         return Severities(self.requester).list(project=self.id)
 
+    def add_role(self, name, **attrs):
+        return Roles(self.requester).create(self.id, name, **attrs)
+
+    def list_roles(self):
+        return Roles(self.requester).list(project=self.id)
+
     def add_priority(self, name, **attrs):
         return Priorities(self.requester).create(self.id, name, **attrs)
 


Test patch:
diff --git a/runtests.sh b/runtests.sh
new file mode 100755
index 0000000..e851caa
--- /dev/null
+++ b/runtests.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+rm .coverage
+flake8 taiga
+nosetests --with-coverage --cover-package=taiga
\ No newline at end of file
diff --git a/tests/test_projects.py b/tests/test_projects.py
index 71c143b..096d071 100644
--- a/tests/test_projects.py
+++ b/tests/test_projects.py
@@ -1,6 +1,7 @@
 from datetime import datetime
 from taiga.requestmaker import RequestMaker
-from taiga.models import User, Point, UserStoryStatus, Severity, Project, Projects
+from taiga.models import User, Point, UserStoryStatus
+from taiga.models import Severity, Role, Project, Projects
 import unittest
 from mock import patch
 from taiga import TaigaAPI
@@ -114,6 +115,20 @@ class TestProjects(unittest.TestCase):
         project.list_severities()
         mock_list_severities.assert_called_with(project=1)
 
+    @patch('taiga.models.Roles.create')
+    def test_add_role(self, mock_new_role):
+        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
+        project = Project(rm, id=1)
+        project.add_role('Role 1')
+        mock_new_role.assert_called_with(1, 'Role 1')
+
+    @patch('taiga.models.Roles.list')
+    def test_list_roles(self, mock_list_roles):
+        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
+        project = Project(rm, id=1)
+        project.list_roles()
+        mock_list_roles.assert_called_with(project=1)
+
     @patch('taiga.models.models.IssueTypes.create')
     def test_add_issue_type(self, mock_new_issue_type):
         rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
diff --git a/tests/test_roles.py b/tests/test_roles.py
new file mode 100644
index 0000000..f131975
--- /dev/null
+++ b/tests/test_roles.py
@@ -0,0 +1,17 @@
+from taiga.requestmaker import RequestMaker
+from taiga.models import Role, Roles
+import unittest
+from mock import patch
+
+
+class TestRoles(unittest.TestCase):
+
+    @patch('taiga.models.base.ListResource._new_resource')
+    def test_create_role(self, mock_new_resource):
+        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
+        mock_new_resource.return_value = Role(rm)
+        sv = Roles(rm).create(1, 'RL 1')
+        mock_new_resource.assert_called_with(
+            payload={'project': 1, 'name': 'RL 1'}
+        )
+