You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.4 KiB
78 lines
2.4 KiB
# -*- coding: utf-8 -*-
|
|
from django.core.urlresolvers import reverse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.db.models.loading import get_model
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.http import HttpResponse
|
|
from django.views.generic import UpdateView
|
|
|
|
from .models import FileModel
|
|
from .forms import FileForm, FileUpdateForm
|
|
|
|
import json
|
|
|
|
|
|
@csrf_exempt
|
|
def ajax_post_file(request, obj_id):
|
|
"""
|
|
Takes file and file data and save it
|
|
"""
|
|
data = {'success': False}
|
|
# takes data from hidden input "model" and initial Model
|
|
Model = get_model(request.GET['model'].split('.')[0],
|
|
request.GET['model'].split('.')[1])
|
|
# initial model object
|
|
obj = Model.objects.get(id=obj_id)
|
|
|
|
if request.is_ajax() and request.method == 'POST':
|
|
file_form = FileForm(request.POST, request.FILES)
|
|
if file_form.is_valid():
|
|
file_form.save(request.FILES, obj)
|
|
data['success'] = True
|
|
else:
|
|
data['errors'] = file_form.errors
|
|
|
|
if request.is_ajax() and request.method == 'GET':
|
|
files = FileModel.objects.filter(
|
|
content_type=ContentType.objects.get_for_model(obj),
|
|
object_id=obj_id
|
|
)
|
|
files_data = []
|
|
for f in files:
|
|
files_data.append({
|
|
'name': f.file_name or f.file_path.name,
|
|
'size': f.file_path.size,
|
|
'file': f.file_path.url,
|
|
'type': 'file',
|
|
'remove_url': reverse('ajax_delete_file', args=[f.pk]),
|
|
'detail_link': reverse('file_update', args=[f.pk])
|
|
})
|
|
data['success'] = True
|
|
data['files'] = files_data
|
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json')
|
|
|
|
|
|
def ajax_delete_file(request, id):
|
|
"""
|
|
delete file
|
|
"""
|
|
if request.is_ajax():
|
|
f = FileModel.objects.get(id=id)
|
|
f.delete()
|
|
data = {'success': True}
|
|
else:
|
|
data = {'success': False}
|
|
return HttpResponse(json.dumps(data), content_type='application/json')
|
|
|
|
|
|
class FileUpdateView(UpdateView):
|
|
"""
|
|
Представление обновления файла
|
|
"""
|
|
template_name = 'c_admin/file/file_update.html'
|
|
form_class = FileUpdateForm
|
|
model = FileModel
|
|
|
|
def get_success_url(self):
|
|
return reverse('file_update', args=[self.object.pk])
|
|
|