Переехал на ImageField
This commit is contained in:
parent
9838efc99b
commit
6052a5bb0e
18
apps/gto/migrations/0015_alter_modeltypeicons_path.py
Normal file
18
apps/gto/migrations/0015_alter_modeltypeicons_path.py
Normal file
@ -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'),
|
||||
),
|
||||
]
|
||||
18
apps/gto/migrations/0016_rename_path_modeltypeicons_icon.py
Normal file
18
apps/gto/migrations/0016_rename_path_modeltypeicons_icon.py
Normal file
@ -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',
|
||||
),
|
||||
]
|
||||
18
apps/gto/migrations/0017_alter_modeltypeicons_icon.py
Normal file
18
apps/gto/migrations/0017_alter_modeltypeicons_icon.py
Normal file
@ -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'),
|
||||
),
|
||||
]
|
||||
22
apps/gto/migrations/0018_auto_20250515_0933.py
Normal file
22
apps/gto/migrations/0018_auto_20250515_0933.py
Normal file
@ -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),
|
||||
]
|
||||
@ -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 = "Иконки оборудования"
|
||||
@ -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,
|
||||
|
||||
28
apps/management/forms/gto_icon.py
Normal file
28
apps/management/forms/gto_icon.py
Normal file
@ -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": "Введите наименование"
|
||||
}
|
||||
),
|
||||
}
|
||||
@ -48,39 +48,49 @@
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
{% if error %}
|
||||
{% if form.errors %}
|
||||
<div class="alert alert-danger alert-dismissible">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×
|
||||
</button>
|
||||
<h4><i class="icon fa fa-ban"></i> Внимание !</h4>
|
||||
{% for com in error_comment %}
|
||||
<p margin:0;margin-left:10px; ">{{ com }}</p>
|
||||
{% for field, errors in form.errors.items %}
|
||||
{% for error in errors %}
|
||||
<p style="margin:0;margin-left:10px;">{{ error }}</p>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-group row">
|
||||
<label for="id_model" class="col-sm-2 col-form-label">Вид
|
||||
оборудования: </label>
|
||||
<label for="{{ form.model.id_for_label }}" class="col-sm-2 col-form-label">Вид
|
||||
оборудования:</label>
|
||||
<div class="col-sm-4">
|
||||
<input autocomplete="off" type="text" name="model" class="form-control"
|
||||
autofocus="" required="" id="id_model">
|
||||
{{ form.model }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group row">
|
||||
<label for="id_last_name" class="col-sm-2 col-form-label">Изображение: </label>
|
||||
<div class="col-sm-4">
|
||||
<label for="icon"
|
||||
class="col-sm-1 col-form-label">{{ gto_icon.model }}</label>
|
||||
<div>
|
||||
<label for="icon" class="file-upload-label">Выберите
|
||||
изображение</label><br>
|
||||
<input type="file" name="icon"
|
||||
class="form-control autosubmit" autofocus=""
|
||||
id="icon" style="visibility:hidden;"
|
||||
accept="image/png">
|
||||
<label for="{{ form.icon.id_for_label }}" class="col-sm-2 col-form-label">
|
||||
Изображение
|
||||
<span class="text-danger">*</span>
|
||||
</label>
|
||||
<div class="col-sm-8">
|
||||
<div class="custom-file">
|
||||
{{ form.icon }}
|
||||
<span class="file-name"></span>
|
||||
<label class="custom-file-label"
|
||||
for="{{ form.icon.id_form_label }}">
|
||||
Выберите изображение
|
||||
</label>
|
||||
{% if form.icon.errors %}
|
||||
<div class="invalid-feedback d-block">
|
||||
{% for error in form.icon.errors %}
|
||||
<p>{{ error }}</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div><!-- /.card-body -->
|
||||
<div class="card-footer">
|
||||
|
||||
@ -104,7 +104,6 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
@ -413,9 +413,9 @@
|
||||
<label for="icon_{{ gto_icon.id }}" class="file-upload-label">Выберите
|
||||
изображение</label><br>
|
||||
<span>Сейчас выбран: <a
|
||||
href="/{{ gto_icon.path }}">/{{ gto_icon.path }}</a></span>
|
||||
href="{{ gto_icon.icon.url }}">{{ gto_icon.icon.url }}</a></span>
|
||||
<input type="file" name="{{ gto_icon.model }}"
|
||||
value='/{{ gto_icon.path }}'
|
||||
value='{{ gto_icon.icon.url }}'
|
||||
class="form-control autosubmit" autofocus=""
|
||||
form="gto_icon_save"
|
||||
id="icon_{{ gto_icon.id }}" style="visibility:hidden;"
|
||||
@ -646,7 +646,7 @@
|
||||
function delete_user(id) {
|
||||
var Rec = $.ajax({
|
||||
method: 'POST',
|
||||
url: window.location.pathname + 'user/delete/',
|
||||
url: window.location.icon.image.urlname + 'user/delete/',
|
||||
async: true,
|
||||
data: {
|
||||
id: id,
|
||||
|
||||
@ -12,6 +12,7 @@ from apps.geoproc.models.model_sector import Sector
|
||||
from apps.gto.models.model_type_icons import ModelTypeIcons
|
||||
from apps.main.models.model_icons import Icons
|
||||
from apps.main.models.model_static_layer import StaticLayer
|
||||
from apps.management.forms.gto_icon import ModelTypeIconsForm
|
||||
from apps.management.forms.icon import IconForm
|
||||
from apps.management.forms.orthomosaic import OrthomosaicForm
|
||||
from apps.management.views import views_user
|
||||
@ -131,19 +132,17 @@ def gto_icon_save(request):
|
||||
file_obj = request.FILES[file]
|
||||
save_path = os.path.join("media/map/gto", file_obj.name)
|
||||
|
||||
# Убедитесь, что директория существует
|
||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||
|
||||
# Открываем файл для записи
|
||||
with open(save_path, "wb") as destination:
|
||||
for chunk in file_obj.chunks():
|
||||
destination.write(chunk)
|
||||
|
||||
icon = ModelTypeIcons.objects.filter(model=file).first()
|
||||
if icon is None:
|
||||
ModelTypeIcons.objects.create(model=file, path=save_path)
|
||||
ModelTypeIcons.objects.create(model=file, icon=file_obj)
|
||||
else:
|
||||
icon.path = save_path
|
||||
icon.icon = file_obj
|
||||
icon.save()
|
||||
return redirect("/management/?module=settings")
|
||||
except Exception:
|
||||
@ -155,29 +154,15 @@ def gto_icon_save(request):
|
||||
@never_cache
|
||||
def add_gto_icon(request):
|
||||
try:
|
||||
if request.method == "GET":
|
||||
return render(request, "icon_add.html")
|
||||
if request.method == "POST":
|
||||
file_obj = request.FILES["icon"]
|
||||
model = request.POST["model"]
|
||||
save_path = os.path.join("media/map/gto", file_obj.name)
|
||||
form = ModelTypeIconsForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect("/management/?module=settings")
|
||||
else:
|
||||
form = ModelTypeIconsForm()
|
||||
|
||||
# Убедитесь, что директория существует
|
||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||
|
||||
# Открываем файл для записи
|
||||
with open(save_path, "wb") as destination:
|
||||
for chunk in file_obj.chunks():
|
||||
destination.write(chunk)
|
||||
|
||||
icon = ModelTypeIcons.objects.filter(model=model).first()
|
||||
if icon is None:
|
||||
ModelTypeIcons.objects.create(model=model, path=save_path)
|
||||
else:
|
||||
icon.path = save_path
|
||||
icon.save()
|
||||
|
||||
return redirect("/management/?module=settings")
|
||||
return render(request, "icon_add.html", {'form': form})
|
||||
except Exception:
|
||||
logging.error(traceback.format_exc())
|
||||
return redirect("/management/")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user