def get_requests(cls, organization, domain=None, user_id=None): key = [organization] if domain: key.append(domain) if user_id: key.append(user_id) # todo - forcing invalidating on all requests while we turn these features on slowly results = cache_core.cached_view( cls.get_db(), "orgs/org_requests", startkey=key, endkey=key + [{}], reduce=False, include_docs=True, wrapper=cls.wrap, ) #return results.all() if not user_id else results.one() if not user_id: return results else: try: length = len(results) if length == 1: return results[0] elif length > 0: raise MultipleResultsFound( "OrgRequests found multiple results for %s" % key) except IndexError: return None
def one(self): """ Return exactly one result or raise an exception. """ length = len(self) if length > 1: raise MultipleResultsFound("%s results found." % length) return self.first()
def get_open_sms_session(cls, domain, contact_id): """ Looks up the open sms survey session for the given domain and contact_id. Only one session is expected to be open at a time. Raises MultipleResultsFound if more than one session is open. """ objs = cls.get_all_open_sms_sessions(domain, contact_id).all() if len(objs) > 1: raise MultipleResultsFound('more than 1 ({}) session found for domain {} and contact {}'.format( len(objs), domain, contact_id )) elif len(objs) == 0: return None return objs[0]
def get_by_name(cls, name, strict=False): extra_args = { 'stale': settings.COUCH_STALE_QUERY } if not strict else {} results = cache_core.cached_view(cls.get_db(), "orgs/by_name", key=name, reduce=False, include_docs=True, wrapper=cls.wrap, **extra_args) length = len(results) if length > 1: raise MultipleResultsFound( "Error, Organization.get_by_name returned more than 1 result for %s" % name) elif length == 1: return list(results)[0] else: return None