Reusable media (#3131)

Co-authored-by: Martin Gross <gross@rami.io>
This commit is contained in:
Raphael Michel
2023-04-03 10:45:22 +02:00
committed by GitHub
parent 377117548d
commit d0b449ea89
67 changed files with 2876 additions and 133 deletions

View File

@@ -116,6 +116,7 @@ def team(organizer):
can_view_vouchers=True,
can_change_orders=True,
can_manage_customers=True,
can_manage_reusable_media=True,
can_change_organizer_settings=True
)

View File

@@ -33,7 +33,9 @@ from pytz import UTC
from tests.const import SAMPLE_PNG
from pretix.api.serializers.item import QuestionSerializer
from pretix.base.models import Checkin, InvoiceAddress, Order, OrderPosition
from pretix.base.models import (
Checkin, InvoiceAddress, Order, OrderPosition, ReusableMedium,
)
# Lots of this code is overlapping with test_checkin.py, and some of it is arguably redundant since it's triggering
# the same backend code paths (for now). However, this is SUCH a critical part of pretix that we don't want to take
@@ -275,6 +277,72 @@ def test_by_secret_special_chars(token_client, organizer, clist, event, order):
assert resp.data['status'] == 'ok'
@pytest.mark.django_db
def test_by_medium(token_client, organizer, clist, event, order):
with scopes_disabled():
ReusableMedium.objects.create(
type="barcode",
identifier="abcdef",
organizer=organizer,
linked_orderposition=order.positions.first(),
)
resp = _redeem(token_client, organizer, clist, "abcdef", {"source_type": "barcode"})
assert resp.status_code == 201
assert resp.data['status'] == 'ok'
with scopes_disabled():
ci = clist.checkins.get(position=order.positions.first())
assert ci.raw_barcode == "abcdef"
assert ci.raw_source_type == "barcode"
@pytest.mark.django_db
def test_by_medium_not_connected(token_client, organizer, clist, event, order):
with scopes_disabled():
ReusableMedium.objects.create(
type="barcode",
identifier="abcdef",
organizer=organizer,
)
resp = _redeem(token_client, organizer, clist, "abcdef", {"source_type": "barcode"})
assert resp.status_code == 404
assert resp.data['status'] == 'error'
assert resp.data['reason'] == 'invalid'
@pytest.mark.django_db
def test_by_medium_wrong_type(token_client, organizer, clist, event, order):
with scopes_disabled():
ReusableMedium.objects.create(
type="nfc_uid",
identifier="abcdef",
organizer=organizer,
linked_orderposition=order.positions.first(),
)
resp = _redeem(token_client, organizer, clist, "abcdef", {"source_type": "barcode"})
assert resp.status_code == 404
assert resp.data['status'] == 'error'
assert resp.data['reason'] == 'invalid'
resp = _redeem(token_client, organizer, clist, "abcdef", {"source_type": "nfc_uid"})
assert resp.status_code == 201
assert resp.data['status'] == 'ok'
@pytest.mark.django_db
def test_by_medium_inactive(token_client, organizer, clist, event, order):
with scopes_disabled():
ReusableMedium.objects.create(
type="barcode",
identifier="abcdef",
organizer=organizer,
active=False,
linked_orderposition=order.positions.first(),
)
resp = _redeem(token_client, organizer, clist, "abcdef", {"source_type": "barcode"})
assert resp.status_code == 404
assert resp.data['status'] == 'error'
assert resp.data['reason'] == 'invalid'
@pytest.mark.django_db
def test_only_once(token_client, organizer, clist, event, order):
with scopes_disabled():

View File

