mirror of
https://github.com/kjanat/livegraphs-django.git
synced 2026-02-13 22:35:45 +01:00
- Add ty.toml configuration with Django project root - Add py.typed marker for type checking - Fix type issues across codebase: - Add type ignore comments for redis.exceptions imports - Fix django.db.models.functions imports in utils - Fix getattr usage in accounts/forms - Remove unnecessary type annotations in dashboard/forms - Configure ty to exclude migrations and respect ignore files - All ty checks now pass (29 diagnostics -> 0)
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
# dashboard/forms.py
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from django import forms
|
|
|
|
if TYPE_CHECKING:
|
|
pass
|
|
|
|
from .models import Dashboard, DataSource
|
|
|
|
|
|
class DataSourceUploadForm(forms.ModelForm):
|
|
"""Form for uploading CSV files"""
|
|
|
|
class Meta:
|
|
model = DataSource
|
|
fields = ["name", "description", "file"]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.company = kwargs.pop("company", None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def save(self, commit=True):
|
|
instance = super().save(commit=False)
|
|
if self.company:
|
|
instance.company = self.company
|
|
if commit:
|
|
instance.save()
|
|
return instance
|
|
|
|
|
|
class DashboardForm(forms.ModelForm):
|
|
"""Form for creating and editing dashboards"""
|
|
|
|
class Meta:
|
|
model = Dashboard
|
|
fields = ["name", "description", "data_sources"]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.company = kwargs.pop("company", None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if self.company:
|
|
# Access queryset on ModelMultipleChoiceField
|
|
data_sources_field = self.fields["data_sources"] # type: ignore[assignment]
|
|
data_sources_field.queryset = DataSource.objects.filter(company=self.company) # type: ignore[attr-defined]
|
|
|
|
def save(self, commit=True):
|
|
instance = super().save(commit=False)
|
|
if self.company:
|
|
instance.company = self.company
|
|
if commit:
|
|
instance.save()
|
|
self.save_m2m()
|
|
return instance
|