Created
November 28, 2014 06:32
-
-
Save ryumei/8f8cf268b1278e8b543d to your computer and use it in GitHub Desktop.
Create and traverse project tree of Redmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from redmine import Redmine | |
class ProjectNode(object): | |
def __init__(self, project): | |
self.project = project | |
self.children = [] | |
self.has_parent = hasattr(project, u'parent') | |
def add_child(self, child): | |
self.children.append(child) | |
def trace(self, family=None): | |
family = [] if family is None else family | |
family.append(self.project.name) | |
self.execute(family) | |
if len(self.children) > 0: | |
for child in self.children: | |
child.trace(family) | |
family.pop() | |
def execute(self, family=None): | |
print("%s has %d children" % ('/'.join(family), len(self.children))) | |
redmine = Redmine('http://redmine-server/', key='API_KEY') | |
projects = redmine.project.all() | |
project_map = {} | |
for project in projects: | |
project_map[project.id] = ProjectNode(project) | |
project_root = [] | |
for node in project_map.values(): | |
if node.has_parent: | |
parent_id = node.project.parent.id | |
project_map[parent_id].add_child(node) | |
else: | |
project_root.append(node) | |
for node in project_root: | |
node.trace() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment