mirror of
https://github.com/pretix/pretix.git
synced 2026-05-08 15:44:02 +00:00
Fix #2016 -- Correct shredding of question answers with file attachments
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import os
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
|
|
||||||
@@ -324,10 +325,16 @@ class QuestionAnswerShredder(BaseDataShredder):
|
|||||||
description = _('This will remove all answers to questions, as well as logged changes to them.')
|
description = _('This will remove all answers to questions, as well as logged changes to them.')
|
||||||
|
|
||||||
def generate_files(self) -> List[Tuple[str, str, str]]:
|
def generate_files(self) -> List[Tuple[str, str, str]]:
|
||||||
yield 'question-answers.json', 'application/json', json.dumps({
|
d = {}
|
||||||
'{}-{}'.format(op.order.code, op.positionid): AnswerSerializer(op.answers.all(), many=True).data
|
for op in OrderPosition.all.filter(order__event=self.event).prefetch_related('answers', 'answers__question'):
|
||||||
for op in OrderPosition.all.filter(order__event=self.event).prefetch_related('answers')
|
for a in op.answers.all():
|
||||||
}, indent=4)
|
if a.file:
|
||||||
|
fname = f'{op.order.code}-{op.positionid}-{a.question.identifier}-{os.path.basename(a.file.name)}'
|
||||||
|
yield fname, 'application/unknown', a.file.read()
|
||||||
|
d[f'{op.order.code}-{op.positionid}'] = AnswerSerializer(
|
||||||
|
sorted(op.answers.all(), key=lambda a: a.question_id), context={'request': None}, many=True
|
||||||
|
).data
|
||||||
|
yield 'question-answers.json', 'application/json', json.dumps(d, indent=4)
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def shred_data(self):
|
def shred_data(self):
|
||||||
|
|||||||
@@ -226,6 +226,7 @@ def test_invoice_address_shredder(event, order):
|
|||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_question_answer_shredder(event, order, question):
|
def test_question_answer_shredder(event, order, question):
|
||||||
opt = question.options.first()
|
opt = question.options.first()
|
||||||
|
q2 = event.questions.create(question="Photo", type="F", identifier="DEF")
|
||||||
l1 = order.log_action(
|
l1 = order.log_action(
|
||||||
'pretix.event.order.modified',
|
'pretix.event.order.modified',
|
||||||
data={
|
data={
|
||||||
@@ -242,21 +243,33 @@ def test_question_answer_shredder(event, order, question):
|
|||||||
question=question,
|
question=question,
|
||||||
answer='S'
|
answer='S'
|
||||||
)
|
)
|
||||||
|
qa2 = QuestionAnswer.objects.create(
|
||||||
|
orderposition=order.positions.first(),
|
||||||
|
question=q2,
|
||||||
|
answer='file:///foo.pdf'
|
||||||
|
)
|
||||||
qa.file.save('foo.pdf', ContentFile('foo'))
|
qa.file.save('foo.pdf', ContentFile('foo'))
|
||||||
fname = qa.file.path
|
fname = qa.file.path
|
||||||
assert os.path.exists(fname)
|
assert os.path.exists(fname)
|
||||||
qa.options.add(opt)
|
qa.options.add(opt)
|
||||||
s = QuestionAnswerShredder(event)
|
s = QuestionAnswerShredder(event)
|
||||||
f = list(s.generate_files())
|
f = list(s.generate_files())
|
||||||
assert json.loads(f[0][2]) == {
|
assert json.loads(f[-1][2]) == {
|
||||||
'{}-1'.format(order.code): [{
|
'{}-1'.format(order.code): [{
|
||||||
'question': question.pk,
|
'question': question.pk,
|
||||||
'answer': 'S',
|
'answer': 'S',
|
||||||
'question_identifier': question.identifier,
|
'question_identifier': question.identifier,
|
||||||
'options': [opt.pk],
|
'options': [opt.pk],
|
||||||
'option_identifiers': [opt.identifier],
|
'option_identifiers': [opt.identifier],
|
||||||
|
}, {
|
||||||
|
'question': q2.pk,
|
||||||
|
'answer': f'/api/v1/organizers/dummy/events/dummy/orderpositions/{qa2.orderposition_id}/answer/{qa2.question_id}/',
|
||||||
|
'question_identifier': q2.identifier,
|
||||||
|
'options': [],
|
||||||
|
'option_identifiers': [],
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
assert f[0][0].endswith('.pdf')
|
||||||
s.shred_data()
|
s.shred_data()
|
||||||
order.refresh_from_db()
|
order.refresh_from_db()
|
||||||
assert not os.path.exists(fname)
|
assert not os.path.exists(fname)
|
||||||
|
|||||||
Reference in New Issue
Block a user