def set_cache_timeout(cls, timeout): """Set the cache timeout for this object type. :param timeout: The timeout to set against this object type. """ get_accessor().set_cache_timeout(cls.rally_name, timeout)
def create_from_ref(cls, reference): """Create an instance of ``cls`` by getting data for ``reference``. :param reference: The full url reference to make an api call with. :returns: A populated :py:exc:`~pyrally.models.BaseRallyModel` inheriting instance which matches the reference. :raises: A :py:class:`~pyrally.models.ReferenceNotFoundException` if we get any error back from the API. """ response = get_accessor().make_api_call(reference, True) errors = response.get('OperationResult', {'Errors': []}).get('Errors') if errors: msg = """ Failure: {0}-{1}: {2}. If you called this directly, check that you've got the right reference number. Otherwise, it's highly likely that the reference was left hanging around in Rally after a delete (eg if a User was deleted). """.format(cls, reference, errors) raise ReferenceNotFoundException(msg) return cls(response[cls.rally_name])
def _get_results_page(cls, query_string, start_index=1): """ Get a page of results for the query given. :param query_string: The string to get results for. If ``None``, or ``""``, no query is passed in and all objects for the class will be returned. :param start_index: The 1-based offset to fetch from. A pagesize of 100 is returned. :returns: The QueryResult entity as returned by the API, containing at most 100 results. :raises: Exception if ['QueryResult']['Errors'] contains anything. """ query_arg = "" if query_string: query_arg = "query=({0})&".format(query_string) url = "{0}.js?{1}pagesize=100&start={2}&fetch=true".format( cls.rally_name.lower(), query_arg, start_index) query_result_dict = get_accessor().make_api_call(url) if query_result_dict['QueryResult']['Errors']: raise Exception('Errors in query: {0}'.format( query_result_dict['QueryResult']['Errors'])) return query_result_dict['QueryResult']
def test_get_accessor_returns_created_accessor_if_available(RallyAccessor, ACCESSOR): """ Test ``get_accessor`` returns created. Test that :py:func:`~pyrally.rally_access.get_accessor` returns the created accessor if it is available. """ assert_equal(get_accessor(), ACCESSOR) assert_false(RallyAccessor.called)
def test_get_accessor_returns_created_accessor_if_available( RallyAccessor, ACCESSOR): """ Test ``get_accessor`` returns created. Test that :py:func:`~pyrally.rally_access.get_accessor` returns the created accessor if it is available. """ assert_equal(get_accessor(), ACCESSOR) assert_false(RallyAccessor.called)
def test_get_accessor_creates_accessor_if_not_already_created(RallyAccessor): """ Test ``get_accessor`` returns created. Test that :py:func:`~pyrally.rally_access.get_accessor` creates and returns a :py:class:`~pyrally.rally_access.RallyAccessor` object if it has not already been created. """ assert_equal(get_accessor('uname', 'pword', 'base_url'), RallyAccessor.return_value) assert_equal(RallyAccessor.call_args[0], ('uname', 'pword', 'base_url'))
def get_results_page(query_string, start_index=1): query_arg = "" if query_string: query_arg = "query=({0})&".format(query_string) url = "{0}.js?{1}pagesize=100&start={2}&fetch=true".format( cls.rally_name.lower(), query_arg, start_index) query_result_dict = get_accessor().make_api_call(url) if query_result_dict['QueryResult']['Errors']: raise Exception('Errors in query: {0}'.format( query_result_dict['QueryResult']['Errors'])) return query_result_dict['QueryResult']
def delete(self): """Delete this object from Rally""" if not self.ref: raise Exception("Cannot delete item that is not synced with Rally." " If you've just created this item try running" "update_rally().") response = get_accessor().make_api_call(self.ref, True, method='DELETE') if response['OperationResult']['Errors']: raise Exception('Errors in delete: {0}'.format( response['OperationResult']['Errors'])) self.delete_from_cache()
def create_from_ref(cls, reference): response = get_accessor().make_api_call(reference, True) errors = response.get('OperationResult', {'Errors': []}).get('Errors') if errors: msg = """ Failure: {0}-{1}: {2}. If you called this directly, check that you've got the right reference number. Otherwise, it's highly likely that the reference was left hanging around in Rally after a delete (eg if a User was deleted). """.format(cls, reference, errors) raise ReferenceNotFoundException(msg) return cls(response[cls.rally_name])
def update_rally(self, refresh=True): """Update or create ``self`` on Rally. If there is no sign of a _ref internally, a create is sent, otherwise an update is sent. :param refresh: Used on creates. If ``True`` a fetch is performed after the object is created in Rally and ``self`` is populated with the new data. :returns: The response from the server. """ if self.ref: # Do update data = {self.rally_name: self.rally_data} response = get_accessor().make_api_call(self.ref, True, method='POST', data=data) if response['OperationResult']['Errors']: raise Exception('Errors in query: {0}'.format( response['OperationResult']['Errors'])) else: data = {self.rally_name: self.rally_data} url = "{0}/create.js".format(self.rally_name.lower()) response = get_accessor().make_api_call(url, method='POST', data=data) if response['CreateResult']['Errors']: raise Exception('Errors in query: {0}'.format( response['CreateResult']['Errors'])) reference = response['CreateResult']['Object']['_ref'] if refresh: self.delete_from_cache() new_data = get_accessor().make_api_call(reference, True) self.rally_data = new_data[self.rally_name] return response
def delete_from_cache(self): """Remove this item from the cache""" get_accessor().delete_from_cache(self.rally_name, self.ref)
def __init__(self, username=None, password=None, base_url=None, apikey=None): self.rally_access = get_accessor(username, password, base_url, apikey)
def rally_url(self): defect_id = self.ref.split('/')[-1].replace('.js', '') base_url = get_accessor().base_url url = "{0}slm/rally.sp#/detail/defect/{1}".format(base_url, defect_id) return url
def __init__(self, username, password): self.rally_access = get_accessor(username, password)
def __init__(self, username, password, base_url): self.rally_access = get_accessor(username, password, base_url)