def query_1(self, query, params=()): """ Perform an SQL query that should yield at most one row. Like query(), but: 1. The method can raise the exceptions NotFoundError (indicating a query returned no rows) or TooManyRowsError (query returned more than one row). 2. When the query returns a single row, but each returned row contains multiple columns, the method will return a single row object. 3. Otherwise (i.e. the query returns a single row with a single column) the method will return the value within the row object (and not the row object itself). """ res = self.query(query, params) if len(res) == 1: if len(res[0]) == 1: return res[0][0] return res[0] elif len(res) == 0: raise Errors.NotFoundError(repr(params)) else: raise Errors.TooManyRowsError(repr(params))
def ephorte_set_standard_role(self, operator, person_id, role, sko, arkivdel, journalenhet): """ Set given role as standard role. There can be only one standard role, thus if another role is marked as standard role, it will no longer be the persons standard role. """ # Check operators permissions if not self.ba.can_add_ephorte_role(operator.get_entity_id()): raise PermissionDenied("Currently limited to ephorte admins") try: person = self.util.get_target(person_id, restrict_to=['Person']) except Errors.TooManyRowsError: raise CerebrumError("Unexpectedly found more than one person") ou = self._get_ou(stedkode=sko) # Check that the person has the given role. tmp = self.ephorte_role.get_role(person.entity_id, self._get_role_type(role), ou.entity_id, self._get_arkivdel(arkivdel), self._get_journalenhet(journalenhet)) # Some sanity checks if not tmp: raise CerebrumError("Person has no such role") elif len(tmp) > 1: raise Errors.TooManyRowsError("Unexpectedly found more than one" " role") new_std_role = tmp[0] if new_std_role['standard_role'] == 'T': return "Role is already standard role" # There can be only one standard role for row in self.ephorte_role.list_roles(person_id=person.entity_id): if row['standard_role'] == 'T': self.logger.debug("Unset role %s at %s as standard_role" % ( row['role_type'], row['adm_enhet'])) self.ephorte_role.set_standard_role_val(person.entity_id, row['role_type'], row['adm_enhet'], row['arkivdel'], row['journalenhet'], 'F') # Finally set the new standard role self.ephorte_role.set_standard_role_val( person.entity_id, self._get_role_type(role), ou.entity_id, self._get_arkivdel(arkivdel), self._get_journalenhet(journalenhet), 'T') return "OK, set new standard role"
def find_by_tsd_projectname(self, project_name): """Finds Project OU by project name. This is a L{find}-method, it will populate this entity with any project it finds. :param str project_name: The short-name of the project to find. :raise NotFoundError: If the project is not found. :raise TooManyRowsError: If multiple projects matches the name (should not be possible). """ matched = self.search_tsd_projects(name=project_name, exact_match=True) if not matched: raise Errors.NotFoundError(u"Unknown project: %s" % project_name) if len(matched) != 1: raise Errors.TooManyRowsError( u"Found several OUs with given name: %s" % project_name) return self.find(matched[0]['entity_id'])