Skip to content

Commit

Permalink
Merge pull request #1834 from jovan-shuup/fix_happy_hour
Browse files Browse the repository at this point in the history
Happy Hour: add check for weekdays value instance type
  • Loading branch information
tulimaki committed Mar 31, 2019
2 parents bf1541e + aa83a70 commit 75258df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion shuup/admin/forms/fields.py
Expand Up @@ -10,6 +10,7 @@
from django.forms import (
DecimalField, Field, MultipleChoiceField, Select, SelectMultiple
)
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _


Expand Down Expand Up @@ -119,7 +120,14 @@ class WeekdaysSelectMultiple(SelectMultiple):
def format_value(self, value):
if value is None and self.allow_multiple_selected:
return []
return ["%s" % v for v in value.split(",") if v]

if isinstance(value, str):
value = value.split(",")

if not isinstance(value, (tuple, list)):
value = [value]

return [force_text(v) if v is not None else '' for v in value]


class WeekdayField(MultipleChoiceField):
Expand Down
25 changes: 25 additions & 0 deletions shuup_tests/admin/test_fields.py
Expand Up @@ -6,8 +6,11 @@
# LICENSE file in the root directory of this source tree.
import pytest
from bs4 import BeautifulSoup
from django import forms
from django.utils.encoding import force_text
from django.utils.translation import activate

from shuup.admin.forms.fields import WeekdayField, WeekdaysSelectMultiple
from shuup.admin.modules.product_types.views import ProductTypeEditView
from shuup.core.models import Attribute, AttributeType, ProductType
from shuup.testing.utils import apply_request_middleware
Expand Down Expand Up @@ -35,3 +38,25 @@ def test_select2multiplefield(rf, admin_user):
assert attr2.name in options_names
assert attr1.id in options_values
assert attr2.id in options_values


@pytest.mark.django_db
def test_WeekdayField(rf, admin_user):
activate("en")

class TestForm(forms.Form):
weekdays = WeekdayField()

form = TestForm(data={"weekdays": [1, 2, 3]})
assert form.is_valid()
assert form.fields["weekdays"].to_python([1, 2, 3]) == ["1", "2", "3"]

soup = BeautifulSoup(force_text(form), "lxml")
selected_options = soup.find_all("option", {"selected": True})
non_selected_options = soup.find_all("option", {"selected": False})
assert [opt.attrs["value"] for opt in selected_options] == ["1", "2", "3"]
assert [opt.attrs["value"] for opt in non_selected_options] == ["0", "4", "5", "6"]

assert WeekdaysSelectMultiple().format_value("1,2,3") == ["1", "2", "3"]
assert WeekdaysSelectMultiple().format_value("123") == ["123"]
assert WeekdaysSelectMultiple().format_value(1) == ["1"]

0 comments on commit 75258df

Please sign in to comment.