diff --git a/saleor/account/models.py b/saleor/account/models.py index 584b4e67019..4be962aa1b6 100644 --- a/saleor/account/models.py +++ b/saleor/account/models.py @@ -181,12 +181,6 @@ def get_full_name(self): def get_short_name(self): return self.email - def get_ajax_label(self): - address = self.default_billing_address - if address: - return "%s %s (%s)" % (address.first_name, address.last_name, self.email) - return self.email - class ServiceAccount(ModelWithMetadata): name = models.CharField(max_length=60) diff --git a/saleor/core/templatetags/version.py b/saleor/core/templatetags/version.py deleted file mode 100644 index 5eca41e3ea4..00000000000 --- a/saleor/core/templatetags/version.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.template import Library - -from ... import __version__ - -register = Library() - - -@register.simple_tag -def version(): - return __version__ diff --git a/saleor/core/utils/text.py b/saleor/core/utils/text.py deleted file mode 100644 index d3920969324..00000000000 --- a/saleor/core/utils/text.py +++ /dev/null @@ -1,62 +0,0 @@ -# coding: utf-8 - -import bleach -from django.template.defaultfilters import truncatechars -from html5lib.serializer import HTMLSerializer - - -def get_cleaner(**serializer_kwargs: bool): - """Build a bleach cleaner from the passed parameters. - - :param serializer_kwargs: - options: - - alphabetical_attributes - - escape_lt_in_attrs - - escape_rcdata - - inject_meta_charset - - minimize_boolean_attributes - - omit_optional_tags - - quote_attr_values - - quote_char - - resolve_entities - - sanitize - - space_before_trailing_solidus - - strip_whitespace - - use_best_quote_char - - use_trailing_solidus - :type serializer_kwargs: Dict[str, bool] - - :rtype: bleach.Cleaner - """ - cleaner = bleach.Cleaner([], strip=True) - for k, v in serializer_kwargs.items(): - if k not in HTMLSerializer.options: - raise ValueError( - "Parameter %s is not a valid option for HTMLSerializer" % k - ) - setattr(cleaner.serializer, k, v) - return cleaner - - -def strip_html(text: str, **serializer_kwargs: bool): - """Remove (strip) HTML tags from text. - - Can also take additional parameters - to be passed to the serializer (see `get_cleaner`). - - :param text: - :type text: str - :param serializer_kwargs: - :type serializer_kwargs: Dict[str, bool] - :rtype: str - """ - cleaner = get_cleaner(**serializer_kwargs) - text = cleaner.clean(text) - return text - - -def strip_html_and_truncate(html_text: str, max_length: int): - """Strip HTML tags and whitespaces from the text, then trim the description.""" - text = strip_html(html_text, strip_whitespace=True) - text = truncatechars(text, max_length) - return text diff --git a/saleor/core/weight.py b/saleor/core/weight.py index 6595ee7c525..eeb29d361d9 100644 --- a/saleor/core/weight.py +++ b/saleor/core/weight.py @@ -12,12 +12,7 @@ In the end, it does not really matter unless you travel between different planets. """ -from decimal import Decimal - -from django import forms from django.contrib.sites.models import Site -from django.core.validators import MinValueValidator -from django.template.loader import render_to_string from django.utils.translation import pgettext_lazy from measurement.measures import Weight @@ -51,63 +46,3 @@ def convert_weight(weight, unit): def get_default_weight_unit(): site = Site.objects.get_current() return site.settings.default_weight_unit - - -class WeightInput(forms.TextInput): - template = "dashboard/shipping/weight_widget.html" - input_type = "number" - - def format_value(self, value): - if isinstance(value, Weight): - unit = get_default_weight_unit() - if value.unit != unit: - value = convert_weight(value, unit) - return value.value - return value - - def render(self, name, value, attrs=None, renderer=None): - widget = super().render(name, value, attrs=attrs, renderer=renderer) - unit = get_default_weight_unit() - translated_unit = dict(WeightUnits.CHOICES)[unit] - return render_to_string( - self.template, {"widget": widget, "value": value, "unit": translated_unit} - ) - - -class WeightField(forms.FloatField): - def __init__(self, *args, widget=WeightInput, min_value=0, **kwargs): - if isinstance(widget, type): - widget = widget(attrs={"type": "number", "step": "any"}) - super().__init__(*args, widget=widget, **kwargs) - if min_value is not None: - self.validators.append(MinValueValidator(min_value)) - - def to_python(self, value): - value = super().to_python(value) - if value is None: - return value - unit = get_default_weight_unit() - return Weight(**{unit: value}) - - def validate(self, weight): - if weight is None or weight in self.empty_values: - super().validate(weight) - else: - unit = get_default_weight_unit() - if not isinstance(weight, Weight): - raise Exception("%r is not a valid weight." % (weight,)) - if weight.unit != unit: - raise forms.ValidationError( - "Invalid unit: %r (expected %r)." % (weight.unit, unit), - code="invalid", - ) - super().validate(weight.value) - - def clean(self, value): - value = value_to_be_validated = self.to_python(value) - self.validate(value_to_be_validated) - if isinstance(value, Weight): - value_to_be_validated = Decimal(value.value) - # default decimal validators can be used for Weight's value only - self.run_validators(value_to_be_validated) - return value diff --git a/saleor/menu/models.py b/saleor/menu/models.py index 3b1a2763bb0..98c58179f7c 100644 --- a/saleor/menu/models.py +++ b/saleor/menu/models.py @@ -62,23 +62,6 @@ def get_ordering_queryset(self): def linked_object(self): return self.category or self.collection or self.page - @property - def destination_display(self): - linked_object = self.linked_object - - if not linked_object: - prefix = pgettext_lazy("Link object type description", "URL: ") - return prefix + self.url - - if isinstance(linked_object, Category): - prefix = pgettext_lazy("Link object type description", "Category: ") - elif isinstance(linked_object, Collection): - prefix = pgettext_lazy("Link object type description", "Collection: ") - else: - prefix = pgettext_lazy("Link object type description", "Page: ") - - return prefix + str(linked_object) - def get_url(self): linked_object = self.linked_object return linked_object.get_absolute_url() if linked_object else self.url diff --git a/saleor/order/actions.py b/saleor/order/actions.py index 04ba227143e..867420d1ee2 100644 --- a/saleor/order/actions.py +++ b/saleor/order/actions.py @@ -21,7 +21,7 @@ if TYPE_CHECKING: from .models import Order - from ..account.models import User, Address + from ..account.models import User from ..payment.models import Payment @@ -129,11 +129,6 @@ def order_shipping_updated(order: "Order"): get_extensions_manager().order_updated(order) -def order_address_updated(order: "Order", user: "User", address: "Address"): - events.order_updated_address_event(order=order, user=user, address=address) - get_extensions_manager().order_updated(order) - - def order_captured(order: "Order", user: "User", amount: "Decimal", payment: "Payment"): events.payment_captured_event( order=order, user=user, amount=amount, payment=payment diff --git a/saleor/order/models.py b/saleor/order/models.py index 48f8ea69d9b..5f0cd30d679 100644 --- a/saleor/order/models.py +++ b/saleor/order/models.py @@ -283,14 +283,6 @@ def can_capture(self, payment=None): order_status_ok = self.status not in {OrderStatus.DRAFT, OrderStatus.CANCELED} return payment.can_capture() and order_status_ok - def can_charge(self, payment=None): - if not payment: - payment = self.get_last_payment() - if not payment: - return False - order_status_ok = self.status not in {OrderStatus.DRAFT, OrderStatus.CANCELED} - return payment.can_charge() and order_status_ok - def can_void(self, payment=None): if not payment: payment = self.get_last_payment() diff --git a/saleor/page/models.py b/saleor/page/models.py index a8e7da8dedf..bac7ee2d5b1 100644 --- a/saleor/page/models.py +++ b/saleor/page/models.py @@ -7,7 +7,6 @@ from ..core.db.fields import SanitizedJSONField from ..core.models import PublishableModel, PublishedQuerySet -from ..core.utils import build_absolute_uri from ..core.utils.draftjs import json_content_to_raw_text from ..core.utils.translations import TranslationProxy from ..seo.models import SeoModel, SeoModelTranslation @@ -49,9 +48,6 @@ def plain_text_content(self): def get_absolute_url(self): return reverse("page:details", kwargs={"slug": self.slug}) - def get_full_url(self): - return build_absolute_uri(self.get_absolute_url()) - class PageTranslation(SeoModelTranslation): language_code = models.CharField(max_length=10) diff --git a/saleor/product/migrations/0053_product_seo_description.py b/saleor/product/migrations/0053_product_seo_description.py index 596388b5cd6..464db840d40 100644 --- a/saleor/product/migrations/0053_product_seo_description.py +++ b/saleor/product/migrations/0053_product_seo_description.py @@ -1,10 +1,68 @@ # Generated by Django 2.0.2 on 2018-03-11 18:54 import html +import bleach from django.core.validators import MaxLengthValidator from django.db import migrations, models +from django.template.defaultfilters import truncatechars +from html5lib.serializer import HTMLSerializer -from saleor.core.utils.text import strip_html_and_truncate + +def get_cleaner(**serializer_kwargs: bool): + """Build a bleach cleaner from the passed parameters. + + :param serializer_kwargs: + options: + - alphabetical_attributes + - escape_lt_in_attrs + - escape_rcdata + - inject_meta_charset + - minimize_boolean_attributes + - omit_optional_tags + - quote_attr_values + - quote_char + - resolve_entities + - sanitize + - space_before_trailing_solidus + - strip_whitespace + - use_best_quote_char + - use_trailing_solidus + :type serializer_kwargs: Dict[str, bool] + + :rtype: bleach.Cleaner + """ + cleaner = bleach.Cleaner([], strip=True) + for k, v in serializer_kwargs.items(): + if k not in HTMLSerializer.options: + raise ValueError( + "Parameter %s is not a valid option for HTMLSerializer" % k + ) + setattr(cleaner.serializer, k, v) + return cleaner + + +def strip_html(text: str, **serializer_kwargs: bool): + """Remove (strip) HTML tags from text. + + Can also take additional parameters + to be passed to the serializer (see `get_cleaner`). + + :param text: + :type text: str + :param serializer_kwargs: + :type serializer_kwargs: Dict[str, bool] + :rtype: str + """ + cleaner = get_cleaner(**serializer_kwargs) + text = cleaner.clean(text) + return text + + +def strip_html_and_truncate(html_text: str, max_length: int): + """Strip HTML tags and whitespaces from the text, then trim the description.""" + text = strip_html(html_text, strip_whitespace=True) + text = truncatechars(text, max_length) + return text def to_seo_friendly(text): diff --git a/saleor/product/models.py b/saleor/product/models.py index 330de57b4cd..557ebd8caeb 100644 --- a/saleor/product/models.py +++ b/saleor/product/models.py @@ -15,7 +15,6 @@ from django.utils.translation import pgettext_lazy from django_measurement.models import MeasurementField from django_prices.models import MoneyField -from django_prices.templatetags import prices from draftjs_sanitizer import clean_draft_js from measurement.measures import Weight from mptt.managers import TreeManager @@ -532,10 +531,6 @@ def get_first_image(self): images = list(self.images.all()) return images[0] if images else self.product.get_first_image() - def get_ajax_label(self, discounts=None): - price = self.get_price(discounts) - return "%s, %s, %s" % (self.sku, self.display_product(), prices.amount(price)) - class ProductVariantTranslation(models.Model): language_code = models.CharField(max_length=10) diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py index 35bd56bcee1..1f75b9b104b 100644 --- a/saleor/shipping/models.py +++ b/saleor/shipping/models.py @@ -4,7 +4,6 @@ from django.contrib.postgres.fields import JSONField from django.db import models from django.db.models import Q -from django.utils.safestring import mark_safe from django.utils.translation import pgettext_lazy from django_countries.fields import CountryField from django_measurement.models import MeasurementField @@ -12,7 +11,6 @@ from measurement.measures import Weight from prices import Money, MoneyRange -from ..core.utils import format_money from ..core.utils.json_serializer import CustomJsonEncoder from ..core.utils.translations import TranslationProxy from ..core.weight import ( @@ -219,11 +217,6 @@ def __repr__(self): def get_total(self): return self.price - def get_ajax_label(self): - price_html = format_money(self.price) - label = mark_safe("%s %s" % (self, price_html)) - return label - class ShippingMethodTranslation(models.Model): language_code = models.CharField(max_length=10) diff --git a/tests/test_account.py b/tests/test_account.py index 50988a64208..bba9966e86d 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -263,16 +263,6 @@ def test_compare_addresses_different_country(address): assert address != copied_address -def test_user_ajax_label(customer_user): - address = customer_user.default_billing_address - label = "%s %s (%s)" % (address.first_name, address.last_name, customer_user.email) - assert customer_user.get_ajax_label() == label - - -def test_user_ajax_label_without_address(admin_user): - assert admin_user.get_ajax_label() == admin_user.email - - @pytest.mark.parametrize( "email, first_name, last_name, full_name", [ diff --git a/tests/test_core.py b/tests/test_core.py index 9f8ac67acd8..f1a491de4cf 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -25,7 +25,6 @@ get_currency_for_country, random_data, ) -from saleor.core.utils.text import get_cleaner, strip_html from saleor.core.weight import WeightUnits, convert_weight from saleor.discount.models import Sale, Voucher from saleor.giftcard.models import GiftCard @@ -193,17 +192,6 @@ def test_manifest(client, site_settings): assert content["description"] == site_settings.description -def test_utils_get_cleaner_invalid_parameters(): - with pytest.raises(ValueError): - get_cleaner(bad=True) - - -def test_utils_strip_html(): - base_text = "
Hello
" "\n\n" "\tWorld" - text = strip_html(base_text, strip_whitespace=True) - assert text == "Hello World" - - @override_settings(VERSATILEIMAGEFIELD_SETTINGS={"create_images_on_demand": False}) def test_create_thumbnails(product_with_image, settings): sizeset = settings.VERSATILEIMAGEFIELD_RENDITION_KEY_SETS["products"] diff --git a/tests/test_shipping.py b/tests/test_shipping.py index 5dbbc792070..5d0074c556c 100644 --- a/tests/test_shipping.py +++ b/tests/test_shipping.py @@ -2,7 +2,6 @@ from measurement.measures import Weight from prices import Money -from saleor.core.utils import format_money from saleor.shipping.models import ShippingMethod, ShippingMethodType, ShippingZone from saleor.shipping.utils import default_shipping_zone_exists @@ -16,16 +15,6 @@ def test_shipping_get_total(monkeypatch, shipping_zone): assert method.get_total() == price -def test_shipping_get_ajax_label(shipping_zone): - shipping_method = shipping_zone.shipping_methods.get() - label = shipping_method.get_ajax_label() - proper_label = "%(shipping_method)s %(price)s" % { - "shipping_method": shipping_method, - "price": format_money(shipping_method.price), - } - assert label == proper_label - - @pytest.mark.parametrize( "price, min_price, max_price, shipping_included", (