Move auto check-in to plugin with more functionality (#4331)

* Move auto check-in to plugin with more functionality

* Rename field

* Add to MANIFEST.in
This commit is contained in:
Raphael Michel
2024-07-29 09:46:53 +02:00
committed by GitHub
parent c6a2ae3783
commit cab360bdb6
28 changed files with 2285 additions and 6 deletions

View File

@@ -0,0 +1,21 @@
#
# 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/>.
#

View File

@@ -0,0 +1,96 @@
#
# 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 datetime, timezone
import pytest
from django_scopes import scopes_disabled
from rest_framework.test import APIClient
from pretix.base.models import Event, Organizer, Team
@pytest.fixture
@scopes_disabled()
def organizer():
return Organizer.objects.create(name="Dummy", slug="dummy")
@pytest.fixture
@scopes_disabled()
def event(organizer):
e = Event.objects.create(
organizer=organizer,
name="Dummy",
slug="dummy",
date_from=datetime(2017, 12, 27, 10, 0, 0, tzinfo=timezone.utc),
plugins="pretix.plugins.banktransfer,pretix.plugins.ticketoutputpdf,pretix.plugins.autocheckin",
is_public=True,
)
e.settings.timezone = "Europe/Berlin"
return e
@pytest.fixture
@scopes_disabled()
def item(event):
return event.items.create(name="foo", default_price=3)
@pytest.fixture
@scopes_disabled()
def checkin_list(event):
return event.checkin_lists.create(name="foo")
@pytest.fixture
@scopes_disabled()
def team(organizer):
return Team.objects.create(
organizer=organizer,
name="Test-Team",
all_events=True,
can_change_teams=True,
can_manage_gift_cards=True,
can_change_items=True,
can_create_events=True,
can_change_event_settings=True,
can_change_vouchers=True,
can_view_vouchers=True,
can_view_orders=True,
can_change_orders=True,
can_manage_customers=True,
can_manage_reusable_media=True,
can_change_organizer_settings=True,
)
@pytest.fixture
def client():
return APIClient()
@pytest.fixture
@scopes_disabled()
def token_client(client, team):
t = team.tokens.create(name="Foo")
client.credentials(HTTP_AUTHORIZATION="Token " + t.token)
return client

View File

@@ -0,0 +1,155 @@
#
# 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/>.
#
import copy
import pytest
from django_scopes import scopes_disabled
@pytest.fixture
def acr(event, item):
acr = event.autocheckinrule_set.create(
all_products=False,
)
acr.limit_products.add(item)
return acr
RES_RULE = {
"list": None,
"mode": "placed",
"all_sales_channels": True,
"limit_sales_channels": [],
"all_products": False,
"limit_products": [],
"limit_variations": [],
"all_payment_methods": True,
"limit_payment_methods": set(),
}
@pytest.mark.django_db
def test_api_list(event, acr, item, token_client):
res = copy.copy(RES_RULE)
res["id"] = acr.pk
res["limit_products"] = [item.pk]
r = token_client.get(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/".format(
event.organizer.slug, event.slug
)
).data
assert r["results"] == [res]
@pytest.mark.django_db
def test_api_detail(event, acr, item, token_client):
res = copy.copy(RES_RULE)
res["id"] = acr.pk
res["limit_products"] = [item.pk]
r = token_client.get(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/{}/".format(
event.organizer.slug, event.slug, acr.pk
)
).data
assert r == res
@pytest.mark.django_db
def test_api_create(event, acr, item, token_client):
resp = token_client.post(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/".format(
event.slug, event.slug
),
{
"all_products": False,
"limit_products": [item.pk],
},
format="json",
)
assert resp.status_code == 201
with scopes_disabled():
acr = event.autocheckinrule_set.get(pk=resp.data["id"])
assert list(acr.limit_products.all()) == [item]
@pytest.mark.django_db
def test_api_create_validate_pprov(event, acr, item, token_client):
resp = token_client.post(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/".format(
event.slug, event.slug
),
{
"mode": "placed",
"all_payment_methods": False,
"limit_payment_methods": ["manual"],
},
format="json",
)
assert resp.status_code == 400
assert resp.data == {
"non_field_errors": ["all_payment_methods should be used for mode=placed"]
}
resp = token_client.post(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/".format(
event.slug, event.slug
),
{
"mode": "paid",
"all_payment_methods": False,
"limit_payment_methods": ["unknown"],
},
format="json",
)
assert resp.status_code == 400
assert resp.data == {"limit_payment_methods": ['"unknown" is not a valid choice.']}
@pytest.mark.django_db
def test_api_update(event, acr, item, token_client):
resp = token_client.patch(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/{}/".format(
event.slug, event.slug, acr.pk
),
{
"mode": "paid",
"all_payment_methods": False,
"limit_payment_methods": ["manual"],
},
format="json",
)
assert resp.status_code == 200
acr.refresh_from_db()
assert acr.all_payment_methods is False
assert acr.limit_payment_methods == ["manual"]
@pytest.mark.django_db
def test_api_delete(event, acr, item, token_client):
resp = token_client.delete(
"/api/v1/organizers/{}/events/{}/auto_checkin_rules/{}/".format(
event.slug, event.slug, acr.pk
),
)
assert resp.status_code == 204
assert not event.autocheckinrule_set.exists()