@@ -1212,7 +1212,8 @@ def test_get_event_settings(token_client, organizer, event):
"value": "https://example.org",
"label": "Imprint URL",
"help_text": "This should point e.g. to a part of your website that has your contact details and legal "
"information."
"information.",
"readonly": False,
}
@@ -1229,14 +1230,17 @@ def test_patch_event_settings(token_client, organizer, event):
{
'de': 'Ich bin mit den AGB einverstanden.'
}
]
],
'reusable_media_active': True, # readonly, ignored
},
format='json'
)
assert resp.status_code == 200
assert resp.data['imprint_url'] == "https://example.com"
assert not resp.data['reusable_media_active']
event.settings.flush()
assert event.settings.imprint_url == 'https://example.com'
assert not event.settings.reusable_media_active
mocked.assert_not_called()
resp = token_client.patch(

View File

@@ -296,6 +296,8 @@ TEST_ITEM_RES = {
"grant_membership_duration_like_event": True,
"grant_membership_duration_days": 0,
"grant_membership_duration_months": 0,
"media_policy": None,
"media_type": None,
"validity_mode": None,
"validity_fixed_from": None,
"validity_fixed_until": None,

View File

@@ -35,7 +35,8 @@ from pytz import UTC
from tests.const import SAMPLE_PNG
from pretix.base.models import (
InvoiceAddress, Order, OrderPosition, Question, SeatingPlan,
InvoiceAddress, Item, Order, OrderPosition, Organizer, Question,
SeatingPlan,
)
from pretix.base.models.orders import CartPosition, OrderFee, QuestionAnswer
@@ -55,6 +56,27 @@ def taxrule(event):
return event.tax_rules.create(rate=Decimal('19.00'))
@pytest.fixture
def medium(organizer):
return organizer.reusable_media.create(
type="barcode",
identifier="ABCDE"
)
@pytest.fixture
def organizer2():
return Organizer.objects.create(name='Partner', slug='partner')
@pytest.fixture
def medium2(organizer2):
return organizer2.reusable_media.create(
type="barcode",
identifier="ABCDE"
)
@pytest.fixture
def question(event, item):
q = event.questions.create(question="T-Shirt size", type="S", identifier="ABC")
@@ -2810,3 +2832,72 @@ def test_create_cart_and_consume_cart_with_addons(token_client, organizer, event
), format='json', data=res
)
assert resp.status_code == 201
@pytest.mark.django_db
def test_order_create_use_medium(token_client, organizer, event, item, quota, question, medium):
item.media_type = medium.type
item.media_policy = Item.MEDIA_POLICY_REUSE_OR_NEW
item.save()
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['use_reusable_medium'] = medium.pk
res['positions'][0]['answers'][0]['question'] = question.pk
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/?pdf_data=true'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 201
with scopes_disabled():
o = Order.objects.get(code=resp.data['code'])
medium.refresh_from_db()
assert o.positions.first() == medium.linked_orderposition
assert resp.data['positions'][0]['pdf_data']['medium_identifier'] == medium.identifier
@pytest.mark.django_db
def test_order_create_use_medium_other_organizer(token_client, organizer, event, item, quota, question, medium2):
item.media_type = medium2.type
item.media_policy = Item.MEDIA_POLICY_REUSE_OR_NEW
item.save()
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['use_reusable_medium'] = medium2.pk
res['positions'][0]['answers'][0]['question'] = question.pk
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/?pdf_data=true'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.data == {
"positions": [
{
"use_reusable_medium": ["The specified medium does not belong to this organizer."]
}
]
}
assert resp.status_code == 400
@pytest.mark.django_db
def test_order_create_create_medium(token_client, organizer, event, item, quota, question):
item.media_type = 'barcode'
item.media_policy = Item.MEDIA_POLICY_REUSE_OR_NEW
item.save()
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res['positions'][0]['item'] = item.pk
res['positions'][0]['answers'][0]['question'] = question.pk
resp = token_client.post(
'/api/v1/organizers/{}/events/{}/orders/?pdf_data=true'.format(
organizer.slug, event.slug
), format='json', data=res
)
assert resp.status_code == 201
with scopes_disabled():
o = Order.objects.get(code=resp.data['code'])
i = resp.data['positions'][0]['pdf_data']['medium_identifier']
assert i
m = organizer.reusable_media.get(identifier=i)
assert m.linked_orderposition == o.positions.first()
assert m.type == "barcode"

View File

