def attachment_download(request, obj): download = not "preview" in request.GET try: return obj.get_file_response(download=download) except FileNotFoundError: messages.error(request, "Datei wurde nicht gefunden!") return render_error(request)
def _templatetest(request, templatename): """DEBUG: Test a template with given parameters""" try: get_template(templatename) return render(request, templatename, request.GET.dict()) except TemplateDoesNotExist: messages.error(request, f"Vorlage '{templatename}' wurde nicht gefunden.") return render_error(request, status=404) except TemplateSyntaxError as error: messages.error(request, f"Vorlage '{templatename}' enthält ungültige Syntax: {error}") except (NoReverseMatch) as error: messages.error(request, f"Er ist ein Fehler aufgetreten: {error}") return render_error(request, status=400)
def public_view_order(request, obj, order_key): lieferschein = bool("lieferschein" in dict(request.GET)) digital = not bool("druck" in dict(request.GET)) if str(obj.order_key) == order_key: return obj.get_pdf(lieferschein=lieferschein, digital=digital) messages.error( request, "Der Bestellungsschlüssel dieser Bestellung stimmt nicht überein.") return render_error(request)
def email_view(request, obj): """Render an email for online viewing""" token_received = request.GET.get("token", None) token_stored = str(obj.token) if request.user.has_perm("kmuhelper.view_email") or (token_received == token_stored): if obj.is_valid(): return HttpResponse(obj.render(online=True)) messages.warning( request, "Diese E-Mail kann zurzeit leider nicht angezeigt werden.") else: messages.error(request, "Du hast keinen Zugriff auf diese E-Mail!") return render_error(request)
def attachment_view(request, obj): """Render an attachment for online viewing""" token_received = request.GET.get("token", None) token_stored = str(obj.token) if request.user.has_perm("kmuhelper.download_attachment") or ( token_received == token_stored): download = "download" in request.GET try: return obj.get_file_response(download=download) except FileNotFoundError: messages.error(request, "Diese Datei ist leider nicht mehr verfügbar!") else: messages.error(request, "Du hast keinen Zugriff auf diesen E-Mail-Anhang!") return render_error(request)
def wrap(request, object_id, *args, **kwargs): if model.objects.filter(pk=int(object_id)).exists(): obj = model.objects.get(pk=int(object_id)) return function(request, obj, *args, **kwargs) if custom_response: return custom_response messages.warning( request, f'{model._meta.verbose_name} mit ID "{object_id}" wurde nicht gefunden!' ) if raise_404: raise Http404 if show_errorpage: return render_error(request) return redirect(redirect_url or reverse( f"admin:{model._meta.app_label}_{model._meta.model_name}_changelist" ))
def wc_webhooks(request): if request.method != "POST": messages.warning(request, "Dieser Endpunkt ist für Bots reserviert!") return render_error(request, status=405) if not ("x-wc-webhook-topic" in request.headers and "x-wc-webhook-source" in request.headers): return JsonResponse( { "accepted": True, "info": "Request was accepted but ignored because it doesn't contain any usable info!" }, status=202) erwartete_url = settings.get_secret_db_setting("wc-url").lstrip( "https://").lstrip("http://").split("/")[0] erhaltene_url = request.headers["x-wc-webhook-source"].lstrip( "https://").lstrip("http://").split("/")[0] if not erhaltene_url == erwartete_url: log( "[orange_red1]WooCommerce Webhook von einer falschen Webseite ignoriert![/] " + "- Erwartet:", erwartete_url, "- Erhalten:", erhaltene_url) return JsonResponse({ "accepted": False, "reason": "Unknown domain!", }, status=403) log("WooCommerce Webhook erhalten...") topic = request.headers["x-wc-webhook-topic"] obj = json.loads(request.body) if topic in ("product.updated", "product.created"): if Produkt.objects.filter(woocommerceid=obj["id"]).exists(): WooCommerce.product_update( Produkt.objects.get(woocommerceid=obj["id"]), obj) else: WooCommerce.product_create(obj) elif topic == "product.deleted": if Produkt.objects.filter(woocommerceid=obj["id"]).exists(): product = Produkt.objects.get(woocommerceid=obj["id"]) product.woocommerceid = 0 product.save() elif topic in ("customer.updated", "customer.created"): if Kunde.objects.filter(woocommerceid=obj["id"]).exists(): WooCommerce.customer_update( Kunde.objects.get(woocommerceid=obj["id"]), obj) else: WooCommerce.customer_create(obj) elif topic == "customer.deleted": if Kunde.objects.filter(woocommerceid=obj["id"]).exists(): customer = Kunde.objects.get(woocommerceid=obj["id"]) customer.woocommerceid = 0 customer.save() elif topic in ("order.updated", "order.created"): if Bestellung.objects.filter(woocommerceid=obj["id"]).exists(): WooCommerce.order_update( Bestellung.objects.get(woocommerceid=obj["id"]), obj) else: WooCommerce.order_create(obj) elif topic == "order.deleted": if Bestellung.objects.filter(woocommerceid=obj["id"]).exists(): order = Bestellung.objects.get(woocommerceid=obj["id"]) order.woocommerceid = 0 order.save() else: log(f"[orange_red1]Unbekanntes Thema: '{topic}'") return JsonResponse({"accepted": True})
def error(request): return render_error(request)