Skip to content

Commit

Permalink
Fix dashboard widgets and auto register inlines.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsdudakov committed Aug 3, 2024
1 parent 751efe3 commit e6d8bb0
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 106 deletions.
11 changes: 9 additions & 2 deletions docs/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class App:
NAME = "FastAdmin"
AUTHOR_NAME = "Seva D."
AUTHOR_EMAIL = "vsdudakov@gmail.com"
ANTD_CHARTS_EXAMPLES = "https://ant-design-charts.antgroup.com/en/examples"


def read_cls_docstring(cls):
Expand All @@ -38,6 +39,12 @@ def read_cls_docstring(cls):

def get_versions():
return [
{
"version": "0.2.4",
"changes": [
"Fix dashboard widgets and auto register inlines.",
],
},
{
"version": "0.2.3",
"changes": [
Expand Down Expand Up @@ -444,7 +451,7 @@ def get_page_context(page_url):
{"type": "code-python", "content": inspect.getsource(DashboardWidgetAdmin)},
{
"type": "alert-warning",
"content": "Note: Please see <a href='https://charts.ant.design/en/examples' target='_blank'>antd charts</a> for <code>x_field_filter_widget_props</code>.",
"content": f"Note: Please see <a href='{ANTD_CHARTS_EXAMPLES}' target='_blank'>antd charts</a> for <code>x_field_filter_widget_props</code>.",
},
]
case "#widget-chart-types":
Expand All @@ -456,7 +463,7 @@ def get_page_context(page_url):
{"type": "code-python", "content": inspect.getsource(DashboardWidgetType)},
{
"type": "alert-warning",
"content": "Note: Please see <a href='https://charts.ant.design/en/examples' target='_blank'>antd charts</a> for more details (e.g. to see how they look like).",
"content": f"Note: Please see <a href='={ANTD_CHARTS_EXAMPLES}' target='_blank'>antd charts</a> for more details (e.g. to see how they look like).",
},
]
# models
Expand Down
25 changes: 13 additions & 12 deletions docs/code/dashboard/djangoorm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime, timedelta, timezone
from typing import Any
import datetime

from django.db import connection, models

Expand All @@ -20,33 +19,35 @@ def __str__(self):
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"} # noqa: RUF012
x_field_filter_widget_props: dict[str, str] = {"picker": "month"} # noqa: RUF012
x_field_periods = ["day", "week", "month", "year"] # noqa: RUF012

y_field = "count"

def get_data( # type: ignore [override]
self,
min_x_field: str | None = None,
max_x_field: str | None = None,
period_x_field: str | None = None,
) -> dict[str, Any]:
) -> dict:
def dictfetchall(cursor):
columns = [col[0] for col in cursor.description]
return [dict(zip(columns, row, strict=True)) for row in cursor.fetchall()]

with connection.cursor() as c:
if not min_x_field:
min_x_field_date = datetime.now(timezone.utc) - timedelta(days=360)
min_x_field_date = datetime.datetime.now(tz=datetime.UTC) - datetime.timedelta(days=360)
else:
min_x_field_date = datetime.fromisoformat(min_x_field.replace("Z", "+00:00"))
min_x_field_date = datetime.datetime.fromisoformat(min_x_field)
if not max_x_field:
max_x_field_date = datetime.now(timezone.utc) + timedelta(days=1)
max_x_field_date = datetime.datetime.now(tz=datetime.UTC) + datetime.timedelta(days=1)
else:
max_x_field_date = datetime.fromisoformat(max_x_field.replace("Z", "+00:00"))
max_x_field_date = datetime.datetime.fromisoformat(max_x_field)

if not period_x_field or period_x_field not in self.x_field_periods:
if not period_x_field or period_x_field not in (self.x_field_periods or []):
period_x_field = "month"

c.execute(
Expand All @@ -63,7 +64,7 @@ def dictfetchall(cursor):
results = dictfetchall(c)
return {
"results": results,
"min_x_field": min_x_field_date.isoformat().replace("+00:00", "Z"),
"max_x_field": max_x_field_date.isoformat().replace("+00:00", "Z"),
"min_x_field": min_x_field_date.isoformat(),
"max_x_field": max_x_field_date.isoformat(),
"period_x_field": period_x_field,
}
21 changes: 11 additions & 10 deletions docs/code/dashboard/tortoise.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime, timedelta, timezone
from typing import Any
import datetime

from tortoise import Tortoise, fields
from tortoise.models import Model
Expand All @@ -21,12 +20,14 @@ def __str__(self):
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: dict[str, Any] = {"picker": "month"} # noqa: RUF012
x_field_filter_widget_props: dict[str, str] = {"picker": "month"} # noqa: RUF012
x_field_periods = ["day", "week", "month", "year"] # noqa: RUF012

y_field = "count"

async def get_data(
self,
min_x_field: str | None = None,
Expand All @@ -36,13 +37,13 @@ async def get_data(
conn = Tortoise.get_connection("default")

if not min_x_field:
min_x_field_date = datetime.now(timezone.utc) - timedelta(days=360)
min_x_field_date = datetime.datetime.now(tz=datetime.UTC) - datetime.timedelta(days=360)
else:
min_x_field_date = datetime.fromisoformat(min_x_field.replace("Z", "+00:00"))
min_x_field_date = datetime.datetime.fromisoformat(min_x_field)
if not max_x_field:
max_x_field_date = datetime.now(timezone.utc) + timedelta(days=1)
max_x_field_date = datetime.datetime.now(tz=datetime.UTC) + datetime.timedelta(days=1)
else:
max_x_field_date = datetime.fromisoformat(max_x_field.replace("Z", "+00:00"))
max_x_field_date = datetime.datetime.fromisoformat(max_x_field)

if not period_x_field or period_x_field not in (self.x_field_periods or []):
period_x_field = "month"
Expand All @@ -60,7 +61,7 @@ async def get_data(
)
return {
"results": results,
"min_x_field": min_x_field_date.isoformat().replace("+00:00", "Z"),
"max_x_field": max_x_field_date.isoformat().replace("+00:00", "Z"),
"min_x_field": min_x_field_date.isoformat(),
"max_x_field": max_x_field_date.isoformat(),
"period_x_field": period_x_field,
}
6 changes: 0 additions & 6 deletions docs/code/inlines/tortoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ def __str__(self):
return self.message


@register(InlineUser)
class InlineUserAdmin(TortoiseModelAdmin):
# we have to register model for inline usage
pass


class UserMessageAdminInline(TortoiseInlineModelAdmin):
model = InlineUserMessage
list_display = ("user", "message")
Expand Down
Loading

0 comments on commit e6d8bb0

Please sign in to comment.