lfs-samples 1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lfs_samples/__init__.py +0 -0
- lfs_samples/admin.py +9 -0
- lfs_samples/apps.py +24 -0
- lfs_samples/locale/de/LC_MESSAGES/django.mo +0 -0
- lfs_samples/locale/de/LC_MESSAGES/django.po +68 -0
- lfs_samples/migrations/0001_initial.py +47 -0
- lfs_samples/migrations/0002_issample.py +25 -0
- lfs_samples/migrations/__init__.py +0 -0
- lfs_samples/models.py +38 -0
- lfs_samples/templates/lfs_samples/samples.html +56 -0
- lfs_samples/templates/lfs_samples/samples_inline.html +161 -0
- lfs_samples/templates/manage/products/tabs/_assigned_samples.html +88 -0
- lfs_samples/templates/manage/products/tabs/_available_samples.html +88 -0
- lfs_samples/templates/manage/products/tabs/_samples.html +103 -0
- lfs_samples/templatetags/__init__.py +0 -0
- lfs_samples/templatetags/lfs_samples_tags.py +68 -0
- lfs_samples/tests/__init__.py +0 -0
- lfs_samples/tests/settings.py +44 -0
- lfs_samples/tests/test_manage_tab.py +26 -0
- lfs_samples/tests/urls.py +9 -0
- lfs_samples/urls.py +17 -0
- lfs_samples/utils.py +49 -0
- lfs_samples/views.py +388 -0
- lfs_samples-1.1.dist-info/METADATA +150 -0
- lfs_samples-1.1.dist-info/RECORD +28 -0
- lfs_samples-1.1.dist-info/WHEEL +5 -0
- lfs_samples-1.1.dist-info/licenses/LICENSE.txt +27 -0
- lfs_samples-1.1.dist-info/top_level.txt +1 -0
lfs_samples/views.py
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from django.contrib import messages
|
|
4
|
+
from django.contrib.auth.decorators import permission_required
|
|
5
|
+
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
6
|
+
from django.core.paginator import EmptyPage
|
|
7
|
+
from django.core.paginator import Paginator
|
|
8
|
+
from django.core.paginator import PageNotAnInteger
|
|
9
|
+
from django.db.models import Q
|
|
10
|
+
from django.http import HttpRequest, HttpResponse
|
|
11
|
+
from django.template.loader import render_to_string
|
|
12
|
+
from django.urls import reverse
|
|
13
|
+
from django.utils.safestring import mark_safe
|
|
14
|
+
from django.utils.translation import gettext_lazy as _
|
|
15
|
+
from django.views.generic import TemplateView
|
|
16
|
+
|
|
17
|
+
from lfs.caching.utils import lfs_get_object_or_404
|
|
18
|
+
from lfs.catalog.models import Category
|
|
19
|
+
from lfs.catalog.models import Product
|
|
20
|
+
from lfs.catalog.settings import VARIANT
|
|
21
|
+
from lfs.core.signals import product_changed
|
|
22
|
+
from lfs.core.utils import LazyEncoder
|
|
23
|
+
from lfs.manage.products.views import ProductTabMixin
|
|
24
|
+
|
|
25
|
+
from .models import ActivityState
|
|
26
|
+
from .models import IsSample
|
|
27
|
+
from .models import ProductSamplesRelation
|
|
28
|
+
from .utils import add_sample
|
|
29
|
+
from .utils import has_active_samples
|
|
30
|
+
from .utils import is_sample
|
|
31
|
+
from .utils import remove_sample
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def get_parameter(r, p, default=None, with_session=True):
|
|
35
|
+
if with_session:
|
|
36
|
+
return r.GET.get(p, r.POST.get(p, r.session.get(p, default)))
|
|
37
|
+
else:
|
|
38
|
+
return r.GET.get(p, r.POST.get(p, default))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# Parts
|
|
42
|
+
@permission_required("core.manage_shop")
|
|
43
|
+
def manage_samples(request, product_id):
|
|
44
|
+
product = Product.objects.get(pk=product_id)
|
|
45
|
+
samples = ProductSamplesRelation.objects.filter(product_id=product_id)
|
|
46
|
+
|
|
47
|
+
# amount options
|
|
48
|
+
amount_options = []
|
|
49
|
+
for value in (10, 25, 50, 100):
|
|
50
|
+
amount_options.append({"value": value, "selected": value == request.session.get("samples-amount")})
|
|
51
|
+
|
|
52
|
+
samples_inline = manage_samples_inline(request, product_id, as_string=True)
|
|
53
|
+
|
|
54
|
+
result = render_to_string(
|
|
55
|
+
"lfs_samples/samples.html",
|
|
56
|
+
request=request,
|
|
57
|
+
context={
|
|
58
|
+
"product": product,
|
|
59
|
+
"samples": samples,
|
|
60
|
+
"amount_options": amount_options,
|
|
61
|
+
"samples_inline": samples_inline,
|
|
62
|
+
"has_active_samples": has_active_samples(product),
|
|
63
|
+
"is_sample": is_sample(product),
|
|
64
|
+
},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
return mark_safe(result)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@permission_required("core.manage_shop")
|
|
71
|
+
def manage_samples_inline(request, product_id, as_string=False, template_name="lfs_samples/samples_inline.html"):
|
|
72
|
+
"""View which shows all samples for the product with the passed id."""
|
|
73
|
+
product = Product.objects.get(pk=product_id)
|
|
74
|
+
|
|
75
|
+
samples = []
|
|
76
|
+
samples_ids = []
|
|
77
|
+
for psr in ProductSamplesRelation.objects.filter(product_id=product_id):
|
|
78
|
+
samples.append(psr.sample)
|
|
79
|
+
samples_ids.append(psr.sample.id)
|
|
80
|
+
|
|
81
|
+
s = request.session
|
|
82
|
+
|
|
83
|
+
page = get_parameter(request, "page", default=1)
|
|
84
|
+
|
|
85
|
+
# If we get the parameter ``keep-filters`` or ``page`` we take the
|
|
86
|
+
# filters out of the request resp. session. The request takes precedence.
|
|
87
|
+
# The page parameter is given if the user clicks on the next/previous page
|
|
88
|
+
# links. The ``keep-filters`` parameters is given is the users adds/removes
|
|
89
|
+
# products. In this way we keeps the current filters when we needed to. If
|
|
90
|
+
# the whole page is reloaded there is no ``keep-filters`` or ``page`` and
|
|
91
|
+
# all filters are reset as they should.
|
|
92
|
+
|
|
93
|
+
if get_parameter(request, "keep-filters", with_session=False) or page:
|
|
94
|
+
filter_ = get_parameter(request, "filter")
|
|
95
|
+
category_filter = get_parameter(request, "samples_category_filter")
|
|
96
|
+
else:
|
|
97
|
+
filter_ = get_parameter(request, "filter", with_session=False)
|
|
98
|
+
category_filter = get_parameter(request, "samples_category_filter", with_session=False)
|
|
99
|
+
|
|
100
|
+
# The current filters are saved in any case for later use.
|
|
101
|
+
s["samples_page"] = page
|
|
102
|
+
s["filter"] = filter_
|
|
103
|
+
s["samples_category_filter"] = category_filter
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
s["samples-amount"] = int(get_parameter(request, "samples-amount"))
|
|
107
|
+
except TypeError:
|
|
108
|
+
s["samples-amount"] = 25
|
|
109
|
+
|
|
110
|
+
filters = Q()
|
|
111
|
+
if filter_:
|
|
112
|
+
filters &= Q(name__icontains=filter_) | Q(sku__icontains=filter_)
|
|
113
|
+
filters |= Q(sub_type=VARIANT) & Q(active_sku=False) & Q(parent__sku__icontains=filter_)
|
|
114
|
+
filters |= Q(sub_type=VARIANT) & Q(active_name=False) & Q(parent__name__icontains=filter_)
|
|
115
|
+
|
|
116
|
+
if category_filter:
|
|
117
|
+
if category_filter == "None":
|
|
118
|
+
filters &= Q(categories=None)
|
|
119
|
+
elif category_filter == "All":
|
|
120
|
+
pass
|
|
121
|
+
else:
|
|
122
|
+
# First we collect all sub categories and using the `in` operator
|
|
123
|
+
category = lfs_get_object_or_404(Category, pk=category_filter)
|
|
124
|
+
categories = [category]
|
|
125
|
+
categories.extend(category.get_all_children())
|
|
126
|
+
filters &= Q(categories__in=categories)
|
|
127
|
+
|
|
128
|
+
products = (
|
|
129
|
+
Product.objects.filter(filters)
|
|
130
|
+
.exclude(pk__in=samples_ids)
|
|
131
|
+
.exclude(pk=product.pk)
|
|
132
|
+
.exclude(is_sample__isnull=True)
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
paginator = Paginator(products, s["samples-amount"])
|
|
136
|
+
|
|
137
|
+
total = len(products)
|
|
138
|
+
|
|
139
|
+
try:
|
|
140
|
+
page = paginator.page(page)
|
|
141
|
+
except (EmptyPage, PageNotAnInteger):
|
|
142
|
+
page = 0
|
|
143
|
+
|
|
144
|
+
result = render_to_string(
|
|
145
|
+
template_name,
|
|
146
|
+
request=request,
|
|
147
|
+
context={
|
|
148
|
+
"product": product,
|
|
149
|
+
"samples": samples,
|
|
150
|
+
"total": total,
|
|
151
|
+
"page": page,
|
|
152
|
+
"paginator": paginator,
|
|
153
|
+
"filter": filter_,
|
|
154
|
+
},
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
if as_string:
|
|
158
|
+
return result
|
|
159
|
+
else:
|
|
160
|
+
return HttpResponse(
|
|
161
|
+
json.dumps(
|
|
162
|
+
{
|
|
163
|
+
"html": [["#samples-inline", result]],
|
|
164
|
+
}
|
|
165
|
+
),
|
|
166
|
+
content_type="application/json",
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
# Actions
|
|
171
|
+
@permission_required("core.manage_shop")
|
|
172
|
+
def load_tab(request, product_id):
|
|
173
|
+
""" """
|
|
174
|
+
samples = manage_samples(request, product_id)
|
|
175
|
+
return HttpResponse(samples)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@permission_required("core.manage_shop")
|
|
179
|
+
def add_samples(request, product_id):
|
|
180
|
+
"""Adds passed samples (by request body) to product with passed id."""
|
|
181
|
+
parent_product = Product.objects.get(pk=product_id)
|
|
182
|
+
|
|
183
|
+
for temp_id in request.POST.keys():
|
|
184
|
+
if temp_id.startswith("product") is False:
|
|
185
|
+
continue
|
|
186
|
+
|
|
187
|
+
temp_id = temp_id.split("-")[1]
|
|
188
|
+
add_sample(parent_product, temp_id)
|
|
189
|
+
|
|
190
|
+
# This isn't necessary but it cleans the cache. See lfs.cache listeners
|
|
191
|
+
# for more
|
|
192
|
+
parent_product.save()
|
|
193
|
+
|
|
194
|
+
html = [["#samples-inline", manage_samples_inline(request, product_id, as_string=True)]]
|
|
195
|
+
|
|
196
|
+
result = json.dumps({"html": html, "message": _("Samples have been added.")}, cls=LazyEncoder)
|
|
197
|
+
|
|
198
|
+
return HttpResponse(result, content_type="application/json")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@permission_required("core.manage_shop")
|
|
202
|
+
def remove_samples(request, product_id):
|
|
203
|
+
"""Removes passed samples from product with passed id."""
|
|
204
|
+
parent_product = Product.objects.get(pk=product_id)
|
|
205
|
+
|
|
206
|
+
for temp_id in request.POST.keys():
|
|
207
|
+
if temp_id.startswith("product") is False:
|
|
208
|
+
continue
|
|
209
|
+
|
|
210
|
+
temp_id = temp_id.split("-")[1]
|
|
211
|
+
remove_sample(product=parent_product, sample_id=temp_id)
|
|
212
|
+
|
|
213
|
+
# This isn't necessary but it cleans the cache. See lfs.cache listeners
|
|
214
|
+
# for more
|
|
215
|
+
parent_product.save()
|
|
216
|
+
|
|
217
|
+
html = [["#samples-inline", manage_samples_inline(request, product_id, as_string=True)]]
|
|
218
|
+
|
|
219
|
+
result = json.dumps({"html": html, "message": _("Samples have been removed.")}, cls=LazyEncoder)
|
|
220
|
+
|
|
221
|
+
return HttpResponse(result, content_type="application/json")
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
@permission_required("core.manage_shop")
|
|
225
|
+
def update_samples_state(request, product_id):
|
|
226
|
+
"""Updates samples activity state for variants."""
|
|
227
|
+
product = Product.objects.get(pk=product_id)
|
|
228
|
+
if request.POST.get("samples_activity_state"):
|
|
229
|
+
ActivityState.objects.get_or_create(product=product)
|
|
230
|
+
else:
|
|
231
|
+
try:
|
|
232
|
+
activity_state = ActivityState.objects.get(product=product)
|
|
233
|
+
except ActivityState.DoesNotExist:
|
|
234
|
+
pass
|
|
235
|
+
else:
|
|
236
|
+
activity_state.delete()
|
|
237
|
+
|
|
238
|
+
product.save()
|
|
239
|
+
|
|
240
|
+
html = [["#samples-inline", manage_samples_inline(request, product_id, as_string=True)]]
|
|
241
|
+
|
|
242
|
+
result = json.dumps({"html": html, "message": _("Sample has been updated.")}, cls=LazyEncoder)
|
|
243
|
+
|
|
244
|
+
return HttpResponse(result, content_type="application/json")
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
@permission_required("core.manage_shop")
|
|
248
|
+
def update_is_sample(request, product_id):
|
|
249
|
+
"""Updates is sample state."""
|
|
250
|
+
product = Product.objects.get(pk=product_id)
|
|
251
|
+
if request.POST.get("is_sample"):
|
|
252
|
+
IsSample.objects.get_or_create(product=product)
|
|
253
|
+
else:
|
|
254
|
+
try:
|
|
255
|
+
activity_state = IsSample.objects.get(product=product)
|
|
256
|
+
except IsSample.DoesNotExist:
|
|
257
|
+
pass
|
|
258
|
+
else:
|
|
259
|
+
activity_state.delete()
|
|
260
|
+
|
|
261
|
+
product.save()
|
|
262
|
+
|
|
263
|
+
html = [["#samples", manage_samples(request, product_id)]]
|
|
264
|
+
|
|
265
|
+
result = json.dumps({"html": html, "message": _("Sample has been updated.")}, cls=LazyEncoder)
|
|
266
|
+
|
|
267
|
+
return HttpResponse(result, content_type="application/json")
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class ProductSamplesView(PermissionRequiredMixin, ProductTabMixin, TemplateView):
|
|
271
|
+
"""Samples tab for a Product (Bootstrap/HTMX shell)."""
|
|
272
|
+
|
|
273
|
+
tab_name = "samples"
|
|
274
|
+
permission_required = "core.manage_shop"
|
|
275
|
+
page_sizes = [10, 25, 50, 100]
|
|
276
|
+
|
|
277
|
+
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
|
278
|
+
product = self.get_product()
|
|
279
|
+
action = request.POST.get("action")
|
|
280
|
+
|
|
281
|
+
if action == "update_is_sample":
|
|
282
|
+
if request.POST.get("is_sample"):
|
|
283
|
+
IsSample.objects.get_or_create(product=product)
|
|
284
|
+
else:
|
|
285
|
+
IsSample.objects.filter(product=product).delete()
|
|
286
|
+
product.save()
|
|
287
|
+
self._success(_("Sample has been updated."))
|
|
288
|
+
return self.get(request, *args, **kwargs)
|
|
289
|
+
|
|
290
|
+
if action == "update_samples_state":
|
|
291
|
+
if request.POST.get("samples_activity_state"):
|
|
292
|
+
ActivityState.objects.get_or_create(product=product)
|
|
293
|
+
else:
|
|
294
|
+
ActivityState.objects.filter(product=product).delete()
|
|
295
|
+
product.save()
|
|
296
|
+
self._success(_("Sample has been updated."))
|
|
297
|
+
return self.get(request, *args, **kwargs)
|
|
298
|
+
|
|
299
|
+
if action == "add":
|
|
300
|
+
for key in request.POST.keys():
|
|
301
|
+
if key.startswith("product-"):
|
|
302
|
+
add_sample(product, key.split("-", 1)[1])
|
|
303
|
+
product.save()
|
|
304
|
+
product_changed.send(product)
|
|
305
|
+
self._success(_("Samples have been added."))
|
|
306
|
+
return self.get(request, *args, **kwargs)
|
|
307
|
+
|
|
308
|
+
if action == "remove":
|
|
309
|
+
for key in request.POST.keys():
|
|
310
|
+
if key.startswith("sample-"):
|
|
311
|
+
remove_sample(product=product, sample_id=key.split("-", 1)[1])
|
|
312
|
+
product.save()
|
|
313
|
+
product_changed.send(product)
|
|
314
|
+
self._success(_("Samples have been removed."))
|
|
315
|
+
return self.get(request, *args, **kwargs)
|
|
316
|
+
|
|
317
|
+
return self.get(request, *args, **kwargs)
|
|
318
|
+
|
|
319
|
+
def _success(self, message) -> None:
|
|
320
|
+
# For HTMX requests, don't show success message to avoid page disruption
|
|
321
|
+
if not self.request.headers.get("HX-Request"):
|
|
322
|
+
messages.success(self.request, message)
|
|
323
|
+
|
|
324
|
+
def get_context_data(self, **kwargs):
|
|
325
|
+
ctx = super().get_context_data(**kwargs)
|
|
326
|
+
product = self.get_product()
|
|
327
|
+
|
|
328
|
+
# Support HTMX filter/pagination via GET as well as add/remove via POST
|
|
329
|
+
request_data = (
|
|
330
|
+
self.request.POST
|
|
331
|
+
if self.request.method == "POST" and self.request.POST.get("keep-filters")
|
|
332
|
+
else self.request.GET
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
filter_q = request_data.get("filter", "").strip()
|
|
336
|
+
category_filter = request_data.get("category_filter", "").strip()
|
|
337
|
+
try:
|
|
338
|
+
amount = int(request_data.get("amount", 25))
|
|
339
|
+
except (TypeError, ValueError):
|
|
340
|
+
amount = 25
|
|
341
|
+
page_num = request_data.get("page", 1)
|
|
342
|
+
|
|
343
|
+
# Currently assigned samples
|
|
344
|
+
samples = [psr.sample for psr in ProductSamplesRelation.objects.filter(product=product).select_related("sample")]
|
|
345
|
+
sample_ids = [sample.id for sample in samples]
|
|
346
|
+
|
|
347
|
+
# Selectable products: only products which are flagged as sample themselves
|
|
348
|
+
filters = Q()
|
|
349
|
+
if filter_q:
|
|
350
|
+
filters &= Q(name__icontains=filter_q) | Q(sku__icontains=filter_q)
|
|
351
|
+
|
|
352
|
+
if category_filter and category_filter not in ("All", "None"):
|
|
353
|
+
category = lfs_get_object_or_404(Category, pk=category_filter)
|
|
354
|
+
categories = [category]
|
|
355
|
+
categories.extend(category.get_all_children())
|
|
356
|
+
filters &= Q(categories__in=categories)
|
|
357
|
+
elif category_filter == "None":
|
|
358
|
+
filters &= Q(categories=None)
|
|
359
|
+
|
|
360
|
+
available_qs = (
|
|
361
|
+
Product.objects.filter(filters)
|
|
362
|
+
.exclude(is_sample__isnull=True)
|
|
363
|
+
.exclude(pk=product.pk)
|
|
364
|
+
.exclude(pk__in=sample_ids)
|
|
365
|
+
.distinct()
|
|
366
|
+
.order_by("name")
|
|
367
|
+
)
|
|
368
|
+
paginator = Paginator(available_qs, amount)
|
|
369
|
+
try:
|
|
370
|
+
available_page = paginator.page(page_num)
|
|
371
|
+
except (EmptyPage, PageNotAnInteger):
|
|
372
|
+
available_page = paginator.page(1)
|
|
373
|
+
|
|
374
|
+
ctx.update(
|
|
375
|
+
{
|
|
376
|
+
"samples": samples,
|
|
377
|
+
"available_page": available_page,
|
|
378
|
+
"available_paginator": paginator,
|
|
379
|
+
"filter": filter_q,
|
|
380
|
+
"category_filter": category_filter,
|
|
381
|
+
"categories": Category.objects.all(),
|
|
382
|
+
"amount": amount,
|
|
383
|
+
"page_sizes": self.page_sizes,
|
|
384
|
+
"is_sample": is_sample(product),
|
|
385
|
+
"has_active_samples": has_active_samples(product),
|
|
386
|
+
}
|
|
387
|
+
)
|
|
388
|
+
return ctx
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lfs-samples
|
|
3
|
+
Version: 1.1
|
|
4
|
+
Summary: Sample management for LFS
|
|
5
|
+
Home-page: https://github.com/diefenbach/lfs-samples
|
|
6
|
+
Author: Kai Diefenbach
|
|
7
|
+
Author-email: kai.diefenbach@iqpp.de
|
|
8
|
+
License: BSD-3-Clause
|
|
9
|
+
Keywords: django e-commerce online-shop
|
|
10
|
+
Classifier: Environment :: Web Environment
|
|
11
|
+
Classifier: Framework :: Django
|
|
12
|
+
Classifier: Framework :: Django :: 5.2
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE.txt
|
|
19
|
+
Requires-Dist: django-lfs<2,>=1.0
|
|
20
|
+
Dynamic: author
|
|
21
|
+
Dynamic: author-email
|
|
22
|
+
Dynamic: classifier
|
|
23
|
+
Dynamic: description
|
|
24
|
+
Dynamic: description-content-type
|
|
25
|
+
Dynamic: home-page
|
|
26
|
+
Dynamic: keywords
|
|
27
|
+
Dynamic: license
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
Dynamic: requires-dist
|
|
30
|
+
Dynamic: requires-python
|
|
31
|
+
Dynamic: summary
|
|
32
|
+
|
|
33
|
+
# lfs-samples
|
|
34
|
+
|
|
35
|
+
Product samples for [django-lfs](https://pypi.org/project/django-lfs/) (LFS).
|
|
36
|
+
|
|
37
|
+
A shop manager marks products as samples and assigns them to other products. A customer can then order the sample
|
|
38
|
+
of a product from its product page.
|
|
39
|
+
|
|
40
|
+
## What it does
|
|
41
|
+
|
|
42
|
+
- Adds a **Samples** tab to every product in the LFS management interface.
|
|
43
|
+
- Stores in its own tables:
|
|
44
|
+
- whether a product *is a sample*,
|
|
45
|
+
- which samples belong to a product,
|
|
46
|
+
- for variants: whether the variant has *its own samples* or uses those of its parent product.
|
|
47
|
+
- Provides template tags to show a product's samples in the shop.
|
|
48
|
+
|
|
49
|
+
A sample is an ordinary LFS product. When a customer orders it, it goes into the cart like any other product, with
|
|
50
|
+
its own price, stock and delivery settings. To give samples away for free, give the sample products a price of 0.
|
|
51
|
+
|
|
52
|
+
## Requirements
|
|
53
|
+
|
|
54
|
+
- django-lfs >= 1.0, < 2 (which requires Django >= 5.2, < 6)
|
|
55
|
+
- Python >= 3.12
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
1. Install the package:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
pip install lfs-samples
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
2. Add it to `INSTALLED_APPS`, after the LFS apps:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
INSTALLED_APPS = [
|
|
69
|
+
# ... the LFS apps ...
|
|
70
|
+
"lfs_samples",
|
|
71
|
+
]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
3. Include its URLs in your project's `urls.py`. **This step is required:** the app registers the Samples tab as
|
|
75
|
+
soon as it is installed, and without these URLs every product page in the management interface fails with
|
|
76
|
+
`NoReverseMatch` for `lfs_manage_product_samples`.
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from django.urls import include, path
|
|
80
|
+
|
|
81
|
+
urlpatterns = [
|
|
82
|
+
# ...
|
|
83
|
+
path("manage/", include("lfs.manage.urls")),
|
|
84
|
+
path("manage/samples/", include("lfs_samples.urls")),
|
|
85
|
+
]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The templates build all links with `{% url %}`, so any prefix works. `manage/samples/` is recommended: all
|
|
89
|
+
views of the app belong to the management interface (they require the `core.manage_shop` permission), so they
|
|
90
|
+
share its `manage/` prefix.
|
|
91
|
+
|
|
92
|
+
4. Create the tables:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
python manage.py migrate lfs_samples
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Usage in the management interface
|
|
99
|
+
|
|
100
|
+
Open a product in the management interface and select the **Samples** tab.
|
|
101
|
+
|
|
102
|
+
- **Is sample**: marks the product as a sample. Only products marked this way can be assigned to other products.
|
|
103
|
+
A sample has no samples of its own, so the rest of the tab is hidden for it.
|
|
104
|
+
- **Active samples** (variants only): when checked, the variant uses the samples assigned to it. When unchecked,
|
|
105
|
+
the variant uses the samples of its parent product.
|
|
106
|
+
- **Selectable Samples / Selected Samples**: the left list shows the products marked as samples that are not yet
|
|
107
|
+
assigned (searchable by name or SKU, filterable by category); select some and add them to the product. The right
|
|
108
|
+
list shows the samples assigned to the product; select some and remove them.
|
|
109
|
+
|
|
110
|
+
## Showing samples in the shop
|
|
111
|
+
|
|
112
|
+
Load the template tags with `{% load lfs_samples_tags %}`. They read `product` from the template context:
|
|
113
|
+
|
|
114
|
+
- `{% product_samples %}` sets `product_samples` to the list of the product's samples. For a variant without
|
|
115
|
+
active samples it returns the samples of the parent product.
|
|
116
|
+
- `{% is_sample %}` sets `is_sample` to whether the product is a sample.
|
|
117
|
+
- `{% ifproducthassamples %} ... {% else %} ... {% endifproducthassamples %}` renders its first part when samples
|
|
118
|
+
are assigned to the product itself (it does not fall back to the parent product).
|
|
119
|
+
|
|
120
|
+
LFS's product form accepts a sample directly: submitted with `action=add-sample-to-cart` and the sample's id as
|
|
121
|
+
`sample_id`, it adds the sample to the cart. Inside the product form, for example:
|
|
122
|
+
|
|
123
|
+
```django
|
|
124
|
+
{% load i18n lfs_samples_tags %}
|
|
125
|
+
{% product_samples %}
|
|
126
|
+
{% with sample=product_samples.0 %}
|
|
127
|
+
{% if sample %}
|
|
128
|
+
<input type="hidden" name="sample_id" value="{{ sample.id }}">
|
|
129
|
+
<button type="submit" name="action" value="add-sample-to-cart">{% trans "Order a free sample" %}</button>
|
|
130
|
+
{% endif %}
|
|
131
|
+
{% endwith %}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
lfs-theme already shows this button on the product page when `lfs_samples` is installed.
|
|
135
|
+
|
|
136
|
+
The app comes with a German translation.
|
|
137
|
+
|
|
138
|
+
## Development
|
|
139
|
+
|
|
140
|
+
The tests use pytest with pytest-django and an in-memory SQLite database (see `pytest.ini` and
|
|
141
|
+
`lfs_samples/tests/settings.py`). In an environment with django-lfs, pytest and pytest-django installed:
|
|
142
|
+
|
|
143
|
+
```sh
|
|
144
|
+
pip install -e .
|
|
145
|
+
python -m pytest
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
BSD 3-Clause License, see `LICENSE.txt`.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
lfs_samples/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
lfs_samples/admin.py,sha256=v9lW8eU-abGnCAfRh1sQMC5gKRs9Qy6b1xgju0LmqVM,212
|
|
3
|
+
lfs_samples/apps.py,sha256=3yGhRJSNWRFCS3jlSRjIQdScsneHwoDcSSIOlsSyp14,731
|
|
4
|
+
lfs_samples/models.py,sha256=umL2AhcMa2cP72IBjKY7lBrgh7jowq0LmOj2PvxNWmo,1179
|
|
5
|
+
lfs_samples/urls.py,sha256=s5P2IMPym9pVfPWgrBxo8egqMimZASe6eOwnH1pk98s,985
|
|
6
|
+
lfs_samples/utils.py,sha256=oq0QRsGgfqmGWNBk43LfU-QevW4iMpSvbLLgyu6xQ50,1393
|
|
7
|
+
lfs_samples/views.py,sha256=Ra7JGJSqNp271LWAd8mCAEbpv-iYXKDnvmZ31o6kL6Q,13800
|
|
8
|
+
lfs_samples/locale/de/LC_MESSAGES/django.mo,sha256=53kTwsfcswGBwuJKJA1LqhYntVmDbsYtQXSTdVGJ4v0,1077
|
|
9
|
+
lfs_samples/locale/de/LC_MESSAGES/django.po,sha256=2QoKVx7DpJaKueASI5v8VPlz0JsXHIJZlCuDJ_S9n7E,1749
|
|
10
|
+
lfs_samples/migrations/0001_initial.py,sha256=2cF5WxCwo5DWRDxYoKKBPppCCLrYTO90HOmlrMpwMh0,1613
|
|
11
|
+
lfs_samples/migrations/0002_issample.py,sha256=xyqIoP5Zpo9cARdks3YiyhzFDicSask0x8TrAaFOaKY,715
|
|
12
|
+
lfs_samples/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
lfs_samples/templates/lfs_samples/samples.html,sha256=HjYFfEW_zvtKng1M5-TfQzCgi_Dw_ZgEG58x8qRXERc,2002
|
|
14
|
+
lfs_samples/templates/lfs_samples/samples_inline.html,sha256=MZkb7U5a2apQPHar4qnfU8KCAZqS6YLOyOfD6dGWZGY,8321
|
|
15
|
+
lfs_samples/templates/manage/products/tabs/_assigned_samples.html,sha256=iF5AL45Ox846Edv5f_4WBnP3X9q4UJPdaz1jy0D1lSQ,4371
|
|
16
|
+
lfs_samples/templates/manage/products/tabs/_available_samples.html,sha256=_k_n8fn5hYGgzlv9_SfZyesIE0Gcs67TF4HhNpJhX74,4448
|
|
17
|
+
lfs_samples/templates/manage/products/tabs/_samples.html,sha256=ihJHe4sMP7sEPfzoiHVrBvBegsRkEsMLM-55znXf1Xo,5083
|
|
18
|
+
lfs_samples/templatetags/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
|
+
lfs_samples/templatetags/lfs_samples_tags.py,sha256=IHy0dBdVhXdaC0JsadtZYb39L6PBOUc307Xb_6x-KUQ,1895
|
|
20
|
+
lfs_samples/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
|
+
lfs_samples/tests/settings.py,sha256=GMhvU9sog4EkEWQ6F7esWECPhcv2Rq6n5Mpgm0l4HtU,1713
|
|
22
|
+
lfs_samples/tests/test_manage_tab.py,sha256=BBL9tN1bu3HQFFxb9DQHFqV21F4rVK_wYctv1eQv8pk,996
|
|
23
|
+
lfs_samples/tests/urls.py,sha256=ZW6bTB_FM0WIdpv02v4j5hglMGYKk57VwppN8o1Avvo,285
|
|
24
|
+
lfs_samples-1.1.dist-info/licenses/LICENSE.txt,sha256=DZUu07fRnxSPORIOUqHVCbS6ZrelyNzz4IdMf42zUDs,1523
|
|
25
|
+
lfs_samples-1.1.dist-info/METADATA,sha256=ClSSnAHFOQzAMDNkI-TAAy_12jXW47c11qZKd2Ks7yY,5236
|
|
26
|
+
lfs_samples-1.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
27
|
+
lfs_samples-1.1.dist-info/top_level.txt,sha256=wV4_YtITtswF5iDP8lZwU91g07d_3y0Qs7h5_Lv9Jr8,12
|
|
28
|
+
lfs_samples-1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Copyright (c) 2015, Kai Diefenbach - IQ++
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
|
8
|
+
this list of conditions and the following disclaimer.
|
|
9
|
+
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
documentation and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
* Neither the name of the author nor the names of other contributors may
|
|
15
|
+
be used to endorse or promote products derived from this software
|
|
16
|
+
without specific prior written permission.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lfs_samples
|