Skip to content

Commit

Permalink
added allowed_chart_types with defaults to kpi & removed chart type m…
Browse files Browse the repository at this point in the history
…odel
  • Loading branch information
samul-1 committed Nov 11, 2023
1 parent 972d980 commit ecd0d1b
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 98 deletions.
50 changes: 26 additions & 24 deletions smartreport_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
from django.contrib import admin
from .models import Kpi, ReportTemplate, ReportTemplatePage, KpiReportElement, Alarm, ChartType
from .models import (
Kpi,
ReportTemplate,
ReportTemplatePage,
KpiReportElement,
Alarm,
)


class KpiReportElementInline(admin.TabularInline):
model = KpiReportElement
extra = 1


class ReportTemplatePageInline(admin.TabularInline):
model = ReportTemplatePage
extra = 1
show_change_link = True
inlines = [KpiReportElementInline]


@admin.register(Kpi)
class KpiAdmin(admin.ModelAdmin):
list_display = ('name',)
search_fields = ('name',)
list_display = ("name",)
search_fields = ("name",)


@admin.register(ReportTemplate)
class ReportTemplateAdmin(admin.ModelAdmin):
list_display = ('name', 'frequency')
list_filter = ('frequency',)
list_display = ("name", "frequency")
list_filter = ("frequency",)
inlines = [ReportTemplatePageInline]


@admin.register(ReportTemplatePage)
class ReportTemplatePageAdmin(admin.ModelAdmin):
list_display = ('report_template', 'layout')
list_filter = ('layout',)
list_display = ("report_template", "layout")
list_filter = ("layout",)
inlines = [KpiReportElementInline]


@admin.register(KpiReportElement)
class KpiReportElementAdmin(admin.ModelAdmin):
list_display = ('report_page', 'kpi', 'chart_type')
list_filter = ('chart_type',)
list_display = ("report_page", "kpi", "chart_type")
list_filter = ("chart_type",)


@admin.register(Alarm)
class AlarmAdmin(admin.ModelAdmin):
list_display = ('id', 'user_type', 'kpi', 'min_value', 'max_value')
list_filter = ('user_type', 'kpi')
search_fields = ('user_type', 'kpi__name')

def get_kpi_name(self, obj):
return obj.kpi.name

get_kpi_name.short_description = 'KPI Name'

@admin.register(ChartType)
class ChartTypeAdmin(admin.ModelAdmin):
list_display = ('id', 'kpi', 'plot_name')
list_filter = ('kpi',)
search_fields = ('kpi__name',)
list_display = ("id", "user_type", "kpi", "min_value", "max_value")
list_filter = ("user_type", "kpi")
search_fields = ("user_type", "kpi__name")

def get_kpi_name(self, obj):
return obj.kpi.name

get_kpi_name.short_description = 'KPI Name'
get_kpi_name.short_description = "KPI Name"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.7 on 2023-11-11 10:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('smartreport_app', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='kpi',
name='allowed_charts',
field=models.JSONField(default=list),
),
migrations.DeleteModel(
name='ChartType',
),
]
18 changes: 18 additions & 0 deletions smartreport_app/migrations/0003_alter_kpi_allowed_charts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-11 10:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('smartreport_app', '0002_kpi_allowed_charts_delete_charttype'),
]

operations = [
migrations.AlterField(
model_name='kpi',
name='allowed_charts',
field=models.JSONField(default=['line', 'bar', 'pie', 'doughnut', 'radar']),
),
]
19 changes: 19 additions & 0 deletions smartreport_app/migrations/0004_alter_kpi_allowed_charts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.7 on 2023-11-11 10:30

from django.db import migrations, models
import smartreport_app.models


class Migration(migrations.Migration):

dependencies = [
('smartreport_app', '0003_alter_kpi_allowed_charts'),
]

operations = [
migrations.AlterField(
model_name='kpi',
name='allowed_charts',
field=models.JSONField(default=smartreport_app.models.DEFAULT_CHART_CHOICES),
),
]
38 changes: 25 additions & 13 deletions smartreport_app/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from django.core.exceptions import ValidationError


class UserType(models.TextChoices):
DOCTOR = "doctor", _("Doctor")
Expand All @@ -9,6 +11,18 @@ class UserType(models.TextChoices):
MACHINE_MAINTAINER = "machine_maintainer", _("Machine Maintainer")


