diff --git a/apps/gto/migrations/0015_alter_modeltypeicons_path.py b/apps/gto/migrations/0015_alter_modeltypeicons_path.py new file mode 100644 index 0000000..28ca400 --- /dev/null +++ b/apps/gto/migrations/0015_alter_modeltypeicons_path.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.25 on 2025-05-15 09:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('gto', '0014_auto_20250121_1511'), + ] + + operations = [ + migrations.AlterField( + model_name='modeltypeicons', + name='path', + field=models.FileField(upload_to='map/gto'), + ), + ] diff --git a/apps/gto/migrations/0016_rename_path_modeltypeicons_icon.py b/apps/gto/migrations/0016_rename_path_modeltypeicons_icon.py new file mode 100644 index 0000000..4541649 --- /dev/null +++ b/apps/gto/migrations/0016_rename_path_modeltypeicons_icon.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.25 on 2025-05-15 09:22 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('gto', '0015_alter_modeltypeicons_path'), + ] + + operations = [ + migrations.RenameField( + model_name='modeltypeicons', + old_name='path', + new_name='icon', + ), + ] diff --git a/apps/gto/migrations/0017_alter_modeltypeicons_icon.py b/apps/gto/migrations/0017_alter_modeltypeicons_icon.py new file mode 100644 index 0000000..443b5c6 --- /dev/null +++ b/apps/gto/migrations/0017_alter_modeltypeicons_icon.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.25 on 2025-05-15 09:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('gto', '0016_rename_path_modeltypeicons_icon'), + ] + + operations = [ + migrations.AlterField( + model_name='modeltypeicons', + name='icon', + field=models.ImageField(upload_to='map/gto'), + ), + ] diff --git a/apps/gto/migrations/0018_auto_20250515_0933.py b/apps/gto/migrations/0018_auto_20250515_0933.py new file mode 100644 index 0000000..67adfe6 --- /dev/null +++ b/apps/gto/migrations/0018_auto_20250515_0933.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.25 on 2025-05-15 09:33 + +from django.db import migrations + + +def fix_icon_paths(apps, schema_editor): + ModelTypeIcons = apps.get_model("gto", "ModelTypeIcons") + for item in ModelTypeIcons.objects.all(): + if item.icon and 'media/' in item.icon.name: + item.icon.name = item.icon.name.replace('media/', '') + item.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("gto", "0017_alter_modeltypeicons_icon"), + ] + + operations = [ + migrations.RunPython(fix_icon_paths, reverse_code=migrations.RunPython.noop), + ] diff --git a/apps/gto/models/model_type_icons.py b/apps/gto/models/model_type_icons.py index 9418485..f1e6d21 100644 --- a/apps/gto/models/model_type_icons.py +++ b/apps/gto/models/model_type_icons.py @@ -3,11 +3,11 @@ from django.db import models class ModelTypeIcons(models.Model): model = models.CharField(max_length=128) - path = models.CharField(max_length=256) + icon = models.ImageField(upload_to='map/gto') # Изменили на FileField def __str__(self): return self.model - class Meta: # отображение в админики + class Meta: verbose_name = "Иконка оборудования" - verbose_name_plural = "Иконки оборудования" + verbose_name_plural = "Иконки оборудования" \ No newline at end of file diff --git a/apps/gto/views/views_main.py b/apps/gto/views/views_main.py index ccbb49b..d5d12c0 100644 --- a/apps/gto/views/views_main.py +++ b/apps/gto/views/views_main.py @@ -36,7 +36,7 @@ def update_object(request): icon = "" icon_obj = ModelTypeIcons.objects.filter(model=type_g).first() if icon_obj is not None: - icon = "/" + icon_obj.path + icon = icon_obj.icon.url m_list.append( { "id": obj.id, diff --git a/apps/management/forms/gto_icon.py b/apps/management/forms/gto_icon.py new file mode 100644 index 0000000..bd53feb --- /dev/null +++ b/apps/management/forms/gto_icon.py @@ -0,0 +1,28 @@ +from django import forms + +from apps.gto.models.model_type_icons import ModelTypeIcons +from apps.management.forms.icon import validate_image + + +class ModelTypeIconsForm(forms.ModelForm): + icon = forms.ImageField( + required=True, + validators=[validate_image], + widget=forms.FileInput( + attrs={"class": "custom-file-input", "accept": "image/*", + "lang": "ru"} + ), + ) + + class Meta: + model = ModelTypeIcons + fields = ["model", "icon"] + widgets = { + "model": forms.TextInput(attrs={ + "autocomplete": "off", + "autofocus": True, + "class": "form-control", + "placeholder": "Введите наименование" + } + ), + } \ No newline at end of file diff --git a/apps/management/templates/icon_add.html b/apps/management/templates/icon_add.html index 5cb8c0f..fa90a48 100644 --- a/apps/management/templates/icon_add.html +++ b/apps/management/templates/icon_add.html @@ -48,39 +48,49 @@
{{ com }}
+ {% for field, errors in form.errors.items %} + {% for error in errors %} +{{ error }}
+ {% endfor %} {% endfor %}{{ error }}
+ {% endfor %} +