40356-vm/core/views.py
2026-06-30 00:49:24 +00:00

122 lines
4.5 KiB
Python

from django.contrib import messages
from django.db.models import Count, Q
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from .forms import PaymentConfirmationForm
from .models import PaymentConfirmation, Product, ProductCategory
WHATSAPP_NUMBER = "5359177041"
BUSINESS_NAME = "Flores Software Ideas"
def _base_context(**extra):
context = {
"project_name": BUSINESS_NAME,
"project_description": "Flores Software Ideas ofrece soluciones tecnológicas, software empresarial y pagos por QR, Transfermóvil y Enzona.",
"whatsapp_number": WHATSAPP_NUMBER,
}
context.update(extra)
return context
def home(request):
categories = ProductCategory.objects.filter(is_active=True).annotate(
active_products=Count("products", filter=Q(products__is_active=True))
)
featured_products = Product.objects.filter(is_active=True, is_featured=True).select_related("category")[:6]
recent_confirmations = PaymentConfirmation.objects.filter(status=PaymentConfirmation.STATUS_PENDING).count()
payment_form = PaymentConfirmationForm()
return render(
request,
"core/index.html",
_base_context(
categories=categories,
featured_products=featured_products,
payment_form=payment_form,
pending_payments=recent_confirmations,
page_title="Tienda virtual de soluciones tecnológicas",
meta_description="Compra o cotiza software empresarial, hardware, desarrollo web, apps móviles y soporte técnico con pagos por QR, Transfermóvil o Enzona.",
),
)
def catalog(request):
category_slug = request.GET.get("categoria")
categories = ProductCategory.objects.filter(is_active=True)
products = Product.objects.filter(is_active=True).select_related("category")
selected_category = None
if category_slug:
selected_category = get_object_or_404(ProductCategory, slug=category_slug, is_active=True)
products = products.filter(category=selected_category)
return render(
request,
"core/catalog.html",
_base_context(
categories=categories,
products=products,
selected_category=selected_category,
page_title="Catálogo tecnológico",
meta_description="Explora productos y servicios tecnológicos listos para cotizar y pagar en línea.",
),
)
def product_detail(request, slug):
product = get_object_or_404(Product.objects.select_related("category"), slug=slug, is_active=True)
related_products = Product.objects.filter(is_active=True, category=product.category).exclude(pk=product.pk)[:3]
payment_form = PaymentConfirmationForm(initial={"product": product, "amount": product.price_from})
return render(
request,
"core/product_detail.html",
_base_context(
product=product,
related_products=related_products,
payment_form=payment_form,
page_title=product.name,
meta_description=product.short_description,
),
)
def confirm_payment(request):
initial = {}
product_slug = request.GET.get("producto")
if product_slug:
product = get_object_or_404(Product, slug=product_slug, is_active=True)
initial = {"product": product, "amount": product.price_from}
if request.method == "POST":
form = PaymentConfirmationForm(request.POST, request.FILES)
if form.is_valid():
confirmation = form.save()
messages.success(request, "Tu confirmación fue enviada. Un administrador revisará el pago en el panel.")
return redirect("payment_success", pk=confirmation.pk)
else:
form = PaymentConfirmationForm(initial=initial)
return render(
request,
"core/confirm_payment.html",
_base_context(
form=form,
page_title="Confirmar pago",
meta_description="Reporta tu pago por QR, Transfermóvil, Enzona o transferencia para revisión manual del administrador.",
),
)
def payment_success(request, pk):
confirmation = get_object_or_404(PaymentConfirmation.objects.select_related("product"), pk=pk)
return render(
request,
"core/payment_success.html",
_base_context(
confirmation=confirmation,
page_title="Pago recibido para revisión",
meta_description="Confirmación de pago recibida y pendiente de aprobación manual.",
),
)