CHART_CHOICES = (
"line",
"bar",
"pie",
"scatter",
)


def DEFAULT_CHART_CHOICES():
return [*CHART_CHOICES]


class Kpi(models.Model):
name = models.CharField(max_length=255)

Expand All @@ -17,27 +31,25 @@ class Kpi(models.Model):
choices=UserType.choices,
)

allowed_charts = models.JSONField(default=DEFAULT_CHART_CHOICES)

priority = models.IntegerField(default=0)

isNew = models.BooleanField(default=True)

def __str__(self):
return self.name

def save(self, *args, **kwargs):
self.full_clean()
super().save(*args, **kwargs)

class ChartType(models.Model):
kpi = models.ForeignKey(
Kpi, related_name="allowed_charts", on_delete=models.CASCADE
)

CHART_CHOICES = [
("line", "line"),
("bar", "bar"),
("pie", "pie"),
("doughnut", "doughnut"),
("radar", "radar"),
]
plot_name = models.CharField(max_length=128, choices=CHART_CHOICES)
def clean(self):
if not isinstance(self.allowed_charts, list):
raise ValidationError("Allowed charts must be a list")
for chart in self.allowed_charts:
if chart not in CHART_CHOICES:
raise ValidationError(f"{chart} is not a valid chart type")


class ReportTemplate(models.Model):
Expand Down
28 changes: 14 additions & 14 deletions smartreport_app/serializers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from rest_framework import serializers
from .models import KpiReportElement, ReportTemplatePage, ReportTemplate, Kpi, Alarm, ChartType, DashboardLayout
from .models import (
KpiReportElement,
ReportTemplatePage,
ReportTemplate,
Kpi,
Alarm,
DashboardLayout,
)


class KpiReportElementSerializer(serializers.ModelSerializer):
class Meta:
model = KpiReportElement
fields = "__all__"
fields = ["id", "kpi", "chart_type"]


class ReportTemplatePageSerializer(serializers.ModelSerializer):
elements = KpiReportElementSerializer(many=True)

class Meta:
model = ReportTemplatePage
fields = "__all__"
fields = ["elements", "id", "layout"]


class ReportTemplateSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -43,24 +50,16 @@ def create(self, validated_data):
KpiReportElement.objects.create(report_page=report_page, **element_data)

return report_template

class ChartTypeSerializer(serializers.ModelSerializer):
class Meta:
model = ChartType
fields = "__all__"


class KpiSerializer(serializers.ModelSerializer):
allowed_charts = ChartTypeSerializer(many=True, required=False)

class Meta:
model = Kpi
fields = "__all__"

def to_representation(self, instance):
data = super().to_representation(instance)
data['id'] = str(data['id'])
data['allowed_charts'] = [chart['plot_name'] for chart in data['allowed_charts']]
data["id"] = str(data["id"])
return data


Expand All @@ -69,7 +68,8 @@ class Meta:
model = Alarm
fields = "__all__"


class DashboardLayoutSerializer(serializers.ModelSerializer):
class Meta:
model = DashboardLayout
fields = "__all__"
fields = "__all__"
18 changes: 7 additions & 11 deletions smartreport_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'report-templates', views.ReportTemplateViewSet)
router.register(r'report-template-pages', views.ReportTemplatePageViewSet)
router.register(r'kpi-report-elements', views.KpiReportElementViewSet)
router.register(r'kpi-list', views.KpiViewSet)
router.register(r'alarms-list', views.AlarmViewSet)
router.register(r'chart-types', views.ChartTypeViewSet)
router.register(r'dashboard-layout', views.DashboardLayoutViewSet)
router.register(r"report-templates", views.ReportTemplateViewSet)
router.register(r"report-template-pages", views.ReportTemplatePageViewSet)
router.register(r"kpi-report-elements", views.KpiReportElementViewSet)
router.register(r"kpi-list", views.KpiViewSet)
router.register(r"alarms-list", views.AlarmViewSet)
router.register(r"dashboard-layout", views.DashboardLayoutViewSet)

# The API URLs are now determined automatically by the router.
urlpatterns = [
path('', include(router.urls)),
path('kpi-data/', views.kpi_data)
]
urlpatterns = [path("", include(router.urls)), path("kpi-data/", views.kpi_data)]
Loading

0 comments on commit ecd0d1b

Please sign in to comment.