def _process_chunk(chunk, curr_res=None, curr_res_meta=None, prev_rev_content=None): """Process chunk of revisions and find empty.""" result = [] for rev in chunk: if _resource_differ(curr_res, rev): curr_res = _query_new_res(rev.resource_type, rev.resource_id) if curr_res is None: continue prev_rev_content = _query_first_rev_content( rev.resource_type, rev.resource_id) if prev_rev_content is None: continue curr_res_meta = meta_info.MetaInfo(curr_res) curr_rev_content = rev.content changes_present = revisions_diff.changes_present( obj=curr_res, new_rev_content=curr_rev_content, prev_rev_content=prev_rev_content, ) if not changes_present: result.append(rev) else: prev_rev_content = curr_rev_content state = dict(curr_res=curr_res, curr_res_meta=curr_res_meta, prev_rev_content=prev_rev_content) return result, state
def _handle_if_empty(self): """Check if revision is empty and update is_empty flag if true.""" # Check if new revision contains any changes in resource state. Revisions # created with "created" or "deleted" action are not considered empty. if self in db.session.new and self.action == u"modified": obj = referenced_objects.get(self.resource_type, self.resource_id) # Content serialization and deserialization is needed since content of # prev revision stored in DB was serialized before storing and due to # this couldn't be correctly compared to content of revision in hands. content = json.loads(utils.as_json(self.content)) self.is_empty = bool( obj and not revisions_diff.changes_present(obj, content))
def create_revisions(resources, event_id, user_id, action): """Create revisions for provided objects in bulk. Args: resources: [(obj_type, obj_id)] List with types and ids of objects. event_id: id of event that lead to revisions creation. user_id: id of user for which revisions should be created. action: action that will be displayed in revisions """ issue_objs = all_models.IssuetrackerIssue.query.filter( sa.tuple_(all_models.IssuetrackerIssue.object_type, all_models.IssuetrackerIssue.object_id).in_(resources)) obj_content_pairs = [(obj, obj.log_json()) for obj in issue_objs] revision_data = [{ "resource_id": obj.id, "resource_type": obj.type, "event_id": event_id, "action": action, "content": content, "resource_slug": None, "source_type": None, "source_id": None, "destination_type": None, "destination_id": None, "updated_at": datetime.datetime.utcnow(), "modified_by_id": user_id, "created_at": datetime.datetime.utcnow(), "context_id": obj.context_id, "is_empty": revisions_diff.changes_present(obj, content), } for obj, content in obj_content_pairs] inserter = all_models.Revision.__table__.insert() db.session.execute(inserter.values(revision_data))
def _revision_generator(user_id, action, objects): """Generate and return revisions bodies for objects.""" from ggrc.utils import revisions for obj in objects: content = obj.log_json() if "access_control_list" in content and content["access_control_list"]: for acl in content["access_control_list"]: acl["person"] = { "id": acl["person_id"], "type": "Person", "href": "/api/people/{}".format(acl["person_id"]), } rev = revisions.build_revision_body( obj.id, obj.__class__.__name__, content, None, action, user_id ) if isinstance(obj, Synchronizable): rev["created_at"] = obj.updated_at rev["updated_at"] = obj.updated_at else: rev["created_at"] = datetime.utcnow().replace(microsecond=0).isoformat() rev["updated_at"] = datetime.utcnow().replace(microsecond=0).isoformat() for attr in ["source_type", "source_id", "destination_type", "destination_id"]: rev[attr] = getattr(obj, attr, None) if action in ("created", "deleted"): rev["is_empty"] = False else: rev["is_empty"] = bool( obj and not revisions_diff.changes_present(obj, rev["content"]) ) yield rev
def create_revisions(resources, event_id, user_id, action): """Create revisions for provided objects in bulk. Args: resources: [(obj_type, obj_id)] List with types and ids of objects. event_id: id of event that lead to revisions creation. user_id: id of user for which revisions should be created. action: action that will be displayed in revisions """ issue_objs = all_models.IssuetrackerIssue.query.filter( sa.tuple_( all_models.IssuetrackerIssue.object_type, all_models.IssuetrackerIssue.object_id ).in_(resources) ) obj_content_pairs = [(obj, obj.log_json()) for obj in issue_objs] revision_data = [ { "resource_id": obj.id, "resource_type": obj.type, "event_id": event_id, "action": action, "content": content, "resource_slug": None, "source_type": None, "source_id": None, "destination_type": None, "destination_id": None, "updated_at": datetime.datetime.utcnow(), "modified_by_id": user_id, "created_at": datetime.datetime.utcnow(), "context_id": obj.context_id, "is_empty": revisions_diff.changes_present(obj, content), } for obj, content in obj_content_pairs ] inserter = all_models.Revision.__table__.insert() db.session.execute(inserter.values(revision_data))