View File

@@ -0,0 +1,310 @@
#
# 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/>.
#
import copy
from datetime import timedelta
import pytest
from django.utils.timezone import now
from django_scopes import scopes_disabled
from tests.api.test_order_create import ORDER_CREATE_PAYLOAD
from pretix.base.models import Order, OrderPayment
from pretix.base.signals import order_paid, order_placed
from pretix.plugins.autocheckin.models import AutoCheckinRule
@pytest.fixture
@scopes_disabled()
def order(organizer, event, item):
order = Order.objects.create(
event=event,
status=Order.STATUS_PENDING,
expires=now() + timedelta(days=3),
sales_channel=organizer.sales_channels.get(identifier="web"),
total=4,
)
order.positions.create(order=order, item=item, price=2)
return order
@pytest.mark.django_db
@scopes_disabled()
def test_sales_channel_all(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
all_sales_channels=True,
)
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_sales_channel_limit(event, item, order, checkin_list):
acr = event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
all_sales_channels=False,
)
order_placed.send(event, order=order)
assert not order.positions.first().checkins.exists()
acr.limit_sales_channels.add(order.sales_channel)
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_items_all(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
all_products=True,
)
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_items_limit(event, item, order, checkin_list):
acr = event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
all_products=False,
)
order_placed.send(event, order=order)
assert not order.positions.first().checkins.exists()
acr.limit_products.add(item)
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_variations_limit(event, item, order, checkin_list):
var = item.variations.create(value="V1")
op = order.positions.first()
op.variation = var
op.save()
acr = event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
all_products=False,
)
order_placed.send(event, order=order)
assert not order.positions.first().checkins.exists()
acr.limit_variations.add(var)
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
order.positions.first().checkins.all().delete()
acr.limit_products.add(item)
acr.limit_variations.clear()
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_mode_placed(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
)
order_paid.send(event, order=order)
assert not order.positions.first().checkins.exists()
order_placed.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_mode_paid(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
)
order_placed.send(event, order=order)
assert not order.positions.first().checkins.exists()
order_paid.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_payment_provider_limit(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
all_payment_methods=False,
limit_payment_methods=["manual"],
)
p = order.payments.create(
amount=order.total,
state=OrderPayment.PAYMENT_STATE_CONFIRMED,
provider="banktransfer",
)
order_paid.send(event, order=order)
assert not order.positions.first().checkins.exists()
p.provider = "manual"
p.save()
order_paid.send(event, order=order)
assert order.positions.first().checkins.exists()
@pytest.mark.django_db
@scopes_disabled()
def test_idempodency(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
)
order_paid.send(event, order=order)
assert order.positions.first().checkins.count() == 1
order_paid.send(event, order=order)
assert order.positions.first().checkins.count() == 1
@pytest.mark.django_db
@scopes_disabled()
def test_multiple_rules_same_list(event, item, order, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
)
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
)
order_paid.send(event, order=order)
assert order.positions.first().checkins.count() == 1
@pytest.mark.django_db
@scopes_disabled()
def test_multiple_rules_different_lists(event, item, order, checkin_list):
cl2 = event.checkin_lists.create(name="bar")
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
)
event.autocheckinrule_set.create(
list=cl2,
mode=AutoCheckinRule.MODE_PAID,
)
order_paid.send(event, order=order)
assert order.positions.first().checkins.count() == 2
@pytest.mark.django_db
@scopes_disabled()
def test_autodetect_lists(event, item, order, checkin_list):
cl2 = event.checkin_lists.create(name="bar", all_products=False)
cl2.limit_products.add(item)
event.checkin_lists.create(name="baz", all_products=False)
event.autocheckinrule_set.create(
mode=AutoCheckinRule.MODE_PAID,
)
order_paid.send(event, order=order)
assert {c.list_id for c in order.positions.first().checkins.all()} == {
checkin_list.pk,
cl2.pk,
}
@pytest.mark.django_db
@scopes_disabled()
def test_order_create_via_api_placed(
token_client, organizer, event, item, checkin_list
):
acr = event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PLACED,
all_sales_channels=False,
)
acr.limit_sales_channels.add(organizer.sales_channels.get(identifier="web"))
q = event.quotas.create(name="Foo", size=None)
q.items.add(item)
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res["positions"][0]["item"] = item.pk
res["positions"][0]["answers"] = []
resp = token_client.post(
"/api/v1/organizers/{}/events/{}/orders/".format(organizer.slug, event.slug),
format="json",
data=res,
)
assert resp.status_code == 201
o = Order.objects.get(code=resp.data["code"])
assert o.positions.first().checkins.first().auto_checked_in
@pytest.mark.django_db
@scopes_disabled()
def test_order_create_via_api_paid(token_client, organizer, event, item, checkin_list):
event.autocheckinrule_set.create(
list=checkin_list,
mode=AutoCheckinRule.MODE_PAID,
all_payment_methods=False,
limit_payment_methods=["manual"],
)
q = event.quotas.create(name="Foo", size=None)
q.items.add(item)
res = copy.deepcopy(ORDER_CREATE_PAYLOAD)
res["positions"][0]["item"] = item.pk
res["positions"][0]["answers"] = []
res["status"] = "p"
res["payment_provider"] = "manual"
resp = token_client.post(
"/api/v1/organizers/{}/events/{}/orders/".format(organizer.slug, event.slug),
format="json",
data=res,
)
assert resp.status_code == 201
o = Order.objects.get(code=resp.data["code"])
assert o.positions.first().checkins.first().auto_checked_in

