Skip to content

Commit

Permalink
Merge pull request #553 from tulimaki/solve-group-availability-for-un…
Browse files Browse the repository at this point in the history
…saved-contacts

Core: Handle unsaved customers in group availability component (SHUUP-2878)
  • Loading branch information
Pikkupomo committed Jun 17, 2016
2 parents 9c2b337 + d0c5d80 commit cfc7779
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion shoop/core/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-06-15 19:11+0000\n"
"POT-Creation-Date: 2016-06-17 19:37+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: en <LL@li.org>\n"
Expand Down Expand Up @@ -1031,6 +1031,9 @@ msgstr ""
msgid "groups"
msgstr ""

msgid "Customer does not belong to any group."
msgstr ""

msgid "Service is not available for any of the customers groups."
msgstr ""

Expand Down
4 changes: 4 additions & 0 deletions shoop/core/models/_service_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ class GroupAvailabilityBehaviorComponent(ServiceBehaviorComponent):
groups = models.ManyToManyField("ContactGroup", verbose_name=_("groups"))

def get_unavailability_reasons(self, service, source):
if not source.customer.pk:
yield ValidationError(_("Customer does not belong to any group."))
return

customer_groups = set(source.customer.groups.all().values_list("pk", flat=True))
groups_to_match = set(self.groups.all().values_list("pk", flat=True))
if not bool(customer_groups & groups_to_match):
Expand Down
13 changes: 12 additions & 1 deletion shoop_tests/core/test_contact_group_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

from shoop.core.models import (
GroupAvailabilityBehaviorComponent, OrderLineType
GroupAvailabilityBehaviorComponent, OrderLineType, PersonContact
)
from shoop.testing.factories import (
create_product, create_random_person, get_default_customer_group,
Expand Down Expand Up @@ -67,6 +67,17 @@ def test_with_multiple_groups(admin_user):
_test_service_availability(source, payment_method, True)


@pytest.mark.django_db
def test_unsaved_contact(admin_user):
payment_method = get_default_payment_method()
_assign_component_for_service(payment_method, [PersonContact.get_default_group()])
person = PersonContact(name="Kalle")
source = _get_source_for_contact(admin_user, payment_method)
source.customer = person
assert not person.pk and not source.customer.pk
_test_service_availability(source, payment_method, False)


def _assign_component_for_service(service, groups):
assert service.behavior_components.count() == 0
component = GroupAvailabilityBehaviorComponent.objects.create()
Expand Down

0 comments on commit cfc7779

Please sign in to comment.