38652-vm/core/views.py
2026-02-20 20:50:37 +00:00

56 lines
1.9 KiB
Python

import os
import platform
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Category, Vendor, Product
def home(request):
"""Render the landing screen with marketplace products and categories."""
categories = Category.objects.all()
featured_products = Product.objects.filter(is_active=True).order_by('-created_at')[:8]
vendors = Vendor.objects.all()[:6]
context = {
"project_name": "Pasar UMKM Desa Pemagarsari",
"categories": categories,
"featured_products": featured_products,
"vendors": vendors,
}
return render(request, "core/index.html", context)
def product_list(request, category_slug=None):
"""List all products, optionally filtered by category."""
category = None
products = Product.objects.filter(is_active=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
context = {
'category': category,
'products': products,
'categories': Category.objects.all(),
}
return render(request, "core/product_list.html", context)
def product_detail(request, slug):
"""Show details of a single product."""
product = get_object_or_404(Product, slug=slug, is_active=True)
related_products = Product.objects.filter(category=product.category).exclude(id=product.id)[:4]
context = {
'product': product,
'related_products': related_products,
}
return render(request, "core/product_detail.html", context)
def vendor_detail(request, slug):
"""Show details of a single vendor and their products."""
vendor = get_object_or_404(Vendor, slug=slug)
products = vendor.products.filter(is_active=True)
context = {
'vendor': vendor,
'products': products,
}
return render(request, "core/vendor_detail.html", context)