@@ -1802,7 +1802,7 @@ def test_pdf_data(token_client, organizer, event, order, django_assert_max_num_q
assert not resp.data['positions'][0].get('pdf_data')
# order list
with django_assert_max_num_queries(29):
with django_assert_max_num_queries(30):
resp = token_client.get('/api/v1/organizers/{}/events/{}/orders/?pdf_data=true'.format(
organizer.slug, event.slug
))
@@ -1815,7 +1815,7 @@ def test_pdf_data(token_client, organizer, event, order, django_assert_max_num_q
assert not resp.data['results'][0]['positions'][0].get('pdf_data')
# position list
with django_assert_max_num_queries(32):
with django_assert_max_num_queries(33):
resp = token_client.get('/api/v1/organizers/{}/events/{}/orderpositions/?pdf_data=true'.format(
organizer.slug, event.slug
))

View File

@@ -62,7 +62,8 @@ def test_get_settings(token_client, organizer):
assert resp.data['event_list_type'] == {
"value": "week",
"label": "Default overview style",
"help_text": "If your event series has more than 50 dates in the future, only the month or week calendar can be used."
"help_text": "If your event series has more than 50 dates in the future, only the month or week calendar can be used.",
"readonly": False,
}

View File

@@ -0,0 +1,352 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io GmbH and contributors
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation in version 3 of the License.
#
# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are
# applicable granting you additional permissions and placing additional restrictions on your usage of this software.
# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive
# this file, see <https://pretix.eu/about/en/license>.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
from datetime import timedelta
from decimal import Decimal
import pytest
from django.utils.timezone import now
from django_scopes import scopes_disabled
from pretix.base.models import Order, Organizer, ReusableMedium
@pytest.fixture
def giftcard(organizer):
gc = organizer.issued_gift_cards.create(secret="ABCDEF", currency="EUR")
gc.transactions.create(value=Decimal('23.00'))
return gc
@pytest.fixture
def medium(organizer):
m = organizer.reusable_media.create(identifier="ABCDEFGH", type="barcode", active=True)
return m
@pytest.fixture
def organizer2():
return Organizer.objects.create(name='Partner', slug='partner')
@pytest.fixture
def giftcard2(organizer2):
gc = organizer2.issued_gift_cards.create(secret="ABCDEF", currency="EUR")
gc.transactions.create(value=Decimal('23.00'))
return gc
@pytest.fixture
def customer(organizer, event):
return organizer.customers.create(
identifier="8WSAJCJ",
email="foo@example.org",
name_parts={"_legacy": "Foo"},
name_cached="Foo",
is_verified=False,
)
TEST_MEDIUM_RES = {
"id": 1,
"identifier": "ABCDEFGH",
"type": "barcode",
"active": True,
"expires": None,
"customer": None,
"linked_orderposition": None,
"linked_giftcard": None,
"notes": None,
"info": {},
}
@pytest.mark.django_db
def test_medium_list(token_client, organizer, event, medium):
res = dict(TEST_MEDIUM_RES)
res["id"] = medium.pk
res["created"] = medium.created.isoformat().replace('+00:00', 'Z')
res["updated"] = medium.updated.isoformat().replace('+00:00', 'Z')
resp = token_client.get('/api/v1/organizers/{}/reusablemedia/'.format(organizer.slug))
assert resp.status_code == 200
assert [res] == resp.data['results']
resp = token_client.get('/api/v1/organizers/{}/reusablemedia/?identifier=XYZABC'.format(organizer.slug))
assert resp.status_code == 200
assert [] == resp.data['results']
resp = token_client.get('/api/v1/organizers/{}/reusablemedia/?identifier=ABCDEFGH'.format(organizer.slug))
assert resp.status_code == 200
assert [res] == resp.data['results']
@pytest.mark.django_db
def test_medium_detail(token_client, organizer, event, medium, giftcard, customer):
res = dict(TEST_MEDIUM_RES)
res["id"] = medium.pk
res["created"] = medium.created.isoformat().replace('+00:00', 'Z')
res["updated"] = medium.updated.isoformat().replace('+00:00', 'Z')
resp = token_client.get('/api/v1/organizers/{}/reusablemedia/{}/'.format(organizer.slug, medium.pk))
assert resp.status_code == 200
assert res == resp.data
with scopes_disabled():
o = Order.objects.create(
code='FOO', event=event, email='dummy@dummy.test',
status=Order.STATUS_PENDING, datetime=now(), expires=now() + timedelta(days=10),
total=14, locale='en'
)
ticket = event.items.create(name='Early-bird ticket', category=None, default_price=23, admission=True,
personalized=True)
op = o.positions.create(item=ticket, price=Decimal("14"))
medium.linked_orderposition = op
medium.linked_giftcard = giftcard
medium.customer = customer
medium.save()
resp = token_client.get(
'/api/v1/organizers/{}/reusablemedia/{}/?expand=linked_giftcard&expand=linked_orderposition&expand=customer'.format(
organizer.slug, medium.pk))
assert resp.status_code == 200
assert resp.data["customer"] == {
"identifier": customer.identifier,
"external_identifier": None,
"email": "foo@example.org",
"name": "Foo",
"name_parts": {"_legacy": "Foo"},
"is_active": True,
"is_verified": False,
"last_login": None,
"date_joined": customer.date_joined.isoformat().replace("+00:00", "Z"),
"locale": "en",
"last_modified": customer.last_modified.isoformat().replace("+00:00", "Z"),
"notes": None
}
assert resp.data["linked_orderposition"] == {
"id": op.pk,
"order": {"code": "FOO", "event": "dummy"},
"positionid": op.positionid,
"item": ticket.pk,
"variation": None,
"price": "14.00",
"attendee_name": None,
"attendee_name_parts": {},
"company": None,
"street": None,
"zipcode": None,
"city": None,
"country": None,
"state": None,
"discount": None,
"attendee_email": None,
"voucher": None,
"tax_rate": "0.00",
"tax_value": "0.00",
"secret": op.secret,
"addon_to": None,
"subevent": None,
"checkins": [],
"downloads": [],
"answers": [],
"tax_rule": None,
"pseudonymization_id": op.pseudonymization_id,
"pdf_data": {},
"seat": None,
"canceled": False,
"valid_from": None,
"valid_until": None,
"blocked": None
}
assert resp.data["linked_giftcard"] == {
"id": giftcard.pk,
"secret": "ABCDEF",
"issuance": giftcard.issuance.isoformat().replace("+00:00", "Z"),
"value": "23.00",
"currency": "EUR",
"testmode": False,
"expires": None,
"conditions": None
}
TEST_MEDIUM_CREATE_PAYLOAD = {
"type": "barcode",
"identifier": "FOOBAR",
"active": True,
"info": {"foo": "bar"}
}
@pytest.mark.django_db
def test_medium_create(token_client, organizer, giftcard):
payload = dict(TEST_MEDIUM_CREATE_PAYLOAD)
payload['linked_giftcard'] = giftcard.pk
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/'.format(organizer.slug),
payload,
format='json'
)
assert resp.status_code == 201
with scopes_disabled():
m = ReusableMedium.objects.get(pk=resp.data['id'])
assert m.organizer == organizer
assert m.type == "barcode"
assert m.identifier == "FOOBAR"
assert m.active
assert m.linked_giftcard == giftcard
assert m.info == {"foo": "bar"}
assert m.created > now() - timedelta(minutes=10)
assert m.updated > now() - timedelta(minutes=10)
@pytest.mark.django_db
def test_medium_foreignkeyval(token_client, organizer, giftcard2):
payload = dict(TEST_MEDIUM_CREATE_PAYLOAD)
payload['linked_giftcard'] = giftcard2.pk
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/'.format(organizer.slug),
payload,
format='json'
)
assert resp.status_code == 400
assert resp.data == {'linked_giftcard': [f'Invalid pk "{giftcard2.pk}" - object does not exist.']}
@pytest.mark.django_db
def test_medium_create_duplicate(token_client, organizer, event, medium):
payload = dict(TEST_MEDIUM_CREATE_PAYLOAD)
payload['identifier'] = medium.identifier
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/'.format(organizer.slug),
payload,
format='json'
)
assert resp.status_code == 400
assert resp.data == {
'identifier': ['A medium with the same identifier and type already exists in your organizer account.']}
@pytest.mark.django_db
def test_medium_patch(token_client, organizer, event, medium, giftcard, customer):
resp = token_client.patch(
'/api/v1/organizers/{}/reusablemedia/{}/'.format(organizer.slug, medium.pk),
{
'linked_giftcard': giftcard.pk,
'customer': customer.identifier,
'info': {'test': 2},
'identifier': 'WILLBEIGNORED',
},
format='json'
)
assert resp.status_code == 200
medium.refresh_from_db()
assert medium.linked_giftcard == giftcard
assert medium.customer == customer
assert medium.info == {'test': 2}
assert medium.identifier == "ABCDEFGH"
@pytest.mark.django_db
def test_medium_no_deletion(token_client, organizer, event, medium):
resp = token_client.delete(
'/api/v1/organizers/{}/reusablemedia/{}/'.format(organizer.slug, medium.pk),
)
assert resp.status_code == 405
@pytest.mark.django_db
def test_medium_lookup_ok(token_client, organizer, event, medium):
res = dict(TEST_MEDIUM_RES)
res["id"] = medium.pk
res["created"] = medium.created.isoformat().replace('+00:00', 'Z')
res["updated"] = medium.updated.isoformat().replace('+00:00', 'Z')
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/lookup/'.format(organizer.slug),
{
"type": medium.type,
"identifier": medium.identifier,
},
format='json'
)
assert resp.status_code == 200
assert res == resp.data["result"]
@pytest.mark.django_db
def test_medium_lookup_not_found(token_client, organizer, organizer2, medium):
medium.organizer = organizer2
medium.save()
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/lookup/'.format(organizer.slug),
{
"type": medium.type,
"identifier": medium.identifier,
},
format='json'
)
assert resp.status_code == 200
assert resp.data["result"] is None
@pytest.mark.django_db
def test_medium_autocreate(token_client, organizer):
# Disabled
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/lookup/'.format(organizer.slug),
{
"type": "nfc_uid",
"identifier": "AABBCCDD",
},
format='json'
)
assert resp.status_code == 200
assert resp.data["result"] is None
# Enabled
organizer.settings.reusable_media_type_nfc_uid_autocreate_giftcard = True
organizer.settings.reusable_media_type_nfc_uid_autocreate_giftcard_currency = 'EUR'
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/lookup/?expand=linked_giftcard'.format(organizer.slug),
{
"type": "nfc_uid",
"identifier": "AABBCCDD",
},
format='json'
)
assert resp.status_code == 200
res = resp.data["result"]
with scopes_disabled():
m = ReusableMedium.objects.get(pk=res["id"])
assert res["identifier"] == "AABBCCDD" == m.identifier
assert res["type"] == "nfc_uid" == m.type
assert res["linked_giftcard"]["value"] == "0.00"
# Ignore NFC random UID
resp = token_client.post(
'/api/v1/organizers/{}/reusablemedia/lookup/?expand=linked_giftcard'.format(organizer.slug),
{
"type": "nfc_uid",
"identifier": "08080808",
},
format='json'
)
assert resp.status_code == 200
assert resp.data["result"] is None

View File

@@ -39,7 +39,7 @@ def second_team(organizer, event):
TEST_TEAM_RES = {
'id': 1, 'name': 'Test-Team', 'all_events': True, 'limit_events': [], 'can_create_events': True,
'can_change_teams': True, 'can_change_organizer_settings': True, 'can_manage_gift_cards': True,
'can_manage_customers': True,
'can_manage_customers': True, 'can_manage_reusable_media': True,
'can_change_event_settings': True, 'can_change_items': True, 'can_view_orders': True, 'can_change_orders': True,
'can_view_vouchers': True, 'can_change_vouchers': True, 'can_checkin_orders': False
}
@@ -47,7 +47,7 @@ TEST_TEAM_RES = {
SECOND_TEAM_RES = {
'id': 1, 'name': 'User team', 'all_events': False, 'limit_events': ['dummy'],
'can_create_events': False,
'can_manage_customers': False,
'can_manage_customers': False, 'can_manage_reusable_media': False,
'can_change_teams': False, 'can_change_organizer_settings': False, 'can_manage_gift_cards': False,
'can_change_event_settings': False, 'can_change_items': False, 'can_view_orders': False, 'can_change_orders': False,
'can_view_vouchers': False, 'can_change_vouchers': False, 'can_checkin_orders': False