diff --git a/rtcclient/client.py b/rtcclient/client.py index 96b0ad7..2ac15b7 100644 --- a/rtcclient/client.py +++ b/rtcclient/client.py @@ -92,8 +92,7 @@ def _get_headers(self): _headers["Accept"] = self.CONTENT_XML return _headers - def getProjectAreas(self, archived=False, - returned_properties=None): + def getProjectAreas(self, archived=False, returned_properties=None): """Get all :class:`rtcclient.project_area.ProjectArea` objects If no :class:`rtcclient.project_area.ProjectArea` objects are @@ -108,11 +107,8 @@ def getProjectAreas(self, archived=False, :rtype: list """ - rp = returned_properties - return self._get_paged_resources("ProjectArea", - page_size="10", - archived=archived, - returned_properties=rp) + return self._getProjectAreas(archived=archived, + returned_properties=returned_properties) def getProjectArea(self, projectarea_name, archived=False, returned_properties=None): @@ -127,25 +123,54 @@ def getProjectArea(self, projectarea_name, archived=False, :rtype: rtcclient.project_area.ProjectArea """ - self.log.debug("Try to get ", projectarea_name) if not isinstance(projectarea_name, six.string_types) or not projectarea_name: excp_msg = "Please specify a valid ProjectArea name" self.log.error(excp_msg) raise exception.BadValue(excp_msg) + self.log.debug("Try to get ", projectarea_name) rp = returned_properties - proj_areas = self.getProjectAreas(archived=archived, - returned_properties=rp) + proj_areas = self._getProjectAreas(archived=archived, + returned_properties=rp, + projectarea_name=projectarea_name) + if proj_areas is not None: - for proj_area in proj_areas: - if proj_area.title == projectarea_name: - self.log.info("Find ", proj_area) - return proj_area + proj_area = proj_areas[0] + self.log.info("Find ", proj_area) + return proj_area self.log.error("No ProjectArea named %s", projectarea_name) raise exception.NotFound("No ProjectArea named %s" % projectarea_name) + def _getProjectAreas(self, archived=False, returned_properties=None, + projectarea_name=None, projectarea_id=None): + rp = returned_properties + + filter_rule = None + if projectarea_name is not None: + fpaname_rule = ("dc:title", None, projectarea_name) + filter_rule = self._add_filter_rule(filter_rule, fpaname_rule) + + if projectarea_id is not None: + paid_url = "/".join([self.url, "oslc/projectareas", + projectarea_id]) + fpaid_rule = ("@rdf:resource", None, paid_url) + filter_rule = self._add_filter_rule(filter_rule, fpaid_rule) + + return self._get_paged_resources("ProjectArea", + page_size="10", + archived=archived, + returned_properties=rp, + filter_rule=filter_rule) + + def _add_filter_rule(self, filter_rule, added_rule): + if filter_rule is None: + filter_rule = [added_rule] + else: + filter_rule.append(added_rule) + return filter_rule + def getProjectAreaByID(self, projectarea_id, archived=False, returned_properties=None): """Get :class:`rtcclient.project_area.ProjectArea` object by its id @@ -160,22 +185,22 @@ def getProjectAreaByID(self, projectarea_id, archived=False, :rtype: rtcclient.project_area.ProjectArea """ - self.log.debug("Try to get by its id: %s", - projectarea_id) if not isinstance(projectarea_id, six.string_types) or not projectarea_id: excp_msg = "Please specify a valid ProjectArea ID" self.log.error(excp_msg) raise exception.BadValue(excp_msg) + self.log.debug("Try to get by its id: %s", + projectarea_id) rp = returned_properties - proj_areas = self.getProjectAreas(archived=archived, - returned_properties=rp) + proj_areas = self._getProjectAreas(archived=archived, + returned_properties=rp, + projectarea_id=projectarea_id) if proj_areas is not None: - for proj_area in proj_areas: - if proj_area.id == projectarea_id: - self.log.info("Find ", proj_area) - return proj_area + proj_area = proj_areas[0] + self.log.info("Find ", proj_area) + return proj_area self.log.error("No ProjectArea's ID is %s", projectarea_id) raise exception.NotFound("No ProjectArea's ID is %s" % projectarea_id) @@ -244,14 +269,14 @@ def checkProjectAreaID(self, projectarea_id, archived=False): self.log.debug("Check the validity of the ProjectArea id: %s", projectarea_id) - proj_areas = self.getProjectAreas(archived=archived) + proj_areas = self._getProjectAreas(archived=archived, + projectarea_id=projectarea_id) if proj_areas is not None: - for proj_area in proj_areas: - if proj_area.id == projectarea_id: - self.log.info("Find whose id is: %s", - proj_area, - projectarea_id) - return True + proj_area = proj_areas[0] + self.log.info("Find whose id is: %s", + proj_area, + projectarea_id) + return True self.log.error("No ProjectArea whose id is: %s", projectarea_id) @@ -280,23 +305,23 @@ def getTeamArea(self, teamarea_name, projectarea_id=None, :rtype: rtcclient.models.TeamArea """ - self.log.debug("Try to get ", teamarea_name) if not isinstance(teamarea_name, six.string_types) or not teamarea_name: excp_msg = "Please specify a valid TeamArea name" self.log.error(excp_msg) raise exception.BadValue(excp_msg) - teamareas = self.getTeamAreas(projectarea_id=projectarea_id, - projectarea_name=projectarea_name, - archived=archived, - returned_properties=returned_properties) + self.log.debug("Try to get ", teamarea_name) + teamareas = self._getTeamAreas(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived, + returned_properties=returned_properties, + teamarea_name=teamarea_name) if teamareas is not None: - for teamarea in teamareas: - if teamarea.title == teamarea_name: - self.log.info("Find ", teamarea) - return teamarea + teamarea = teamareas[0] + self.log.info("Find ", teamarea) + return teamarea self.log.error("No TeamArea named %s", teamarea_name) raise exception.NotFound("No TeamArea named %s" % teamarea_name) @@ -324,15 +349,30 @@ def getTeamAreas(self, projectarea_id=None, projectarea_name=None, :rtype: list """ + return self._getTeamAreas(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived, + returned_properties=returned_properties) + + def _getTeamAreas(self, projectarea_id=None, projectarea_name=None, + archived=False, returned_properties=None, + teamarea_name=None): + projarea_id = self._pre_get_resource(projectarea_id=projectarea_id, projectarea_name=projectarea_name) - rp = returned_properties + + filter_rule = None + if teamarea_name is not None: + ftaname_rule = ("dc:title", None, teamarea_name) + filter_rule = self._add_filter_rule(filter_rule, ftaname_rule) + return self._get_paged_resources("TeamArea", projectarea_id=projarea_id, page_size="100", archived=archived, - returned_properties=rp) + returned_properties=rp, + filter_rule=filter_rule) def getOwnedBy(self, email, projectarea_id=None, projectarea_name=None): @@ -371,24 +411,24 @@ def getPlannedFor(self, plannedfor_name, projectarea_id=None, :rtype: rtcclient.models.PlannedFor """ - self.log.debug("Try to get ", plannedfor_name) if not isinstance(plannedfor_name, six.string_types) or not plannedfor_name: excp_msg = "Please specify a valid PlannedFor name" self.log.error(excp_msg) raise exception.BadValue(excp_msg) + self.log.debug("Try to get ", plannedfor_name) rp = returned_properties - plannedfors = self.getPlannedFors(projectarea_id=projectarea_id, - projectarea_name=projectarea_name, - archived=archived, - returned_properties=rp) + plannedfors = self._getPlannedFors(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived, + returned_properties=rp, + plannedfor_name=plannedfor_name) if plannedfors is not None: - for plannedfor in plannedfors: - if plannedfor.title == plannedfor_name: - self.log.info("Find ", plannedfor) - return plannedfor + plannedfor = plannedfors[0] + self.log.info("Find ", plannedfor) + return plannedfor self.log.error("No PlannedFor named %s", plannedfor_name) raise exception.NotFound("No PlannedFor named %s" % plannedfor_name) @@ -416,15 +456,30 @@ def getPlannedFors(self, projectarea_id=None, projectarea_name=None, :rtype: list """ + return self._getPlannedFors(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived, + returned_properties=returned_properties) + + def _getPlannedFors(self, projectarea_id=None, projectarea_name=None, + archived=False, returned_properties=None, + plannedfor_name=None): + projarea_id = self._pre_get_resource(projectarea_id=projectarea_id, projectarea_name=projectarea_name) + filter_rule = None + if plannedfor_name is not None: + fpfname_rule = ("dc:title", None, plannedfor_name) + filter_rule = self._add_filter_rule(filter_rule, fpfname_rule) + rp = returned_properties return self._get_paged_resources("PlannedFor", projectarea_id=projarea_id, page_size="100", archived=archived, - returned_properties=rp) + returned_properties=rp, + filter_rule=filter_rule) def getSeverity(self, severity_name, projectarea_id=None, projectarea_name=None): @@ -447,14 +502,14 @@ def getSeverity(self, severity_name, projectarea_id=None, self.log.error(excp_msg) raise exception.BadValue(excp_msg) - severities = self.getSeverities(projectarea_id=projectarea_id, - projectarea_name=projectarea_name) + severities = self._getSeverities(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + severity_name=severity_name) if severities is not None: - for severity in severities: - if severity.title == severity_name: - self.log.info("Find ", severity) - return severity + severity = severities[0] + self.log.info("Find ", severity) + return severity self.log.error("No Severity named %s", severity_name) raise exception.NotFound("No Severity named %s" % severity_name) @@ -476,6 +531,11 @@ def getSeverities(self, projectarea_id=None, projectarea_name=None): :rtype: list """ + return self._getSeverities(projectarea_id=projectarea_id, + projectarea_name=projectarea_name) + + def _getSeverities(self, projectarea_id=None, projectarea_name=None, + severity_name=None): projarea_id = self._pre_get_resource(projectarea_id=projectarea_id, projectarea_name=projectarea_name) if projarea_id is None: @@ -483,9 +543,16 @@ def getSeverities(self, projectarea_id=None, projectarea_name=None): "projectarea_id and projectarea_name") raise exception.EmptyAttrib("At least input either-or between " "projectarea_id and projectarea_name") + + filter_rule = None + if severity_name is not None: + fsname_rule = ("dc:title", None, severity_name) + filter_rule = self._add_filter_rule(filter_rule, fsname_rule) + return self._get_paged_resources("Severity", projectarea_id=projarea_id, - page_size="10") + page_size="10", + filter_rule=filter_rule) def getPriority(self, priority_name, projectarea_id=None, projectarea_name=None): @@ -508,14 +575,14 @@ def getPriority(self, priority_name, projectarea_id=None, self.log.error(excp_msg) raise exception.BadValue(excp_msg) - priorities = self.getPriorities(projectarea_id=projectarea_id, - projectarea_name=projectarea_name) + priorities = self._getPriorities(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + priority_name=priority_name) if priorities is not None: - for priority in priorities: - if priority.title == priority_name: - self.log.info("Find ", priority) - return priority + priority = priorities[0] + self.log.info("Find ", priority) + return priority self.log.error("No Priority named %s", priority_name) raise exception.NotFound("No Priority named %s" % priority_name) @@ -537,6 +604,11 @@ def getPriorities(self, projectarea_id=None, projectarea_name=None): :rtype: list """ + return self._getPriorities(projectarea_id=projectarea_id, + projectarea_name=projectarea_name) + + def _getPriorities(self, projectarea_id=None, projectarea_name=None, + priority_name=None): projarea_id = self._pre_get_resource(projectarea_id=projectarea_id, projectarea_name=projectarea_name) if projarea_id is None: @@ -544,9 +616,16 @@ def getPriorities(self, projectarea_id=None, projectarea_name=None): "projectarea_id and projectarea_name") raise exception.EmptyAttrib("At least input either-or between " "projectarea_id and projectarea_name") + + filter_rule = None + if priority_name is not None: + fpname_rule = ("dc:title", None, priority_name) + filter_rule = self._add_filter_rule(filter_rule, fpname_rule) + return self._get_paged_resources("Priority", projectarea_id=projarea_id, - page_size="10") + page_size="10", + filter_rule=filter_rule) def getFoundIn(self, foundin_name, projectarea_id=None, projectarea_name=None, archived=False): @@ -568,15 +647,15 @@ def getFoundIn(self, foundin_name, projectarea_id=None, self.log.error(excp_msg) raise exception.BadValue(excp_msg) - foundins = self.getFoundIns(projectarea_id=projectarea_id, - projectarea_name=projectarea_name, - archived=archived) + foundins = self._getFoundIns(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived, + foundin_name=foundin_name) if foundins is not None: - for foundin in foundins: - if foundin.title == foundin_name: - self.log.info("Find ", foundin) - return foundin + foundin = foundins[0] + self.log.info("Find ", foundin) + return foundin self.log.error("No FoundIn named %s", foundin_name) raise exception.NotFound("No FoundIn named %s" % foundin_name) @@ -601,12 +680,24 @@ def getFoundIns(self, projectarea_id=None, projectarea_name=None, :rtype: list """ + return self._getFoundIns(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived) + + def _getFoundIns(self, projectarea_id=None, projectarea_name=None, + archived=False, foundin_name=None): projarea_id = self._pre_get_resource(projectarea_id=projectarea_id, projectarea_name=projectarea_name) + + filter_rule = None + if foundin_name is not None: + ffname_rule = ("dc:title", None, foundin_name) + filter_rule = self._add_filter_rule(filter_rule, ffname_rule) return self._get_paged_resources("FoundIn", projectarea_id=projarea_id, page_size="100", - archived=archived) + archived=archived, + filter_rule=filter_rule) def getFiledAgainst(self, filedagainst_name, projectarea_id=None, projectarea_name=None, archived=False): @@ -629,15 +720,15 @@ def getFiledAgainst(self, filedagainst_name, projectarea_id=None, self.log.error(excp_msg) raise exception.BadValue(excp_msg) - fas = self.getFiledAgainsts(projectarea_id=projectarea_id, - projectarea_name=projectarea_name, - archived=archived) + fas = self._getFiledAgainsts(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived, + filedagainst_name=filedagainst_name) if fas is not None: - for filedagainst in fas: - if filedagainst.title == filedagainst_name: - self.log.info("Find ", filedagainst) - return filedagainst + filedagainst = fas[0] + self.log.info("Find ", filedagainst) + return filedagainst error_msg = "No FiledAgainst named %s" % filedagainst_name self.log.error(error_msg) @@ -664,12 +755,24 @@ def getFiledAgainsts(self, projectarea_id=None, projectarea_name=None, :rtype: list """ + return self._getFiledAgainsts(projectarea_id=projectarea_id, + projectarea_name=projectarea_name, + archived=archived) + + def _getFiledAgainsts(self, projectarea_id=None, projectarea_name=None, + archived=False, filedagainst_name=None): projarea_id = self._pre_get_resource(projectarea_id=projectarea_id, projectarea_name=projectarea_name) + + filter_rule = None + if filedagainst_name is not None: + ffaname_rule = ("dc:title", None, filedagainst_name) + filter_rule = self._add_filter_rule(filter_rule, ffaname_rule) return self._get_paged_resources("FiledAgainst", projectarea_id=projarea_id, page_size="100", - archived=archived) + archived=archived, + filter_rule=filter_rule) def getTemplate(self, copied_from, template_name=None, template_folder=None, keep=False, encoding="UTF-8"): diff --git a/rtcclient/project_area.py b/rtcclient/project_area.py index 7d026c5..6bf3a5e 100644 --- a/rtcclient/project_area.py +++ b/rtcclient/project_area.py @@ -42,6 +42,9 @@ def getRoles(self): :rtype: list """ + # no need to retrieve all the entries from _get_paged_resources + # role raw data is very simple that contains no other links + self.log.info("Get all the roles in ", self) roles_url = "/".join([self.rtc_obj.url, @@ -104,13 +107,7 @@ def getMembers(self, returned_properties=None): :rtype: list """ - self.log.warning("If you are not listed, please contact your RTC " - "administrators to add you as a team member") - rp = returned_properties - return self.rtc_obj._get_paged_resources("Member", - projectarea_id=self.id, - page_size='100', - returned_properties=rp) + return self._getMembers(returned_properties=returned_properties) def getMember(self, email, returned_properties=None): """Get the :class:`rtcclient.models.Member` object by the @@ -128,24 +125,35 @@ def getMember(self, email, returned_properties=None): self.log.error(excp_msg) raise exception.BadValue(excp_msg) - members = self.getMembers(returned_properties=returned_properties) - self.log.warning("Some members that do exist in or belong to" - "the ProjectArea may cannot be retrieved") - self.log.warning("This is an existing bug of RTC") - self.log.debug("Try to get Member whose email is %s>", email) + members = self._getMembers(returned_properties=returned_properties, + email=email) if members is not None: - for member in members: - if member.email == email: - self.log.info("Get in ", - member, self) - return member + member = members[0] + self.log.info("Get in ", + member, self) + return member excp_msg = "No member's email is %s in " % (email, self) self.log.error(excp_msg) raise exception.NotFound(excp_msg) + def _getMembers(self, returned_properties=None, email=None): + self.log.warning("If you are not listed, please contact your RTC " + "administrators to add you as a team member") + rp = returned_properties + filter_rule = None + if email is not None: + fmember_rule = ("rtc_cm:userId", None, email) + filter_rule = self.rtc_obj._add_filter_rule(filter_rule, + fmember_rule) + return self.rtc_obj._get_paged_resources("Member", + projectarea_id=self.id, + page_size='100', + returned_properties=rp, + filter_rule=filter_rule) + def getItemTypes(self, returned_properties=None): """Get all the :class:`rtcclient.models.ItemType` objects in this project area @@ -159,11 +167,7 @@ def getItemTypes(self, returned_properties=None): :rtype: list """ - rp = returned_properties - return self.rtc_obj._get_paged_resources("ItemType", - projectarea_id=self.id, - page_size='10', - returned_properties=rp) + return self._getItemTypes(returned_properties=returned_properties) def getItemType(self, title, returned_properties=None): """Get the :class:`rtcclient.models.ItemType` object by the title @@ -180,20 +184,33 @@ def getItemType(self, title, returned_properties=None): self.log.error(excp_msg) raise exception.BadValue(excp_msg) - itemtypes = self.getItemTypes(returned_properties=returned_properties) self.log.debug("Try to get ", title) + itemtypes = self._getItemTypes(returned_properties=returned_properties, + title=title) if itemtypes is not None: - for itemtype in itemtypes: - if itemtype.title == title: - self.log.info("Get in ", - itemtype, self) - return itemtype + itemtype = itemtypes[0] + self.log.info("Get in ", + itemtype, self) + return itemtype excp_msg = "No itemtype's name is %s in " % (title, self) self.log.error(excp_msg) raise exception.NotFound(excp_msg) + def _getItemTypes(self, returned_properties=None, title=None): + rp = returned_properties + filter_rule = None + if title is not None: + fit_rule = ("dc:title", None, title) + filter_rule = self.rtc_obj._add_filter_rule(filter_rule, + fit_rule) + return self.rtc_obj._get_paged_resources("ItemType", + projectarea_id=self.id, + page_size='10', + returned_properties=rp, + filter_rule=filter_rule) + def getAdministrators(self, returned_properties=None): """Get all the :class:`rtcclient.models.Administrator` objects in this project area @@ -208,11 +225,7 @@ def getAdministrators(self, returned_properties=None): :rtype: list """ - rp = returned_properties - return self.rtc_obj._get_paged_resources("Administrator", - projectarea_id=self.id, - page_size='10', - returned_properties=rp) + return self._getAdministrators(returned_properties=returned_properties) def getAdministrator(self, email, returned_properties=None): """Get the :class:`rtcclient.models.Administrator` object @@ -230,18 +243,31 @@ def getAdministrator(self, email, returned_properties=None): self.log.error(excp_msg) raise exception.BadValue(excp_msg) - rp = returned_properties - administrators = self.getAdministrators(returned_properties=rp) self.log.debug("Try to get Administrator whose email is %s", email) + rp = returned_properties + administrators = self._getAdministrators(returned_properties=rp, + email=email) if administrators is not None: - for administrator in administrators: - if administrator.email == email: - self.log.info("Get in ", - administrator, self) - return administrator + administrator = administrators[0] + self.log.info("Get in ", + administrator, self) + return administrator msg = "No administrator's email is %s in " % (email, self) self.log.error(msg) raise exception.NotFound(msg) + + def _getAdministrators(self, returned_properties=None, email=None): + rp = returned_properties + filter_rule = None + if email is not None: + fadmin_rule = ("rtc_cm:userId", None, email) + filter_rule = self.rtc_obj._add_filter_rule(filter_rule, + fadmin_rule) + return self.rtc_obj._get_paged_resources("Administrator", + projectarea_id=self.id, + page_size='10', + returned_properties=rp, + filter_rule=filter_rule) diff --git a/rtcclient/workitem.py b/rtcclient/workitem.py index 614d0d2..c2615d1 100644 --- a/rtcclient/workitem.py +++ b/rtcclient/workitem.py @@ -368,13 +368,7 @@ def getActions(self): :rtype: list """ - cust_attr = (self.raw_data.get("rtc_cm:state") - .get("@rdf:resource") - .split("/")[-2]) - return self.rtc_obj._get_paged_resources("Action", - projectarea_id=self.contextId, - customized_attr=cust_attr, - page_size="100") + return self._getActions() def getAction(self, action_name): """Get the :class:`rtcclient.models.Action` object by its name @@ -390,17 +384,32 @@ def getAction(self, action_name): self.log.error(excp_msg) raise exception.BadValue(excp_msg) - actions = self.getActions() + actions = self._getActions(action_name=action_name) if actions is not None: - for action in actions: - if action.title == action_name: - self.log.info("Find ", action) - return action + action = actions[0] + self.log.info("Find ", action) + return action self.log.error("No Action named %s", action_name) raise exception.NotFound("No Action named %s" % action_name) + def _getActions(self, action_name=None): + filter_rule = None + if action_name is not None: + faction_rule = ("dc:title", None, action_name) + filter_rule = self.rtc_obj._add_filter_rule(filter_rule, + faction_rule) + + cust_attr = (self.raw_data.get("rtc_cm:state") + .get("@rdf:resource") + .split("/")[-2]) + return self.rtc_obj._get_paged_resources("Action", + projectarea_id=self.contextId, + customized_attr=cust_attr, + page_size="100", + filter_rule=filter_rule) + def getStates(self): """Get all :class:`rtcclient.models.State` objects of this workitem