|
|
|
@ -4,12 +4,15 @@ from django.views.decorators.csrf import csrf_exempt |
|
|
|
from django.db.models.loading import get_model |
|
|
|
from django.db.models.loading import get_model |
|
|
|
from django.contrib.contenttypes.models import ContentType |
|
|
|
from django.contrib.contenttypes.models import ContentType |
|
|
|
from django.http import HttpResponse |
|
|
|
from django.http import HttpResponse |
|
|
|
from django.views.generic import UpdateView |
|
|
|
from django.views.generic import FormView |
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
|
|
from .models import FileModel |
|
|
|
from .models import FileModel |
|
|
|
from .forms import FileForm, FileUpdateForm |
|
|
|
from .forms import FileForm, FileUpdateForm |
|
|
|
|
|
|
|
|
|
|
|
import json |
|
|
|
import json |
|
|
|
|
|
|
|
import magic |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt |
|
|
|
@csrf_exempt |
|
|
|
@ -39,11 +42,12 @@ def ajax_post_file(request, obj_id): |
|
|
|
) |
|
|
|
) |
|
|
|
files_data = [] |
|
|
|
files_data = [] |
|
|
|
for f in files: |
|
|
|
for f in files: |
|
|
|
|
|
|
|
mime = magic.Magic(mime=True) |
|
|
|
files_data.append({ |
|
|
|
files_data.append({ |
|
|
|
'name': f.file_name or f.file_path.name, |
|
|
|
'name': f.file_name or f.file_path.name, |
|
|
|
'size': f.file_path.size, |
|
|
|
'size': f.file_path.size, |
|
|
|
'file': f.file_path.url, |
|
|
|
'file': f.file_path.url, |
|
|
|
'type': 'file', |
|
|
|
'type': mime.from_file(f.file_path.path), |
|
|
|
'remove_url': reverse('ajax_delete_file', args=[f.pk]), |
|
|
|
'remove_url': reverse('ajax_delete_file', args=[f.pk]), |
|
|
|
'detail_link': reverse('file_update', args=[f.pk]) |
|
|
|
'detail_link': reverse('file_update', args=[f.pk]) |
|
|
|
}) |
|
|
|
}) |
|
|
|
@ -66,13 +70,36 @@ def ajax_delete_file(request, id): |
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json') |
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileUpdateView(UpdateView): |
|
|
|
class FileUpdateView(FormView): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Представление обновления файла |
|
|
|
Представление обновления файла |
|
|
|
""" |
|
|
|
""" |
|
|
|
template_name = 'c_admin/file/file_update.html' |
|
|
|
template_name = 'c_admin/file/file_update.html' |
|
|
|
form_class = FileUpdateForm |
|
|
|
form_class = FileUpdateForm |
|
|
|
model = FileModel |
|
|
|
|
|
|
|
|
|
|
|
def get_object(self): |
|
|
|
|
|
|
|
pk = self.kwargs.get('pk') |
|
|
|
|
|
|
|
return get_object_or_404(FileModel, pk=pk) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_initial(self): |
|
|
|
|
|
|
|
data = super(FileUpdateView, self).get_initial() |
|
|
|
|
|
|
|
obj = self.get_object() |
|
|
|
|
|
|
|
data['file_path'] = obj.file_path |
|
|
|
|
|
|
|
data['purpose'] = obj.purpose |
|
|
|
|
|
|
|
for lid, (code, name) in enumerate(settings.LANGUAGES): |
|
|
|
|
|
|
|
data['file_name_%s' % code] = obj.file_name.translate(code) |
|
|
|
|
|
|
|
data['description_%s' % code] = obj.description.translate(code) |
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form): |
|
|
|
|
|
|
|
form.save(self.request, self.kwargs.get('pk')) |
|
|
|
|
|
|
|
return super(FileUpdateView, self).form_valid(form) |
|
|
|
|
|
|
|
|
|
|
|
def get_success_url(self): |
|
|
|
def get_success_url(self): |
|
|
|
return reverse('file_update', args=[self.object.pk]) |
|
|
|
return reverse('file_update', args=[self.get_object().pk]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs): |
|
|
|
|
|
|
|
ctx = super(FileUpdateView, self).get_context_data(**kwargs) |
|
|
|
|
|
|
|
ctx['object'] = self.get_object() |
|
|
|
|
|
|
|
ctx['languages'] = settings.LANGUAGES |
|
|
|
|
|
|
|
return ctx |
|
|
|
|