import json import random from functools import lru_cache from django.contrib.humanize.templatetags.humanize import intcomma from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from django.views.generic import RedirectView, TemplateView from unfold.views import UnfoldModelAdminViewMixin from order.models import OrderModel class HomeView(RedirectView): pattern_name = "admin:index" def dashboard_callback(request, context): pending_count = OrderModel.objects.filter(status='ADMIN_PENDING').count() context.update(random_data()) context.update({'pending_count': pending_count}) return context @lru_cache def random_data(): WEEKDAYS = [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", ] positive = [[1, random.randrange(8, 28)] for i in range(1, 28)] negative = [[-1, -random.randrange(8, 28)] for i in range(1, 28)] average = [r[1] - random.randint(3, 5) for r in positive] performance_positive = [[1, random.randrange(8, 28)] for i in range(1, 28)] performance_negative = [[-1, -random.randrange(8, 28)] for i in range(1, 28)] response = { "navigation": [ {"title": _("Dashboard"), "link": "/", "active": True}, {"title": _("Products"), "link": "/admin/product/productmodel/"}, {"title": _("Orders"), "link": "/admin/order/ordermodel/"}, ], "kpi": [ { "title": "IPhone 16 Pro Max", "metric": f"${intcomma(f"{random.uniform(1000, 9999):.02f}")}", "footer": mark_safe( f'+{intcomma(f"{random.uniform(1, 9):.02f}")}% progress from last week' ), "chart": json.dumps( { "labels": [WEEKDAYS[day % 7] for day in range(1, 28)], "datasets": [{"data": average, "borderColor": "#9333ea"}], } ), }, { "title": "Macbook Pro M3", "metric": f"${intcomma(f"{random.uniform(1000, 9999):.02f}")}", "footer": mark_safe( f'+{intcomma(f"{random.uniform(1, 9):.02f}")}% progress from last week' ), }, { "title": "Apple Watch 8", "metric": f"${intcomma(f"{random.uniform(1000, 9999):.02f}")}", "footer": mark_safe( f'+{intcomma(f"{random.uniform(1, 9):.02f}")}% progress from last week' ), }, ], "progress": [ { "title": "📱 Phone and Mobile", "description": "$2,499.99", "value": 20, }, { "title": "⌚ Watch and Smart Watch", "description": "$1,799.49", "value": 60, }, { "title": "💻 Laptop and Mac Book", "description": "$3,499.99", "value": 85, }, { "title": "📹 Camera and Video Recorder", "description": "$2,299.99", "value": 50, }, { "title": "📷 Camera and Picture Capture", "description": "$1,799.00", "value": 10, }, { "title": "📚 Course Sales and Learning", "description": "$1,299.49", "value": 20, }, { "title": "📈 Ads Revenue and Marketing", "description": "$3,199.99", "value": 90, }, { "title": "📊 Customer Retention and Engagement", "description": "$1,649.00", "value": 80, }, { "title": "📣 Marketing ROI and Campaigns", "description": "$2,199.75", "value": 45, }, { "title": "🤝 Affiliate Partnerships and Collaborations", "description": "$2,899.95", "value": 78, }, ], "chart": json.dumps( { "labels": [WEEKDAYS[day % 7] for day in range(1, 28)], "datasets": [ { "label": "Example 1", "type": "line", "data": average, "borderColor": "var(--color-primary-500)", }, { "label": "Example 2", "data": positive, "backgroundColor": "var(--color-primary-700)", }, { "label": "Example 3", "data": negative, "backgroundColor": "var(--color-primary-300)", }, ], } ), "performance": [ { "title": _("Last week revenue"), "metric": "$1,234.56", "footer": mark_safe( '+3.14% progress from last week' ), "chart": json.dumps( { "labels": [WEEKDAYS[day % 7] for day in range(1, 28)], "datasets": [ { "data": performance_positive, "borderColor": "var(--color-primary-700)", } ], } ), }, { "title": _("Last week expenses"), "metric": "$1,234.56", "footer": mark_safe( '+3.14% progress from last week' ), "chart": json.dumps( { "labels": [WEEKDAYS[day % 7] for day in range(1, 28)], "datasets": [ { "data": performance_negative, "borderColor": "var(--color-primary-300)", }, ], } ), }, ], } return response