View File

@@ -0,0 +1,185 @@
#
# 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/>.
#
import datetime
from django_scopes import scopes_disabled
from tests.base import SoupTest, extract_form_fields
from pretix.base.models import Event, Item, Organizer, Team, User
from pretix.plugins.autocheckin.models import AutoCheckinRule
class AutoCheckinFormTest(SoupTest):
def setUp(self):
super().setUp()
self.user = User.objects.create_user("dummy@dummy.dummy", "dummy")
self.orga1 = Organizer.objects.create(name="CCC", slug="ccc")
self.orga2 = Organizer.objects.create(name="MRM", slug="mrm")
self.event1 = Event.objects.create(
organizer=self.orga1,
name="30C3",
slug="30c3",
plugins="pretix.plugins.autocheckin",
date_from=datetime.datetime(2013, 12, 26, tzinfo=datetime.timezone.utc),
)
self.item1 = Item.objects.create(
event=self.event1, name="Standard", default_price=0, position=1
)
t = Team.objects.create(
organizer=self.orga1,
can_change_event_settings=True,
can_view_orders=True,
can_change_items=True,
all_events=True,
can_create_events=True,
can_change_orders=True,
can_change_vouchers=True,
)
t.members.add(self.user)
t = Team.objects.create(
organizer=self.orga2,
can_change_event_settings=True,
can_view_orders=True,
can_change_items=True,
all_events=True,
can_create_events=True,
can_change_orders=True,
can_change_vouchers=True,
)
t.members.add(self.user)
self.client.login(email="dummy@dummy.dummy", password="dummy")
def test_create(self):
doc = self.get_doc(
"/control/event/%s/%s/autocheckin/add" % (self.orga1.slug, self.event1.slug)
)
form_data = extract_form_fields(doc.select(".container-fluid form")[0])
doc = self.post_doc(
"/control/event/%s/%s/autocheckin/add"
% (self.orga1.slug, self.event1.slug),
form_data,
)
assert doc.select(".alert-success")
assert self.event1.autocheckinrule_set.exists()
def test_delete(self):
acr = self.event1.autocheckinrule_set.create(
mode=AutoCheckinRule.MODE_PAID,
all_payment_methods=False,
limit_payment_methods=["manual"],
)
doc = self.get_doc(
"/control/event/%s/%s/autocheckin/%s/delete"
% (self.orga1.slug, self.event1.slug, acr.id)
)
form_data = extract_form_fields(doc.select(".container-fluid form")[0])
doc = self.post_doc(
"/control/event/%s/%s/autocheckin/%s/delete"
% (self.orga1.slug, self.event1.slug, acr.id),
form_data,
)
assert doc.select(".alert-success")
with scopes_disabled():
assert self.event1.autocheckinrule_set.count() == 0
def test_item_copy(self):
with scopes_disabled():
acr = self.event1.autocheckinrule_set.create(
mode=AutoCheckinRule.MODE_PAID, all_products=False
)
acr.limit_products.add(self.item1)
self.client.post(
"/control/event/%s/%s/items/add" % (self.orga1.slug, self.event1.slug),
{
"name_0": "Intermediate",
"default_price": "23.00",
"tax_rate": "19.00",
"copy_from": str(self.item1.pk),
"has_variations": "1",
},
)
with scopes_disabled():
acr.refresh_from_db()
i_new = Item.objects.get(name__icontains="Intermediate")
assert i_new in acr.limit_products.all()
def test_copy_event(self):
with scopes_disabled():
acr = self.event1.autocheckinrule_set.create(
list=self.event1.checkin_lists.create(name="Test"),
mode=AutoCheckinRule.MODE_PAID,
all_products=False,
all_sales_channels=False,
)
acr.limit_products.add(self.item1)
acr.limit_sales_channels.add(
self.orga1.sales_channels.get(identifier="web")
)
self.post_doc(
"/control/events/add",
{
"event_wizard-current_step": "foundation",
"event_wizard-prefix": "event_wizard",
"foundation-organizer": self.orga2.pk,
"foundation-locales": ("en",),
},
)
self.post_doc(
"/control/events/add",
{
"event_wizard-current_step": "basics",
"event_wizard-prefix": "event_wizard",
"basics-name_0": "33C3",
"basics-slug": "33c3",
"basics-date_from_0": "2016-12-27",
"basics-date_from_1": "10:00:00",
"basics-date_to_0": "2016-12-30",
"basics-date_to_1": "19:00:00",
"basics-location_0": "Hamburg",
"basics-currency": "EUR",
"basics-tax_rate": "19.00",
"basics-locale": "en",
"basics-timezone": "Europe/Berlin",
},
)
self.post_doc(
"/control/events/add",
{
"event_wizard-current_step": "copy",
"event_wizard-prefix": "event_wizard",
"copy-copy_from_event": self.event1.pk,
},
)
with scopes_disabled():
ev = Event.objects.get(slug="33c3")
i_new = ev.items.first()
acr_new = ev.autocheckinrule_set.get()
assert i_new in acr_new.limit_products.all()
assert list(acr_new.limit_sales_channels.all()) == [
self.orga2.sales_channels.get(identifier="web")
]
assert acr_new.list.event == ev