Сохранение уже существующей иконки, кнопка для добавления
This commit is contained in:
parent
aa32181559
commit
62b02add51
@ -279,7 +279,7 @@
|
||||
<section class="content text-sm">
|
||||
|
||||
<form action="./?module=settings&cmd=oset_save" id="oset_save" method="post">{% csrf_token %}</form>
|
||||
<form action="./?module=settings&cmd=gto_icon_save" id="gto_icon_save" method="post">{% csrf_token %}</form>
|
||||
<form action="./?module=settings&cmd=gto_icon_save" id="gto_icon_save" enctype="multipart/form-data" method="post">{% csrf_token %}</form>
|
||||
<div class="form-horizontal">
|
||||
|
||||
<div class="row">
|
||||
@ -337,18 +337,19 @@
|
||||
</div>
|
||||
<div id="gto-collapse" class="collapse" data-parent="#gto-accordion" style="">
|
||||
<div class="card-body">
|
||||
<div class="mb-3"><i class="fas fa-info-circle" style="color: var(--primary);"></i><span class="ml-1">Изображения загружаются автоматически при выборе</span></div>
|
||||
{% for gto_icon in gto_icons %}
|
||||
<div class="form-group row">
|
||||
<label for="icon_{{ gto_icon.id }}" class="col-sm-1 col-form-label">{{ gto_icon.model }}</label>
|
||||
<div class="col-sm-4">
|
||||
<label for="icon_{{ gto_icon.id }}" class="file-upload-label">Выберите изображение</label><br>
|
||||
<span>Сейчас выбран: <a href="/{{ gto_icon.path }}">{{ gto_icon.path }}</a></span>
|
||||
<input type="file" name="icon_{{ gto_icon.id }}" value='{{ gto_icon.path }}' class="form-control" autofocus="" form="gto_icon_save"
|
||||
<span>Сейчас выбран: <a href="/{{ gto_icon.path }}">/{{ gto_icon.path }}</a></span>
|
||||
<input type="file" name="{{ gto_icon.model }}" value='/{{ gto_icon.path }}' class="form-control autosubmit" autofocus="" form="gto_icon_save"
|
||||
id="icon_{{ gto_icon.id }}" style="visibility:hidden;" accept="image/png">
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
<button id="btnok" form="oset_save" type="submit" class="btn bg-gradient-primary">Сохранить</button>
|
||||
<button type="button" class="btn bg-gradient-primary">Добавить</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -513,7 +514,16 @@
|
||||
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
const fileForm = document.getElementById('gto_icon_save')
|
||||
const fileInputs = document.querySelectorAll('.autosubmit')
|
||||
for (const fileInput of fileInputs) {
|
||||
fileInput.addEventListener('change', async (ev) => {
|
||||
console.log(ev)
|
||||
console.log(ev.target.value)
|
||||
fileForm.submit()
|
||||
})
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
@ -15,6 +15,7 @@ from django.shortcuts import render
|
||||
from django.shortcuts import redirect
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from apps.gto.models.model_type_icons import ModelTypeIcons
|
||||
from apps.management.views import views_user
|
||||
|
||||
from apps.general.models.model_settings import Settings
|
||||
@ -110,6 +111,35 @@ def file_save(request):
|
||||
logging.error(traceback.format_exc())
|
||||
return redirect('/management/')
|
||||
|
||||
|
||||
@login_required(login_url='/')
|
||||
@never_cache
|
||||
def gto_icon_save(request):
|
||||
try:
|
||||
for file in request.FILES:
|
||||
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)
|
||||
else:
|
||||
icon.path = save_path
|
||||
icon.save()
|
||||
return redirect('/management/?module=settings')
|
||||
except Exception as err:
|
||||
logging.error(traceback.format_exc())
|
||||
return redirect('/management/')
|
||||
|
||||
|
||||
@login_required(login_url='/')
|
||||
@never_cache
|
||||
def main_index(request):
|
||||
@ -125,6 +155,8 @@ def main_index(request):
|
||||
return oset_save(request)
|
||||
if get_param['cmd']=='file_save':
|
||||
return file_save(request)
|
||||
if get_param['cmd'] =='gto_icon_save':
|
||||
return gto_icon_save(request)
|
||||
context = generalmodule.get_context_template()
|
||||
return render(request, 'settings.html',context)
|
||||
except Exception as err:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user