2018-05-01 02:23:36 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-04-19 19:52:40 +00:00
|
|
|
from django.http import JsonResponse
|
2018-05-01 02:17:18 +00:00
|
|
|
from django.shortcuts import render
|
2018-04-23 05:15:33 +00:00
|
|
|
from django.views.generic import FormView, DetailView
|
2018-04-19 19:52:40 +00:00
|
|
|
|
2018-05-30 03:14:36 +00:00
|
|
|
from .forms import ImageFileForm, FileForm
|
|
|
|
from .models import ImageFile, File
|
|
|
|
|
2018-04-19 19:52:40 +00:00
|
|
|
|
2018-05-01 02:23:36 +00:00
|
|
|
class FileUploadView(LoginRequiredMixin, FormView):
|
2018-04-19 19:52:40 +00:00
|
|
|
model = File
|
|
|
|
form_class = FileForm
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2018-05-01 02:23:36 +00:00
|
|
|
self.object = form.save(commit=False)
|
|
|
|
self.object.user = self.request.user
|
|
|
|
self.object.save()
|
2018-04-19 19:52:40 +00:00
|
|
|
|
2018-04-23 05:15:33 +00:00
|
|
|
return JsonResponse({
|
2018-06-27 18:43:39 +00:00
|
|
|
'is_valid': True,
|
|
|
|
'url': self.object.file.url,
|
2018-04-23 05:15:33 +00:00
|
|
|
'name': self.object.file.name,
|
|
|
|
'id': self.object.pk
|
|
|
|
})
|
2018-04-19 19:52:40 +00:00
|
|
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
return JsonResponse({'is_valid': False, 'errors': form.errors})
|
2018-04-23 05:15:33 +00:00
|
|
|
|
|
|
|
|
2018-05-30 03:14:36 +00:00
|
|
|
class ImageFileUploadView(FileUploadView):
|
|
|
|
model = ImageFile
|
|
|
|
form_class = ImageFileForm
|
|
|
|
|
|
|
|
|
2018-05-01 02:23:36 +00:00
|
|
|
class FileDeleteView(LoginRequiredMixin, DetailView):
|
2018-04-23 05:15:33 +00:00
|
|
|
model = File
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2018-05-01 02:23:36 +00:00
|
|
|
|
|
|
|
if request.user != self.object.user:
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2018-04-23 05:15:33 +00:00
|
|
|
self.object.delete()
|
2018-06-27 18:43:39 +00:00
|
|
|
|
2018-04-23 05:15:33 +00:00
|
|
|
return JsonResponse({
|
|
|
|
'success': True
|
|
|
|
})
|