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.
@@ -0,0 +1,103 @@
1
+ {% load i18n static %}
2
+
3
+ <div class="tab-pane fade show active" id="samples-content" role="tabpanel" aria-labelledby="samples-tab">
4
+ <div class="d-flex align-items-center gap-3 mb-4 flex-wrap">
5
+ <!-- Is sample toggle -->
6
+ <form method="post"
7
+ class="d-flex align-items-center gap-2 mb-0"
8
+ hx-post="{% url 'lfs_manage_product_samples' product.id %}"
9
+ hx-target="#samples-content"
10
+ hx-select="#samples-content"
11
+ hx-swap="outerHTML">
12
+ {% csrf_token %}
13
+ <input type="hidden" name="action" value="update_is_sample">
14
+ <div class="form-check mb-0">
15
+ <input class="form-check-input" type="checkbox" id="isSample" name="is_sample" {% if is_sample %}checked{% endif %}>
16
+ <label class="form-check-label" for="isSample">{% trans "Is sample" %}</label>
17
+ </div>
18
+ <button type="submit" class="btn btn-sm btn-outline-primary">
19
+ <i class="bi bi-check-lg me-1"></i> {% trans 'Save' %}
20
+ </button>
21
+ </form>
22
+
23
+ {% if not is_sample and product.is_variant %}
24
+ <!-- Active samples toggle (variants only) -->
25
+ <form method="post"
26
+ class="d-flex align-items-center gap-2 mb-0"
27
+ hx-post="{% url 'lfs_manage_product_samples' product.id %}"
28
+ hx-target="#samples-content"
29
+ hx-select="#samples-content"
30
+ hx-swap="outerHTML">
31
+ {% csrf_token %}
32
+ <input type="hidden" name="action" value="update_samples_state">
33
+ <div class="form-check mb-0">
34
+ <input class="form-check-input" type="checkbox" id="activeSamples" name="samples_activity_state" {% if has_active_samples %}checked{% endif %}>
35
+ <label class="form-check-label" for="activeSamples">{% trans "Active samples" %}</label>
36
+ </div>
37
+ <button type="submit" class="btn btn-sm btn-outline-primary">
38
+ <i class="bi bi-check-lg me-1"></i> {% trans 'Save' %}
39
+ </button>
40
+ </form>
41
+ {% endif %}
42
+ </div>
43
+
44
+ {% if not is_sample %}
45
+ <div class="d-flex align-items-center gap-2 mb-4">
46
+ <!-- Filter Controls -->
47
+ <div class="input-group">
48
+ <span class="input-group-text bg-white"><i class="bi bi-search"></i></span>
49
+ <input type="text"
50
+ class="form-control"
51
+ placeholder="{% trans 'Search products...' %}"
52
+ name="filter"
53
+ value="{{ filter }}"
54
+ hx-get="{% url 'lfs_manage_product_samples' product.id %}"
55
+ hx-target="#samples-tables"
56
+ hx-select="#samples-tables"
57
+ hx-trigger="input changed delay:300ms, keyup[key=='Escape'] delay:100ms"
58
+ hx-include="[name='amount'],[name='category_filter']"
59
+ hx-push-url="true">
60
+ </div>
61
+
62
+ <select name="category_filter"
63
+ class="form-select"
64
+ style="width: 220px;"
65
+ hx-get="{% url 'lfs_manage_product_samples' product.id %}"
66
+ hx-target="#samples-tables"
67
+ hx-select="#samples-tables"
68
+ hx-include="[name='filter'],[name='amount']"
69
+ hx-push-url="true">
70
+ <option value="" {% if not category_filter %}selected{% endif %}>{% trans "All categories" %}</option>
71
+ <option value="None" {% if category_filter == "None" %}selected{% endif %}>{% trans "Without category" %}</option>
72
+ {% for category in categories %}
73
+ <option value="{{ category.id }}" {% if category_filter == category.id|stringformat:"s" %}selected{% endif %}>{{ category.name }}</option>
74
+ {% endfor %}
75
+ </select>
76
+
77
+ <select name="amount"
78
+ class="form-select"
79
+ style="width: 120px;"
80
+ hx-get="{% url 'lfs_manage_product_samples' product.id %}"
81
+ hx-target="#samples-tables"
82
+ hx-select="#samples-tables"
83
+ hx-include="[name='filter'],[name='category_filter']"
84
+ hx-push-url="true">
85
+ {% for n in page_sizes %}
86
+ <option value="{{ n }}" {% if amount == n %}selected{% endif %}>{{ n }}</option>
87
+ {% endfor %}
88
+ </select>
89
+ </div>
90
+
91
+ <div id="samples-tables" class="row g-4">
92
+ <!-- Selectable Samples (Left Column) -->
93
+ <div class="col-md-6">
94
+ {% include 'manage/products/tabs/_available_samples.html' %}
95
+ </div>
96
+
97
+ <!-- Assigned Samples (Right Column) -->
98
+ <div class="col-md-6">
99
+ {% include 'manage/products/tabs/_assigned_samples.html' %}
100
+ </div>
101
+ </div>
102
+ {% endif %}
103
+ </div>
File without changes
@@ -0,0 +1,68 @@
1
+ from django import template
2
+ from django.template import Library
3
+
4
+ from ..utils import get_samples
5
+ from ..utils import has_samples
6
+ from ..utils import is_sample as is_sample_util
7
+
8
+ register = Library()
9
+
10
+
11
+ class IfProductHasSamples(template.Node):
12
+ @classmethod
13
+ def handle_token(cls, parser, token):
14
+ bits = token.contents.split()
15
+ if len(bits) != 1:
16
+ raise template.TemplateSyntaxError("'%s' tag takes one argument" % bits[0])
17
+ end_tag = "endifproducthassamples"
18
+ nodelist_true = parser.parse(("else", end_tag))
19
+ token = parser.next_token()
20
+ if token.contents == "else": # there is an 'else' clause in the tag
21
+ nodelist_false = parser.parse((end_tag,))
22
+ parser.delete_first_token()
23
+ else:
24
+ nodelist_false = ""
25
+
26
+ return cls(bits[0], nodelist_true, nodelist_false)
27
+
28
+ def __init__(self, codename, nodelist_true, nodelist_false):
29
+ self.codename = codename
30
+ self.nodelist_true = nodelist_true
31
+ self.nodelist_false = nodelist_false
32
+
33
+ def render(self, context):
34
+ product = context.get("product")
35
+
36
+ if has_samples(product):
37
+ return self.nodelist_true.render(context)
38
+
39
+ return self.nodelist_false
40
+
41
+
42
+ @register.tag
43
+ def ifproducthassamples(parser, token):
44
+ return IfProductHasSamples.handle_token(parser, token)
45
+
46
+
47
+ class IsSampleNode(template.Node):
48
+ def render(self, context):
49
+ product = context["product"]
50
+ context["is_sample"] = is_sample_util(product)
51
+ return ""
52
+
53
+
54
+ @register.tag
55
+ def is_sample(parser, token):
56
+ return IsSampleNode()
57
+
58
+
59
+ class ProductSamplesNode(template.Node):
60
+ def render(self, context):
61
+ product = context["product"]
62
+ context["product_samples"] = get_samples(product=product)
63
+ return ""
64
+
65
+
66
+ @register.tag
67
+ def product_samples(parser, token):
68
+ return ProductSamplesNode()
File without changes
@@ -0,0 +1,44 @@
1
+ """Settings for the add-on's tests: LFS with its required apps, plus lfs_samples."""
2
+
3
+ from lfs.settings import LFS_APPS
4
+
5
+ SECRET_KEY = "test-only"
6
+ ALLOWED_HOSTS = ["testserver"]
7
+ SITE_ID = 1
8
+ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
9
+ DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}
10
+ CACHES = {"default": {"BACKEND": "django.core.cache.backends.dummy.DummyCache"}}
11
+ INSTALLED_APPS = [*LFS_APPS, "lfs_samples"]
12
+ MIDDLEWARE = [
13
+ "django.contrib.sessions.middleware.SessionMiddleware",
14
+ "django.middleware.common.CommonMiddleware",
15
+ "django.middleware.csrf.CsrfViewMiddleware",
16
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
17
+ "django.contrib.messages.middleware.MessageMiddleware",
18
+ ]
19
+ ROOT_URLCONF = "lfs_samples.tests.urls"
20
+ STATIC_URL = "static/"
21
+ STATIC_ROOT = "/tmp/lfs-samples-tests/static"
22
+ MEDIA_ROOT = "/tmp/lfs-samples-tests/media"
23
+ STATICFILES_FINDERS = ["django.contrib.staticfiles.finders.AppDirectoriesFinder", "compressor.finders.CompressorFinder"]
24
+ COMPRESS_ENABLED = False
25
+ TEMPLATES = [
26
+ {
27
+ "BACKEND": "django.template.backends.django.DjangoTemplates",
28
+ "APP_DIRS": True,
29
+ "OPTIONS": {
30
+ "context_processors": [
31
+ "django.template.context_processors.request",
32
+ "django.contrib.auth.context_processors.auth",
33
+ "django.contrib.messages.context_processors.messages",
34
+ "lfs.core.context_processors.main",
35
+ ]
36
+ },
37
+ }
38
+ ]
39
+ USE_TZ = True
40
+ LANGUAGE_CODE = "en"
41
+ SESSION_SERIALIZER = "lfs.core.pickle_serializer.PickleSerializer"
42
+ CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
43
+ CRISPY_TEMPLATE_PACK = "bootstrap5"
44
+ REVIEWS_IS_MODERATED = False
@@ -0,0 +1,26 @@
1
+ """The add-on registers its "Samples" tab on the product management page (django-lfs no longer lists it itself)."""
2
+
3
+ import pytest
4
+ from django.contrib.auth import get_user_model
5
+ from django.core.management import call_command
6
+ from django.urls import reverse
7
+
8
+ from lfs.catalog.models import Product
9
+
10
+
11
+ @pytest.fixture
12
+ def admin_client(client, db):
13
+ call_command("lfs_init")
14
+ client.force_login(get_user_model().objects.create_superuser("admin", "admin@example.com", "x"))
15
+ return client
16
+
17
+
18
+ def test_samples_tab_is_listed_and_shows_its_content(admin_client):
19
+ product = Product.objects.create(name="Board", slug="board", sku="B1", price=10, active=True)
20
+ samples_url = reverse("lfs_manage_product_samples", args=[product.pk])
21
+
22
+ data_page = admin_client.get(reverse("lfs_manage_product_data", args=[product.pk])).content.decode()
23
+ samples_page = admin_client.get(samples_url).content.decode()
24
+
25
+ assert samples_url in data_page
26
+ assert 'id="samples-content"' in samples_page
@@ -0,0 +1,9 @@
1
+ """URLs for the add-on's tests: the shop, the management interface, and the add-on."""
2
+
3
+ from django.urls import include, path
4
+
5
+ urlpatterns = [
6
+ path("", include("lfs.core.urls")),
7
+ path("manage/", include("lfs.manage.urls")),
8
+ path("samples/", include("lfs_samples.urls")),
9
+ ]
lfs_samples/urls.py ADDED
@@ -0,0 +1,17 @@
1
+ from django.urls import path, re_path
2
+ from . import views
3
+
4
+ urlpatterns = [
5
+ path("product/<int:id>/samples/", views.ProductSamplesView.as_view(), name="lfs_manage_product_samples"),
6
+ re_path(r"^samples-tab/(?P<product_id>\d*)$", views.load_tab, name="lfs_manage_samples_tab"),
7
+ re_path(r"^samples/(?P<product_id>\d*)$", views.manage_samples, name="lfs_manage_samples"),
8
+ re_path(r"^samples-inline/(?P<product_id>\d*)$", views.manage_samples_inline, name="lfs_manage_samples_inline"),
9
+ re_path(r"^add-samples/(?P<product_id>\d*)$", views.add_samples, name="lfs_manage_add_samples"),
10
+ re_path(r"^remove-samples/(?P<product_id>\d*)$", views.remove_samples, name="lfs_manage_remove_samples"),
11
+ re_path(
12
+ r"^update-samples-state/(?P<product_id>\d*)$",
13
+ views.update_samples_state,
14
+ name="lfs_manage_update_samples_state",
15
+ ),
16
+ re_path(r"^update-is-samples/(?P<product_id>\d*)$", views.update_is_sample, name="lfs_manage_update_is_sample"),
17
+ ]
lfs_samples/utils.py ADDED
@@ -0,0 +1,49 @@
1
+ from .models import ProductSamplesRelation
2
+ from .models import ActivityState
3
+ from .models import IsSample
4
+
5
+
6
+ def get_samples(product):
7
+ samples = []
8
+ if product.is_variant() and has_active_samples(product) is False:
9
+ product = product.parent
10
+
11
+ for product_samples_relation in ProductSamplesRelation.objects.filter(product=product):
12
+ samples.append(product_samples_relation.sample)
13
+ return samples
14
+
15
+
16
+ def has_samples(product):
17
+ return ProductSamplesRelation.objects.filter(product=product).exists()
18
+
19
+
20
+ def add_sample(product, sample_id):
21
+ ProductSamplesRelation.objects.create(product=product, sample_id=sample_id)
22
+
23
+
24
+ def remove_sample(product, sample_id):
25
+ ProductSamplesRelation.objects.filter(product=product, sample_id=sample_id).delete()
26
+
27
+
28
+ def has_active_samples(product_or_id):
29
+ try:
30
+ if isinstance(product_or_id, int):
31
+ ActivityState.objects.get(product_id=product_or_id)
32
+ else:
33
+ ActivityState.objects.get(product=product_or_id)
34
+ except ActivityState.DoesNotExist:
35
+ return False
36
+ else:
37
+ return True
38
+
39
+
40
+ def is_sample(product_or_id):
41
+ try:
42
+ if isinstance(product_or_id, int):
43
+ IsSample.objects.get(product_id=product_or_id)
44
+ else:
45
+ IsSample.objects.get(product=product_or_id)
46
+ except IsSample.DoesNotExist:
47
+ return False
48
+ else:
49
+ return True