Skip to content

Commit

Permalink
Improve logentry shredder function
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Dec 21, 2023
1 parent 12804ff commit 8c86500
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/pretix/base/shredder.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,33 @@ def _shred(d, banlist, whitelist):
for k, v in d.items():
if k not in whitelist:
if isinstance(d[k], list):
d[k] = ['█'] * len(d[k])
if isinstance(d[k], dict):
_shred(d[k], None, ['__'])
newlist = []
for i in d[k]:
if isinstance(i, dict):
_shred(i, None, [None])
else:
i = '█'
newlist.append(i)
d[k] = newlist
elif isinstance(d[k], dict):
_shred(d[k], None, [None])
elif d[k]:
d[k] = '█'
shredded = True
elif banlist:
for k in banlist:
if k in d:
if isinstance(d[k], list):
d[k] = ['█'] * len(d[k])
if isinstance(d[k], dict):
_shred(d, None, ['__'])
newlist = []
for i in d[k]:
if isinstance(i, dict):
_shred(i, None, [None])
else:
i = '█'
newlist.append(i)
d[k] = newlist
elif isinstance(d[k], dict):
_shred(d[k], None, [None])
elif d[k]:
d[k] = '█'
shredded = True
Expand Down
81 changes: 81 additions & 0 deletions src/tests/base/test_shredders.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
AttendeeInfoShredder, CachedTicketShredder, EmailAddressShredder,
InvoiceAddressShredder, InvoiceShredder, PaymentInfoShredder,
QuestionAnswerShredder, WaitingListShredder, shred_constraints,
shred_log_fields,
)


Expand Down Expand Up @@ -412,3 +413,83 @@ def test_shred_constraint_after_event_subevents(event):
date_to=now_dt + timedelta(hours=1)
)
assert shred_constraints(event)


@pytest.mark.django_db
def test_shred_log_fields_banlist(event):
le = event.log_action("foo.bar", data={
"dict": {
"subdict": {
"foo": "bar",
"empty": None,
}
},
"list": [
{
"foo": "bar",
},
"baz",
],
"string": "foo",
"bool": True,
"int": 0,
})
shred_log_fields(le, banlist=["dict", "list", "int", "bool"])
assert le.shredded
assert le.parsed_data == {
"dict": {
"subdict": {
"foo": "█",
"empty": None,
}
},
"list": [
{
"foo": "█",
},
"█",
],
"string": "foo",
"bool": "█",
"int": 0,
}


@pytest.mark.django_db
def test_shred_log_fields_whitelist(event):
le = event.log_action("foo.bar", data={
"dict": {
"subdict": {
"foo": "bar",
"empty": None,
}
},
"list": [
{
"foo": "bar",
},
"baz",
],
"string": "foo",
"bool": True,
"int": 0,
})
shred_log_fields(le, whitelist=["string"])
assert le.shredded
assert le.parsed_data == {
"dict": {
"subdict": {
"foo": "█",
"empty": None,
}
},
"list": [
{
"foo": "█",
},
"█",
],
"string": "foo",
"bool": "█",
"int": 0,
}

0 comments on commit 8c86500

Please sign in to comment.