diff --git a/pyscada/hmi/admin.py b/pyscada/hmi/admin.py index 2496f6a2..2f513b02 100644 --- a/pyscada/hmi/admin.py +++ b/pyscada/hmi/admin.py @@ -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 @@ -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 diff --git a/pyscada/hmi/migrations/0061_auto_20220610_1459.py b/pyscada/hmi/migrations/0061_auto_20220610_1459.py index 44e54643..552cc4b3 100644 --- a/pyscada/hmi/migrations/0061_auto_20220610_1459.py +++ b/pyscada/hmi/migrations/0061_auto_20220610_1459.py @@ -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( @@ -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], ), ), ] diff --git a/pyscada/hmi/migrations/0078_alter_theme_base_filename_alter_theme_view_filename.py b/pyscada/hmi/migrations/0078_alter_theme_base_filename_alter_theme_view_filename.py new file mode 100644 index 00000000..84511da4 --- /dev/null +++ b/pyscada/hmi/migrations/0078_alter_theme_base_filename_alter_theme_view_filename.py @@ -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, + ), + ), + ] diff --git a/pyscada/hmi/models.py b/pyscada/hmi/models.py index 1d879b44..0b18ce05 100644 --- a/pyscada/hmi/models.py +++ b/pyscada/hmi/models.py @@ -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"): @@ -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): @@ -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): diff --git a/pyscada/models.py b/pyscada/models.py index 8ab27ad9..30e8d16d 100755 --- a/pyscada/models.py +++ b/pyscada/models.py @@ -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