Exemple #1
0
def wc_update_product(request, obj):
    if not is_connected():
        messages.error(request,
                       "WooCommerce wurde scheinbar nicht richtig verbunden!")
    else:
        product = WooCommerce.product_update(obj)
        messages.success(request, "Produkt aktualisiert: " + str(product))
    return redirect(reverse("admin:kmuhelper_produkt_change", args=[obj.pk]))
Exemple #2
0
def wc_update_category(request, obj):
    if not is_connected():
        messages.error(request,
                       "WooCommerce wurde scheinbar nicht richtig verbunden!")
    else:
        category = WooCommerce.category_update(obj)
        messages.success(request, "Kategorie aktualisiert: " + str(category))
    return redirect(reverse("admin:kmuhelper_kategorie_change", args=[obj.pk]))
Exemple #3
0
def wc_update_order(request, obj):
    if not is_connected():
        messages.error(request,
                       "WooCommerce wurde scheinbar nicht richtig verbunden!")
    else:
        order = WooCommerce.order_update(obj)
        messages.success(request, "Bestellung aktualisiert: " + str(order))
    return redirect(reverse("admin:kmuhelper_bestellung_change",
                            args=[obj.pk]))
Exemple #4
0
def wc_import_customers(request):
    if not is_connected():
        messages.error(request,
                       "WooCommerce wurde scheinbar nicht richtig verbunden!")
    else:
        count = WooCommerce.customer_import()
        messages.success(
            request, (('{} neue Kunden' if count != 1 else 'Ein neuer Kunde') +
                      ' von WooCommerce importiert!').format(count))
    return redirect(reverse("admin:kmuhelper_kunde_changelist"))
Exemple #5
0
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})