Skip to content

Commit

Permalink
handling revision separately and only if attributes are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rathorevaibhav committed Jan 16, 2024
1 parent e8b6877 commit 4edcf13
Showing 1 changed file with 49 additions and 28 deletions.
77 changes: 49 additions & 28 deletions admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,34 +848,6 @@ def resource_edit(resource_type, resource_id):
editable_relations=editable_relations,
)

if (
hasattr(resource_class, "revisions")
and hasattr(resource_class, "revision_model")
and resource_class.revisions
):
revision_model = resource_class.revision_model
revision_pk = resource_class.revision_pk

cloned_attributes_to_save = {}
for column, value in resource.__dict__.items():
if column in [
"_sa_instance_state",
"created_at",
"updated_at",
"promised_token",
"token_amount",
]:
continue

if column == "id":
cloned_attributes_to_save[revision_pk] = value
continue

cloned_attributes_to_save[column] = value

cloned_resource = revision_model(**cloned_attributes_to_save)
db.session.add(cloned_resource)
db.session.commit()

for attribute in editable_attributes:
if attribute["name"] in request.form:
Expand Down Expand Up @@ -923,6 +895,8 @@ def resource_edit(resource_type, resource_id):
db.session.add(resource)
db.session.commit()

handle_resource_revision(resource_class=resource_class, old_resource=old_resource, new_resource=resource)

# call after update hook
if hasattr(resource_class, "after_update_callback"):
resource_class.after_update_callback(resource, old_resource)
Expand Down Expand Up @@ -1231,3 +1205,50 @@ def update_receipt_status():
response = update_approval_status(current_user)

return response

def handle_resource_revision(resource_class, old_resource, new_resource):
if (
hasattr(resource_class, "revisions")
and hasattr(resource_class, "revision_model")
and resource_class.revisions
):
revision_model = resource_class.revision_model
revision_pk = resource_class.revision_pk

# check if resource has been edited then only create the revision
is_modified = False
for column, value in old_resource.__dict__.items():
if column in [
"id",
"_sa_instance_state",
"created_at",
"updated_at",
]:
continue

if getattr(old_resource, column) != getattr(new_resource, column):
is_modified = True
break

if not is_modified:
return


cloned_attributes_to_save = {}
for column, value in old_resource.__dict__.items():
if column in [
"_sa_instance_state",
"created_at",
"updated_at",
]:
continue

if column == "id":
cloned_attributes_to_save[revision_pk] = value
continue

cloned_attributes_to_save[column] = value

cloned_resource = revision_model(**cloned_attributes_to_save)
db.session.add(cloned_resource)
db.session.commit()

0 comments on commit 4edcf13

Please sign in to comment.