From e69cb4ea50c42ee726b7c11f71eec5f7b4d8fd7e Mon Sep 17 00:00:00 2001 From: erjemin Date: Sun, 15 Nov 2026 18:06:18 +0300 Subject: [PATCH] =?UTF-8?q?add:=20=D0=BF=D1=80=D0=BE=D0=BC=D0=BE-=D0=BF?= =?UTF-8?q?=D0=BB=D0=B0=D1=88=D0=BA=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=81=20=D1=80=D0=B5=D0=B4=D0=B8=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D0=BC=20=D0=B8=20=D0=BF=D0=BE=D0=B4=D1=81?= =?UTF-8?q?=D1=87=D0=B5=D1=82=D0=BE=D0=BC=20=D0=BA=D0=BB=D0=B8=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hypn0/hypn0_site/tests.py | 66 +++++++++++++++++++++++++++++ hypn0/hypn0_site/urls.py | 2 + hypn0/hypn0_site/views.py | 15 ++++++- hypn0/templates/gallery/detail.html | 38 +++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) diff --git a/hypn0/hypn0_site/tests.py b/hypn0/hypn0_site/tests.py index 4630d50..1da7949 100644 --- a/hypn0/hypn0_site/tests.py +++ b/hypn0/hypn0_site/tests.py @@ -1806,3 +1806,69 @@ class PromoVirtualFieldsTests(BaseMediaTestCase): self.assertEqual(saved_item.promo_desc, "") self.assertEqual(saved_item.promo_cta, "") self.assertEqual(saved_item.promo_disclaimer, "") + + def test_gallery_promo_redirect_increments_clicks_and_redirects(self): + item = TbHypn0Item( + s_title="Promo Redirect Test", + file_svg=ContentFile(self.svg_bytes, name="promo_redirect.svg"), + s_promo_url="https://hypno.example.org/partner", + s_promo_title="Partner Link", + i_promo_clicks=5, + is_public=True, + ) + item.save(visitor_uuid_or_fp=self.vid) + + # Вызываем redirect эндпоинт + url = reverse("hypn0_site:gallery_promo_redirect", kwargs={"hash_id": item.s_hash_id}) + response = self.client.get(url) + + self.assertEqual(response.status_code, 302) + self.assertEqual(response["Location"], "https://hypno.example.org/partner") + + # Проверяем инкремент счетчика кликов + item.refresh_from_db() + self.assertEqual(item.i_promo_clicks, 6) + + def test_gallery_promo_redirect_404_if_no_url(self): + item = TbHypn0Item( + s_title="No Promo Url Test", + file_svg=ContentFile(self.svg_bytes, name="no_promo.svg"), + s_promo_url="", + is_public=True, + ) + item.save(visitor_uuid_or_fp=self.vid) + + url = reverse("hypn0_site:gallery_promo_redirect", kwargs={"hash_id": item.s_hash_id}) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + + def test_gallery_detail_renders_emerald_promo_bar(self): + item = TbHypn0Item( + s_title="Promo Visual Test", + file_svg=ContentFile(self.svg_bytes, name="promo_vis.svg"), + s_promo_url="https://sponsor.domain.com", + s_promo_title="CyberToad Labs", + j_metadata={ + "color": "#a855ff", + "promo": { + "desc": "Official hypno sponsor for neural protection.", + "cta": "Get Shield", + "disclaimer": "Реклама. ООО «Слизни», ИНН 7700000000, erid: 2VtzquX", + }, + }, + is_public=True, + ) + item.save(visitor_uuid_or_fp=self.vid) + + url = reverse("hypn0_site:gallery_detail", kwargs={"hash_id": item.s_hash_id}) + response = self.client.get(url) + + self.assertEqual(response.status_code, 200) + content = response.content.decode("utf-8") + + # Проверяем наличие плашки и атрибутов + self.assertIn("ADV", content) + self.assertIn("Official hypno sponsor for neural protection.", content) + self.assertIn("Get Shield", content) + self.assertIn("Реклама. ООО «Слизни», ИНН 7700000000, erid: 2VtzquX", content) + self.assertIn(f"/gallery/{item.s_hash_id}/promo", content) diff --git a/hypn0/hypn0_site/urls.py b/hypn0/hypn0_site/urls.py index 7d4000f..9a07ffa 100644 --- a/hypn0/hypn0_site/urls.py +++ b/hypn0/hypn0_site/urls.py @@ -14,6 +14,8 @@ urlpatterns = [ path("gallery/", views.gallery_detail, name="gallery_detail"), path("gallery//download", views.gallery_download, name="gallery_download"), path("gallery//vote", views.gallery_vote, name="gallery_vote"), + path("gallery//promo", views.gallery_promo_redirect, name="gallery_promo_redirect"), + path("go/", views.gallery_promo_redirect, name="promo_redirect"), path("blog", views.blog_feed, name="blog_feed"), path("blog/", views.blog_detail, name="blog_detail"), ] diff --git a/hypn0/hypn0_site/views.py b/hypn0/hypn0_site/views.py index c1caac0..2501f6b 100644 --- a/hypn0/hypn0_site/views.py +++ b/hypn0/hypn0_site/views.py @@ -4,7 +4,8 @@ import random from django.conf import settings from django.core.files.base import ContentFile from django.core.paginator import Paginator -from django.http import Http404, HttpRequest, HttpResponse, JsonResponse +from django.db.models import F +from django.http import Http404, HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse from django.shortcuts import get_object_or_404, redirect, render from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.http import require_POST @@ -690,6 +691,18 @@ def gallery_vote(request: HttpRequest, hash_id: str) -> HttpResponse: ) +def gallery_promo_redirect(request: HttpRequest, hash_id: str) -> HttpResponse: + """ + Эндпоинт для перехода по промо-ссылке картины с атомарным подсчетом переходов. + """ + item = get_object_or_404(TbHypn0Item, s_hash_id=hash_id, is_public=True) + if not item.s_promo_url: + raise Http404("Промо-ссылка для данной картины не настроена") + + TbHypn0Item.objects.filter(pk=item.pk).update(i_promo_clicks=F("i_promo_clicks") + 1) + return redirect(item.s_promo_url) + + def blog_feed(request: HttpRequest) -> HttpResponse: """ Лента статей блога / хроник гипноза с пагинацией. diff --git a/hypn0/templates/gallery/detail.html b/hypn0/templates/gallery/detail.html index 02d7ca4..e5f5129 100644 --- a/hypn0/templates/gallery/detail.html +++ b/hypn0/templates/gallery/detail.html @@ -169,6 +169,44 @@ {{ svg_stats.total_kb }} KB + + {% if item.has_promo %} + + +
+ + + ADV + + + {% if item.promo_desc %} + + {{ item.promo_desc|safe }} + + {% elif item.s_promo_title %} + + {{ item.s_promo_title|safe }} + + {% endif %} +
+ + +
+ {% if item.promo_cta %} + {{ item.promo_cta|safe }} + {% else %} + Visit + {% endif %} + + + +
+
+ {% endif %}