diff --git a/docs/workitem.rst b/docs/workitem.rst index 9d4f278..8806c8f 100644 --- a/docs/workitem.rst +++ b/docs/workitem.rst @@ -223,6 +223,20 @@ The workitem class implements the __eq__ method allowing it to be compared. if workitem1 == workitem2: #... +Context Manager +^^^^^^^^^^^^^^^ + +It is possible to use the context manager with a workitem. +This is useful, when updating many attributes of it. +Execution speed increases, because the workitem is only updated / saved once, when exiting the context manager. + +.. code:: python + + with workitem as wi: + wi.addAttachment(path_to_file, 'file title') + wi.addComment('comment') + # any many more + List of available attributes ---------------------------- diff --git a/polarion/record.py b/polarion/record.py index cf244ab..84190eb 100644 --- a/polarion/record.py +++ b/polarion/record.py @@ -30,16 +30,16 @@ def __init__(self, polarion, test_run, polarion_record, index): self._polarion_record = polarion_record self._index = index - self._skip_save = False + self._postpone_save = False self._buildWorkitemFromPolarion() def __enter__(self): - self._skip_save = True + self._postpone_save = True return self def __exit__(self, exc_type, exc_val, exc_tb): - self._skip_save = False + self._postpone_save = False self.save() def _buildWorkitemFromPolarion(self): @@ -301,7 +301,7 @@ def save(self): """ Saves the current test record """ - if self._skip_save: + if self._postpone_save: return new_item = {} diff --git a/polarion/workitem.py b/polarion/workitem.py index e5cf38f..56a895d 100644 --- a/polarion/workitem.py +++ b/polarion/workitem.py @@ -36,6 +36,7 @@ def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None, self._project = project self._id = id self._uri = uri + self._postpone_save = False service = self._polarion.getService('Tracker') @@ -88,6 +89,14 @@ def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None, self._buildWorkitemFromPolarion() + def __enter__(self): + self._postpone_save = True + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self._postpone_save = False + self.save() + def _buildWorkitemFromPolarion(self): if self._polarion_item is not None and not self._polarion_item.unresolvable: self._original_polarion = copy.deepcopy(self._polarion_item) @@ -774,6 +783,8 @@ def save(self): """ Update the workitem in polarion """ + if self._postpone_save: + return updated_item = {} for attr, value in self._polarion_item.__dict__.items():