def test_get_cycle_task_dict(self): contract = ContractFactory(title=u"Contract1") cycle_task = CycleTaskFactory(title=u"task1") relationship = RelationshipFactory(source=contract, destination=cycle_task) db.session.delete(relationship) db.session.commit() relationship_revision = Revision(obj=relationship, modified_by_id=None, action="deleted", content="{}") contract_revision = Revision(obj=contract, modified_by_id=None, action="deleted", content='{"display_name": "Contract1"}') revisions = [relationship_revision, contract_revision] EventFactory(modified_by_id=None, action="DELETE", resource_id=relationship.id, resource_type=relationship.type, context_id=None, revisions=revisions) task_dict = get_cycle_task_dict(cycle_task) self.assertEqual(task_dict["related_objects"][0], u"Contract1 [removed from task]") # Test if we handle the title of the object being empty contract = ContractFactory(title=u"") cycle_task = CycleTaskFactory(title=u"task1") relationship = RelationshipFactory(source=contract, destination=cycle_task) task_dict = get_cycle_task_dict(cycle_task) self.assertEqual(task_dict["related_objects"][0], u"Untitled object")
def _revision_generator(user_id, action, objects): """Geberate and return revisions for objects.""" for obj in objects: revision = Revision(obj, user_id, action, obj.log_json()) if isinstance(obj, Synchronizable): revision.created_at = obj.updated_at revision.updated_at = obj.updated_at yield revision
def test_ct_without_revisions_error(self, logger): """Tests that notifications for CycleTask without revisions are handled properly.""" contract = factories.ContractFactory(title=u"Test Contract") cycle_task = wf_factories.CycleTaskGroupObjectTaskFactory( title=u"task1") relationship = factories.RelationshipFactory(source=contract, destination=cycle_task) db.session.delete(relationship) db.session.commit() relationship_revision = Revision(obj=relationship, modified_by_id=None, action="deleted", content="{}") revisions = [relationship_revision] factories.EventFactory(modified_by_id=login.get_current_user_id(), action="DELETE", resource_id=relationship.id, resource_type=relationship.type, revisions=revisions) contract_revision = db.session.query(Revision).filter( Revision.resource_type == "Contract", Revision.action == "created", Revision.resource_id == contract.id).one() db.session.delete(contract_revision) db.session.commit() get_cycle_task_dict(cycle_task) logger.warning.assert_called_once_with( "Unmapped %s id %s from CycleTask id %s has no revisions logged. ", "Contract", contract.id, cycle_task.id)
def test_get_cycle_task_dict(self): """Tests get_cycle_task_dict functionality.""" contract = factories.ContractFactory(title=u"Contract1") cycle_task = wf_factories.CycleTaskGroupObjectTaskFactory( title=u"task1") relationship = factories.RelationshipFactory(source=contract, destination=cycle_task) db.session.delete(relationship) db.session.commit() relationship_revision = Revision(obj=relationship, modified_by_id=None, action="deleted", content="{}") contract_revision = Revision(obj=contract, modified_by_id=None, action="deleted", content='{"display_name": "Contract1"}') revisions = [relationship_revision, contract_revision] factories.EventFactory(modified_by_id=login.get_current_user_id(), action="DELETE", resource_id=relationship.id, resource_type=relationship.type, revisions=revisions) task_dict = get_cycle_task_dict(cycle_task) self.assertEqual(task_dict["related_objects"][0], u"Contract1 [removed from task]") # Test if we handle the title of the object being empty contract = factories.ContractFactory(title=u"") cycle_task = wf_factories.CycleTaskGroupObjectTaskFactory( title=u"task1") factories.RelationshipFactory(source=contract, destination=cycle_task) task_dict = get_cycle_task_dict(cycle_task) self.assertEqual(task_dict["related_objects"][0], u"Untitled object")
def _get_log_revisions(current_user_id, obj=None, force_obj=False): """Generate and return revisions for all cached objects.""" revisions = [] cache = Cache.get_cache() if not cache: return revisions modified_objects = set(cache.dirty) new_objects = set(cache.new) delete_objects = set(cache.deleted) all_edited_objects = itertools.chain(cache.new, cache.dirty, cache.deleted) for o in all_edited_objects: if o.type == "ObjectFolder" and o.folderable: modified_objects.add(o.folderable) if o.type == "Relationship" and o.get_related_for("Document"): documentable = o.get_related_for("Document") document = o.get_related_for(documentable.type) if o in new_objects and document not in documentable.documents: documentable.documents.append(document) if o in delete_objects and document in documentable.documents: documentable.documents.remove(document) if ( documentable not in new_objects and documentable not in delete_objects): modified_objects.add(documentable) revisions.extend(_revision_generator( current_user_id, "created", cache.new )) revisions.extend(_revision_generator( current_user_id, "modified", modified_objects )) if force_obj and obj is not None and obj not in cache.dirty: # If the ``obj`` has been updated, but only its custom attributes have # been changed, then this object will not be added into # ``cache.dirty set``. So that its revision will not be created. # The ``force_obj`` flag solves the issue, but in a bit dirty way. revision = Revision(obj, current_user_id, 'modified', obj.log_json()) revisions.append(revision) revisions.extend(_revision_generator( current_user_id, "deleted", cache.deleted )) return revisions
def _revision_generator(user_id, action, objects): for obj in objects: yield Revision(obj, user_id, action, obj.log_json())