Skip to content

Commit

Permalink
update theme validation and remove unused import
Browse files Browse the repository at this point in the history
  • Loading branch information
clavay committed Feb 22, 2024
1 parent 7d6f53f commit 4bc3b73
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
17 changes: 17 additions & 0 deletions pyscada/hmi/admin.py
Expand Up @@ -33,6 +33,8 @@
from django import forms
from django.db.models.fields.related import OneToOneRel
from django.core.exceptions import ValidationError
from django.template.exceptions import TemplateDoesNotExist, TemplateSyntaxError
from django.template.loader import get_template

import logging

Expand Down Expand Up @@ -631,9 +633,24 @@ class ProcessFlowDiagramAdmin(admin.ModelAdmin):
save_as_continue = True


class ThemeForm(forms.ModelForm):

def clean(self):
super().clean()
try:
get_template(self.cleaned_data["base_filename"] + ".html").render()
except TemplateDoesNotExist as e:
raise ValidationError(f"Template {e} not found.")
try:
get_template(self.cleaned_data["view_filename"] + ".html").render({"base_html":self.cleaned_data.get("base_filename", "base") + ".html"})
except TemplateDoesNotExist as e:
raise ValidationError(f"Template {e} not found.")


class ThemeAdmin(admin.ModelAdmin):
save_as = True
save_as_continue = True
form = ThemeForm

def has_module_permission(self, request):
return False
Expand Down
4 changes: 2 additions & 2 deletions pyscada/hmi/migrations/0061_auto_20220610_1459.py
Expand Up @@ -17,7 +17,7 @@ class Migration(migrations.Migration):
default="base",
help_text="Enter the filename without '.html'",
max_length=400,
validators=[pyscada.hmi.models.validate_tempalte],
#validators=[pyscada.hmi.models.validate_tempalte],
),
),
migrations.AlterField(
Expand All @@ -27,7 +27,7 @@ class Migration(migrations.Migration):
default="view",
help_text="Enter the filename without '.html'",
max_length=400,
validators=[pyscada.hmi.models.validate_tempalte],
#validators=[pyscada.hmi.models.validate_tempalte],
),
),
]
@@ -0,0 +1,30 @@
# Generated by Django 4.2.5 on 2024-02-22 14:37

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("hmi", "0077_transformdatacountvalue"),
]

operations = [
migrations.AlterField(
model_name="theme",
name="base_filename",
field=models.CharField(
default="base",
help_text="Enter the filename without '.html'",
max_length=400,
),
),
migrations.AlterField(
model_name="theme",
name="view_filename",
field=models.CharField(
default="view",
help_text="Enter the filename without '.html'",
max_length=400,
),
),
]
33 changes: 14 additions & 19 deletions pyscada/hmi/models.py
Expand Up @@ -58,18 +58,6 @@ def _create_widget_content(sender, instance, created=False, **kwargs):
return


def validate_tempalte(value):
try:
get_template(value + ".html").render()
except TemplateDoesNotExist:
logger.warning("Template filename not found.")
raise ValidationError(
_("Template filename not found."),
)
except TemplateSyntaxError as e:
logger.info(e)


# raise a ValidationError if value not endswith .html or if template not found
def validate_html(value):
if not value.endswith(".html"):
Expand Down Expand Up @@ -208,13 +196,11 @@ class Theme(models.Model):
max_length=400,
default="base",
help_text="Enter the filename without '.html'",
validators=[validate_tempalte],
)
view_filename = models.CharField(
max_length=400,
default="view",
help_text="Enter the filename without '.html'",
validators=[validate_tempalte],
)

def __str__(self):
Expand All @@ -224,12 +210,21 @@ def check_all_themes(self):
# Delete theme with missing template file
for theme in Theme.objects.all():
try:
get_template(theme.view_filename + ".html").render({"base_html":"base"})
get_template(theme.base_filename + ".html").render()
except TemplateDoesNotExist:
get_template(theme.base_filename + ".html")
get_template(theme.view_filename + ".html")
except TemplateDoesNotExist as e:
logger.info(f"Template {e} not found. {self} will be delete.")
theme.delete()
except TemplateSyntaxError as e:
logger.info(e)
else:
try:
get_template(theme.view_filename + ".html").render({"base_html":theme.base_filename + ".html"})
except TemplateDoesNotExist as e:
logger.info(f"Template {e} used in the view as base_html not found. {self} will be delete.")
theme.delete()
except TemplateSyntaxError as e:
logger.info(e)
except AttributeError:
pass


class ControlElementOption(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion pyscada/models.py
Expand Up @@ -13,7 +13,7 @@
from django.db.models.fields.related import OneToOneRel
from django.forms.models import BaseInlineFormSet

from pyscada.utils import blow_up_data, timestamp_to_datetime, min_pass, max_pass
from pyscada.utils import blow_up_data, timestamp_to_datetime
from pyscada.utils import _get_objects_for_html as get_objects_for_html

from six import text_type
Expand Down

0 comments on commit 4bc3b73

Please sign in to comment.