From 6052a5bb0ed4bf70d1527f1a4d4d406b13e99273 Mon Sep 17 00:00:00 2001 From: zakhy <=> Date: Thu, 15 May 2025 16:48:21 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B5=D1=85=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B0=20ImageField?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0015_alter_modeltypeicons_path.py | 18 ++++++++ .../0016_rename_path_modeltypeicons_icon.py | 18 ++++++++ .../0017_alter_modeltypeicons_icon.py | 18 ++++++++ .../gto/migrations/0018_auto_20250515_0933.py | 22 +++++++++ apps/gto/models/model_type_icons.py | 6 +-- apps/gto/views/views_main.py | 2 +- apps/management/forms/gto_icon.py | 28 +++++++++++ apps/management/templates/icon_add.html | 46 +++++++++++-------- .../management/templates/icons/form_icon.html | 1 - apps/management/templates/settings.html | 6 +-- apps/management/views/views_settings.py | 35 ++++---------- 11 files changed, 149 insertions(+), 51 deletions(-) create mode 100644 apps/gto/migrations/0015_alter_modeltypeicons_path.py create mode 100644 apps/gto/migrations/0016_rename_path_modeltypeicons_icon.py create mode 100644 apps/gto/migrations/0017_alter_modeltypeicons_icon.py create mode 100644 apps/gto/migrations/0018_auto_20250515_0933.py create mode 100644 apps/management/forms/gto_icon.py 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 @@
- {% if error %} + {% if form.errors %}

Внимание !

- {% for com in error_comment %} -

{{ com }}

+ {% for field, errors in form.errors.items %} + {% for error in errors %} +

{{ error }}

+ {% endfor %} {% endfor %}
{% endif %}
- +
- + {{ form.model }}
- -
- -
-
- + +
+
+ {{ form.icon }} + + + {% if form.icon.errors %} +
+ {% for error in form.icon.errors %} +

{{ error }}

+ {% endfor %} +
+ {% endif %}
+
-
diff --git a/apps/management/templates/settings.html b/apps/management/templates/settings.html index aad0054..8e55bf7 100644 --- a/apps/management/templates/settings.html +++ b/apps/management/templates/settings.html @@ -413,9 +413,9 @@
Сейчас выбран: /{{ gto_icon.path }} + href="{{ gto_icon.icon.url }}">{{ gto_icon.icon.url }}