vsdudakov / fastadmin

FastAdmin is an easy-to-use Admin Dashboard App for FastAPI/Flask/Django inspired by Django Admin.
https://vsdudakov.github.io/fastadmin/
MIT License
137 stars 15 forks source link

DashboardWidget no HTML rendering #43

Closed kanewi11 closed 3 months ago

kanewi11 commented 11 months ago

Why when I add UsersDashboardWidgetAdmin, I get no errors, but when I try to access the admin panel, I get no HTML rendering and two errors in the console:

image

from datetime import datetime, timedelta, timezone

from sqlalchemy import func, select
from fastadmin import DashboardWidgetAdmin, DashboardWidgetType, WidgetType, register_widget

from src.database import async_session_maker
from src.auth.models import User

@register_widget
class UsersDashboardWidgetAdmin(DashboardWidgetAdmin):
    title = 'Users'

    dashboard_widget_type = DashboardWidgetType.ChartLine
    x_field = 'date'
    y_field = 'count'
    x_field_filter_widget_type = WidgetType.DatePicker
    x_field_filter_widget_props = {'picker': 'month'}
    x_field_periods = ['day', 'week', 'month', 'year']

    async def get_data(
            self,
            min_x_field: str | None = None,
            max_x_field: str | None = None,
            period_x_field: str | None = None,
    ) -> dict:
        if not min_x_field:
            min_x_field_date = datetime.now(timezone.utc) - timedelta(days=360)
        else:
            min_x_field_date = datetime.fromisoformat(min_x_field.replace('Z', '+00:00'))
        if not max_x_field:
            max_x_field_date = datetime.now(timezone.utc) + timedelta(days=1)
        else:
            max_x_field_date = datetime.fromisoformat(max_x_field.replace('Z', '+00:00'))

        if not period_x_field or period_x_field not in self.x_field_periods:
            period_x_field = 'month'

        stmt = (
            select(
                func.to_char(func.date_trunc(period_x_field, User.created_at), 'DD/MM/YYYY').label('date'),
                func.count(User.id).label('count')
            )
            .where(
                User.created_at >= min_x_field_date,
                User.created_at <= max_x_field_date)
            .group_by('date')
            .order_by('date')
        )
        async with async_session_maker() as session:
            results = await session.execute(stmt)

        return {
            'results': [result._mapping for result in results.all()],
            'min_x_field': min_x_field_date.isoformat().replace('+00:00', 'Z'),
            'max_x_field': max_x_field_date.isoformat().replace('+00:00', 'Z'),
            'period_x_field': period_x_field,
        }
vsdudakov commented 3 months ago

Should be fixed in 0.2 version with integration of new frontend framework vite. Please reopen if not