|
3 | 3 |
|
4 | 4 | from urllib.parse import urljoin |
5 | 5 |
|
6 | | -import requests |
7 | 6 | import structlog |
8 | 7 | from bs4 import BeautifulSoup |
9 | 8 | import chardet |
|
16 | 15 | HttpResponse, |
17 | 16 | HttpResponseNotFound, |
18 | 17 | HttpResponseRedirect, |
19 | | - HttpRequest, |
20 | 18 | ) |
21 | 19 | from django.shortcuts import redirect |
22 | 20 | from django.template.loader import render_to_string |
23 | 21 | from django.urls import reverse |
24 | | -from django.utils.decorators import method_decorator |
25 | 22 | from django.views import View |
26 | | -from django.views.decorators.cache import never_cache |
27 | 23 | from django.views.generic import TemplateView |
28 | 24 |
|
29 | 25 | from config.settings import ENABLE_DB_CACHE |
@@ -915,57 +911,3 @@ def get(self, request, requested_version): |
915 | 911 | if requested_version == "release": |
916 | 912 | new_path = "/libraries/" |
917 | 913 | return HttpResponseRedirect(new_path) |
918 | | - |
919 | | - |
920 | | -@method_decorator(never_cache, name="dispatch") |
921 | | -class QRCodeView(View): |
922 | | - """Handles QR code urls, sending them to Plausible, then redirecting to the desired url. |
923 | | -
|
924 | | - QR code urls are formatted /qrc/<campaign_identifier>/desired/path/to/content/, and will |
925 | | - result in a redirect to /desired/path/to/content/. |
926 | | -
|
927 | | - E.g. https://www.boost.org/qrc/pv-01/library/latest/beast/ will send this full url to Plausible, |
928 | | - then redirect to https://www.boost.org/library/latest/beast/ |
929 | | - """ |
930 | | - |
931 | | - def get(self, request: HttpRequest, campaign_identifier: str, main_path: str = ""): |
932 | | - absolute_url = request.build_absolute_uri(request.path) |
933 | | - referrer = request.META.get("HTTP_REFERER", "") |
934 | | - user_agent = request.META.get("HTTP_USER_AGENT", "") |
935 | | - |
936 | | - plausible_payload = { |
937 | | - "name": "pageview", |
938 | | - "domain": "qrc.boost.org", |
939 | | - "url": absolute_url, |
940 | | - "referrer": referrer, |
941 | | - } |
942 | | - |
943 | | - headers = {"Content-Type": "application/json", "User-Agent": user_agent} |
944 | | - |
945 | | - client_ip = request.META.get("HTTP_X_FORWARDED_FOR", "").split(",")[0].strip() |
946 | | - client_ip = client_ip or request.META.get("REMOTE_ADDR") |
947 | | - |
948 | | - if client_ip: |
949 | | - headers["X-Forwarded-For"] = client_ip |
950 | | - |
951 | | - try: |
952 | | - requests.post( |
953 | | - "https://plausible.io/api/event", |
954 | | - json=plausible_payload, |
955 | | - headers=headers, |
956 | | - timeout=2.0, |
957 | | - ) |
958 | | - except Exception as e: |
959 | | - # Don’t interrupt the redirect - just log it |
960 | | - logger.error(f"Plausible event post failed: {e}") |
961 | | - |
962 | | - # Now that we've sent the request url to plausible, we can redirect to the main_path |
963 | | - # Preserve the original querystring, if any. |
964 | | - # Example: /qrc/3/library/latest/algorithm/?x=1 -> /library/latest/algorithm/?x=1 |
965 | | - # `main_path` is everything after qrc/<campaign>/ thanks to <path:main_path>. |
966 | | - redirect_path = "/" + main_path if main_path else "/" |
967 | | - qs = request.META.get("QUERY_STRING") |
968 | | - if qs: |
969 | | - redirect_path = f"{redirect_path}?{qs}" |
970 | | - |
971 | | - return HttpResponseRedirect(redirect_path) |
0 commit comments