46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
from django.shortcuts import render
|
|
from django.http import JsonResponse
|
|
from django.views.generic import FormView, DetailView
|
|
|
|
from .forms import FileForm
|
|
from .models import File
|
|
|
|
class FileUploadView(FormView):
|
|
# FIXME require login
|
|
|
|
model = File
|
|
form_class = FileForm
|
|
|
|
def form_valid(self, form):
|
|
self.object = form.save()
|
|
|
|
# FIXME set File owner
|
|
|
|
return JsonResponse({
|
|
'is_valid': True, 'url': self.object.file.url,
|
|
'name': self.object.file.name,
|
|
'id': self.object.pk
|
|
})
|
|
|
|
def form_invalid(self, form):
|
|
return JsonResponse({'is_valid': False, 'errors': form.errors})
|
|
|
|
|
|
class FileDeleteView(DetailView):
|
|
# FIXME require login
|
|
|
|
model = File
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
return self.post(request, *args, **kwargs)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
# FIXME check file ownership
|
|
|
|
self.object = self.get_object()
|
|
self.object.delete()
|
|
|
|
return JsonResponse({
|
|
'success': True
|
|
})
|