Skip to content

Commit

Permalink
Delete CalculatedVariable (moved to the pyscada.operations plugin)
Browse files Browse the repository at this point in the history
in order to not loose your calculated variables you should install
pyscada.operations plugin before running the pyscada migration 108.
  • Loading branch information
clavay authored and trombastic committed Jan 29, 2024
1 parent 75a90e9 commit b5a5f8c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 741 deletions.
119 changes: 3 additions & 116 deletions pyscada/admin.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import unicode_literals

from pyscada.models import Device, DeviceProtocol, DeviceHandler
from pyscada.models import PeriodicField, CalculatedVariableSelector, CalculatedVariable
from pyscada.models import Variable, VariableProperty, DataSource, DataSourceModel
from pyscada.models import Scaling, Color
from pyscada.models import Unit, Dictionary, DictionaryItem
Expand Down Expand Up @@ -251,43 +250,9 @@ def last_value(self, instance):
return f"ValueError {e} - with timestamp {v.timestamp_old} : {v.prev_value.__str__()} {instance.unit.unit}"
except Variable.DoesNotExist:
pass
def get_queryset(self, request):
"""Limit Pages to those that belong to the request's user."""
qs = super(VariableStateAdmin, self).get_queryset(request)
return qs.filter(calculatedvariable__isnull=True)


class CalculatedVariableSelectorAdmin(admin.ModelAdmin):
list_display = ("id", "main_variable", "active")
list_display_links = ("main_variable",)
list_editable = ("active",)
raw_id_fields = ("main_variable",)
filter_horizontal = ("period_fields",)
save_as = True
save_as_continue = True

# Disable changing variable
def get_readonly_fields(self, request, obj=None):
if obj is not None and obj.main_variable is not None:
return ["main_variable"]
return []


class PeriodicFieldAdmin(admin.ModelAdmin):
list_display = (
"id",
"__str__",
"type",
"property",
"start_from",
"period",
"period_factor",
)
list_filter = ("calculatedvariableselector",)
# list_editable = ('type', 'property', 'start_from', 'period', 'period_factor',)
list_display_links = None # ('__str__',)
save_as = True
save_as_continue = True
except TimeoutError:
return "Timeout on value query"
return f" - : NaN {instance.unit.unit}"


class DeviceForm(forms.ModelForm):
Expand Down Expand Up @@ -524,82 +489,7 @@ class CoreVariableAdmin(VariableAdmin):
)
list_display_links = ("name",)


class ExtendedCalculatedVariable(Variable):
class Meta:
proxy = True
verbose_name = "Calculated variable"
verbose_name_plural = "Calculated variables"


class CalculatedVariableAdminInline(admin.StackedInline):
model = CalculatedVariable


class CalculatedVariableAdmin(VariableAdmin):
list_display = (
"name",
"last_check",
"state",
"last_value",
"chart_line_color",
"short_name",
)
list_editable = (
"chart_line_color",
"short_name",
)
list_filter = (
ProtocolListFilter,
DeviceListFilter,
VariableListFilter,
"active",
"writeable",
"unit__unit",
)
list_display_links = None

def has_add_permission(self, request):
return False

def get_changelist_form(self, request, **kwargs):
return VariableAdminFrom

def last_check(self, instance):
return instance.calculatedvariable.last_check

def state(self, instance):
return instance.calculatedvariable.state

def store_variable(self, instance):
return instance.calculatedvariable.store_variable

def main_device(self, instance):
return instance.calculatedvariable.store_variable.device

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "device":
kwargs["queryset"] = Device.objects.filter(
short_name=CalculatedVariableSelector.dname
)
return super(CalculatedVariableAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs
)

def get_queryset(self, request):
"""Limit Pages to those that belong to the request's user."""
qs = super(admin.ModelAdmin, self).get_queryset(request)
return qs.filter(calculatedvariable__isnull=False)

inlines = [CalculatedVariableAdminInline]

@property
def media(self):
response = super(admin.ModelAdmin, self).media
return response

save_as = True
save_as_continue = True
# show only datasource with can_select as True
if db_field.name == "datasource":
ids = []
Expand Down Expand Up @@ -1158,9 +1048,6 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
admin_site.register(DeviceHandler, DeviceHandlerAdmin)
admin_site.register(Variable, CoreVariableAdmin)
admin_site.register(VariableProperty, VariablePropertyAdmin)
admin_site.register(CalculatedVariableSelector, CalculatedVariableSelectorAdmin)
admin_site.register(PeriodicField, PeriodicFieldAdmin)
admin_site.register(ExtendedCalculatedVariable, CalculatedVariableAdmin)
admin_site.register(Scaling, ScalingAdmin)
admin_site.register(Unit)
admin_site.register(ComplexEvent, ComplexEventAdmin)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.5 on 2023-12-14 11:37

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("pyscada", "0106_datasource_datasourcemodel_djangodatabase_and_more"),
]

operations = [
migrations.AlterField(
model_name="calculatedvariableselector",
name="period_fields",
field=models.ManyToManyField(blank=True, to="pyscada.periodicfield"),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.2.5 on 2024-01-16 16:16

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("pyscada", "0107_alter_calculatedvariableselector_period_fields"),
]

operations = [
migrations.RemoveField(
model_name="calculatedvariable",
name="period",
),
migrations.RemoveField(
model_name="calculatedvariable",
name="store_variable",
),
migrations.RemoveField(
model_name="calculatedvariable",
name="variable_calculated_fields",
),
migrations.RemoveField(
model_name="calculatedvariableselector",
name="main_variable",
),
migrations.RemoveField(
model_name="calculatedvariableselector",
name="period_fields",
),
migrations.DeleteModel(
name="ExtendedCalculatedVariable",
),
migrations.DeleteModel(
name="CalculatedVariable",
),
migrations.DeleteModel(
name="CalculatedVariableSelector",
),
migrations.DeleteModel(
name="PeriodicField",
),
]
Loading

0 comments on commit b5a5f8c

Please sign in to comment.