Skip to content

Commit

Permalink
Use constants for creation/deletion history messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pylipp committed Feb 4, 2025
1 parent de4011b commit ccc75a8
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 25 deletions.
3 changes: 2 additions & 1 deletion back/boxtribute_server/business_logic/warehouse/box/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from ....models.definitions.unit import Unit
from ....models.utils import (
BATCH_SIZE,
HISTORY_DELETION_MESSAGE,
RANDOM_SEQUENCE_GENERATION_ATTEMPTS,
convert_ids,
save_creation_to_history,
Expand Down Expand Up @@ -358,7 +359,7 @@ def delete_boxes(*, user_id, boxes):
now = utcnow()
history_entries = [
DbChangeHistory(
changes="Record deleted",
changes=HISTORY_DELETION_MESSAGE,
table_name=Box._meta.table_name,
record_id=box.id,
user=user_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ....models.definitions.standard_product import StandardProduct
from ....models.utils import (
BATCH_SIZE,
HISTORY_CREATION_MESSAGE,
handle_non_existing_resource,
safely_handle_deletion,
save_creation_to_history,
Expand Down Expand Up @@ -249,7 +250,7 @@ def enable_standard_products(
)
history_entries = [
DbChangeHistory(
changes="Record created",
changes=HISTORY_CREATION_MESSAGE,
table_name=Product._meta.table_name,
record_id=product.id,
user=user_id,
Expand Down
9 changes: 6 additions & 3 deletions back/boxtribute_server/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# Number of attempts when trying to a generate unique random sequence
RANDOM_SEQUENCE_GENERATION_ATTEMPTS = 10

HISTORY_CREATION_MESSAGE = "Record created"
HISTORY_DELETION_MESSAGE = "Record deleted"


def utcnow():
"""Return current datetime in UTC, in second precision (the MySQL database is
Expand Down Expand Up @@ -76,14 +79,14 @@ def save_creation_to_history(f):
The function runs the decorated function, effectively executing the creation. An
entry in the history table is created.
"""
return _save_to_history(f, "Record created")
return _save_to_history(f, HISTORY_CREATION_MESSAGE)


def safely_handle_deletion(f):
"""Using this decorator will set the `deleted_on` timestamp of the to-be-deleted
model instance and save the changes (i.e. `save()` does not have to be called).
"""
return _save_to_history(f, "Record deleted")
return _save_to_history(f, HISTORY_DELETION_MESSAGE)


def _save_to_history(f, changes):
Expand All @@ -100,7 +103,7 @@ def inner(*args, **kwargs):
if not isinstance(result, db.Model):
return result

if "deleted" in changes:
if changes == HISTORY_DELETION_MESSAGE:
result.deleted_on = now
result.save()

Expand Down
3 changes: 2 additions & 1 deletion back/test/data/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from boxtribute_server.enums import BoxState
from boxtribute_server.models.definitions.history import DbChangeHistory
from boxtribute_server.models.utils import HISTORY_CREATION_MESSAGE

from .box import another_marked_for_shipment_box_data
from .box import data as box_data
Expand All @@ -17,7 +18,7 @@ def data():
[
{
"id": i,
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": box["id"],
"change_date": box["created_on"],
"table_name": "stock",
Expand Down
12 changes: 8 additions & 4 deletions back/test/endpoint_tests/test_beneficiary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import pytest
from boxtribute_server.enums import HumanGender
from boxtribute_server.models.definitions.history import DbChangeHistory
from boxtribute_server.models.utils import compute_age
from boxtribute_server.models.utils import (
HISTORY_CREATION_MESSAGE,
HISTORY_DELETION_MESSAGE,
compute_age,
)
from utils import assert_successful_request


Expand Down Expand Up @@ -282,7 +286,7 @@ def test_beneficiary_mutations(
assert history_entries[i].pop("change_date").isoformat().startswith(today)
assert history_entries == [
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(beneficiary_id),
"from_int": None,
"to_int": None,
Expand Down Expand Up @@ -355,13 +359,13 @@ def test_beneficiary_mutations(
"to_int": int(beneficiary_id),
},
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"record_id": int(deactivated_child_id),
"from_int": None,
"to_int": None,
},
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"record_id": int(deactivated_beneficiary_id),
"from_int": None,
"to_int": None,
Expand Down
16 changes: 10 additions & 6 deletions back/test/endpoint_tests/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from auth import mock_user_for_request
from boxtribute_server.enums import BoxState, TagType
from boxtribute_server.models.definitions.history import DbChangeHistory
from boxtribute_server.models.utils import RANDOM_SEQUENCE_GENERATION_ATTEMPTS
from boxtribute_server.models.utils import (
HISTORY_CREATION_MESSAGE,
HISTORY_DELETION_MESSAGE,
RANDOM_SEQUENCE_GENERATION_ATTEMPTS,
)
from utils import (
assert_bad_user_input,
assert_forbidden_request,
Expand Down Expand Up @@ -907,7 +911,7 @@ def test_box_mutations(
box_id = int(updated_box["id"])
assert history[22:] == [
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"from_int": None,
"to_int": None,
"record_id": box_id,
Expand All @@ -918,7 +922,7 @@ def test_box_mutations(
"to_float": None,
},
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"from_int": None,
"to_int": None,
"record_id": box_id + 1,
Expand All @@ -929,7 +933,7 @@ def test_box_mutations(
"to_float": None,
},
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"from_int": None,
"to_int": None,
"record_id": box_id + 2,
Expand Down Expand Up @@ -1165,7 +1169,7 @@ def test_box_mutations(
"to_float": None,
},
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"from_int": None,
"to_int": None,
"record_id": box_id,
Expand All @@ -1176,7 +1180,7 @@ def test_box_mutations(
"to_float": None,
},
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"from_int": None,
"to_int": None,
"record_id": box_id + 1,
Expand Down
18 changes: 11 additions & 7 deletions back/test/endpoint_tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import pytest
from boxtribute_server.enums import BoxState, ProductGender, ProductType
from boxtribute_server.models.definitions.history import DbChangeHistory
from boxtribute_server.models.utils import (
HISTORY_CREATION_MESSAGE,
HISTORY_DELETION_MESSAGE,
)
from utils import assert_successful_request

today = date.today().isoformat()
Expand Down Expand Up @@ -401,7 +405,7 @@ def _create_update_mutation(field, value):
assert history_entries[i].pop("change_date").isoformat().startswith(today)
assert history_entries == [
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(product_id),
"from_int": None,
"to_int": None,
Expand Down Expand Up @@ -449,13 +453,13 @@ def _create_update_mutation(field, value):
"to_int": 1,
},
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"record_id": int(product_id),
"from_int": None,
"to_int": None,
},
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(another_product_id),
"from_int": None,
"to_int": None,
Expand Down Expand Up @@ -699,19 +703,19 @@ def _create_update_mutation(field, value):
assert history_entries[i].pop("change_date").isoformat().startswith(today)
assert history_entries == [
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(product_id),
"from_int": None,
"to_int": None,
},
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"record_id": int(product_id),
"from_int": None,
"to_int": None,
},
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(another_product_id),
"from_int": None,
"to_int": None,
Expand Down Expand Up @@ -831,7 +835,7 @@ def test_standard_product_bulk_mutations(
assert entry.pop("change_date").isoformat().startswith(today)
assert history_entries == [
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(product_id),
"from_int": None,
"to_int": None,
Expand Down
8 changes: 6 additions & 2 deletions back/test/endpoint_tests/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import pytest
from boxtribute_server.enums import TagType
from boxtribute_server.models.definitions.history import DbChangeHistory
from boxtribute_server.models.utils import (
HISTORY_CREATION_MESSAGE,
HISTORY_DELETION_MESSAGE,
)
from utils import (
assert_bad_user_input,
assert_forbidden_request,
Expand Down Expand Up @@ -260,13 +264,13 @@ def test_tags_mutations(client, tags, base1_active_tags, another_beneficiary, lo
assert history_entries[i].pop("change_date").isoformat().startswith(today)
assert history_entries == [
{
"changes": "Record deleted",
"changes": HISTORY_DELETION_MESSAGE,
"record_id": int(deleted_tag_id),
"from_int": None,
"to_int": None,
},
{
"changes": "Record created",
"changes": HISTORY_CREATION_MESSAGE,
"record_id": int(tag_id),
"from_int": None,
"to_int": None,
Expand Down

0 comments on commit ccc75a8

Please sign in to comment.