Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Materialize image widget

Marcin Gębalaed27c6771eee
diff --git a/saleor/core/templates/materializecssform/field.html b/saleor/core/templates/materializecssform/field.html
index ff046b090a..9834651151 100644
--- a/saleor/core/templates/materializecssform/field.html
+++ b/saleor/core/templates/materializecssform/field.html
@@ -1,4 +1,5 @@
{% load materializecss %}
+{% load utils %}
{% if field|is_checkbox %}
@@ -129,7 +130,21 @@
{% endif %}
{{ field }}
</p>
+ </div>
+ {% elif field|is_versatile_image_ppoi_click_widget %}
+ <div class="col file-field input-field {{ classes.label }}">
+ {% if field.auto_id %}
+ <label class="{{ classes.label }} {% if field.field.required %}{{ form.required_css_class }}{% endif %}" for="{{ field.auto_id }}">{{ field.label }}</label><br>
+ {% endif %}
+ <div class="btn">
+ <span>File</span>
+ <input class="file-chooser" id="{{ field.auto_id }}_0" name="{{ field.name }}_0" type="file">
+ <input class="ppoi-input" id="{{ field.auto_id }}_1" name="{{ field.name }}_1" type="hidden">
+ </div>
+ <div class="file-path-wrapper">
+ <input class="file-path validate" type="text">
+ </div>
{% for error in field.errors %}
<p class="help-block materialize-red-text">{{ error }}</p>
{% endfor %}
@@ -139,7 +154,8 @@
{{ field.help_text|safe }}
</p>
{% endif %}
- </div>
+
+ </div>
{% else %}
<div class="input-field col {{ classes.label }}">
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 3835b032aa..9dd5193b42 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -6,6 +6,7 @@ from django.utils.translation import pgettext_lazy
from ...product.models import (ProductImage, Stock, ProductVariant, Product,
ProductAttribute, AttributeChoiceValue)
+from .widgets import ImagePreviewWidget
PRODUCT_CLASSES = {Product: 'Default'}
@@ -122,10 +123,16 @@ class StockBulkDeleteForm(forms.Form):
class ProductImageForm(forms.ModelForm):
+
class Meta:
model = ProductImage
exclude = ('product', 'order')
+ def __init__(self, *args, **kwargs):
+ super(ProductImageForm, self).__init__(*args, **kwargs)
+ if self.instance.image:
+ self.fields['image'].widget = ImagePreviewWidget()
+
class ProductAttributeForm(forms.ModelForm):
class Meta:
diff --git a/saleor/dashboard/product/widgets.py b/saleor/dashboard/product/widgets.py
new file mode 100644
index 0000000000..fc462ace16
--- /dev/null
+++ b/saleor/dashboard/product/widgets.py
@@ -0,0 +1,26 @@
+from django.forms import MultiWidget, HiddenInput
+from versatileimagefield.widgets import (ClearableFileInputWithImagePreview,
+ SizedImageCenterpointWidgetMixIn)
+
+
+class ImagePreviewFileInput(ClearableFileInputWithImagePreview):
+ template_with_initial_and_imagepreview = """
+ <div class="sizedimage-mod preview">
+ <div class="image-wrap outer">
+ <div class="point-stage" id="%(point_stage_id)s"
+ data-image_preview_id="%(image_preview_id)s">
+ <div class="ppoi-point" id="%(ppoi_id)s"></div>
+ </div>
+ <div class="image-wrap inner">
+ %(image_preview)s
+ </div>
+ </div>
+ </div>"""
+
+
+class ImagePreviewWidget(SizedImageCenterpointWidgetMixIn, MultiWidget):
+
+ def __init__(self, attrs=None):
+ widgets = (ImagePreviewFileInput(attrs={'class': 'file-chooser'}),
+ HiddenInput(attrs={'class': 'ppoi-input'}))
+ super(ImagePreviewWidget, self).__init__(widgets, attrs)
\ No newline at end of file
diff --git a/saleor/dashboard/templates/dashboard/product/product_image_form.html b/saleor/dashboard/templates/dashboard/product/product_image_form.html
index c878034967..cf60deaadc 100644
--- a/saleor/dashboard/templates/dashboard/product/product_image_form.html
+++ b/saleor/dashboard/templates/dashboard/product/product_image_form.html
@@ -40,7 +40,7 @@
<div class="row">
<div class="col l8">
{% csrf_token %}
- {{ form.image }}
+ {{ form.image|materializecss }}
<div class="row">
{{ form.alt|materializecss }}
diff --git a/saleor/dashboard/templatetags/utils.py b/saleor/dashboard/templatetags/utils.py
index 775129792c..51ce3ede48 100644
--- a/saleor/dashboard/templatetags/utils.py
+++ b/saleor/dashboard/templatetags/utils.py
@@ -1,4 +1,7 @@
import json
+
+from versatileimagefield.widgets import VersatileImagePPOIClickWidget
+
try:
from urllib.parse import urlencode
except ImportError:
@@ -23,3 +26,8 @@ def construct_get_query(context, **params):
all_params.update(request_get)
all_params.update(params)
return '?' + urlencode(all_params)
+
+
+def is_versatile_image_ppoi_click_widget(field):
+ return isinstance(field.field.widget, VersatileImagePPOIClickWidget)