Dalai Felinto
d00bcc585a
When submitting a file the created extension gets a user right away. This makes sure there are no orphans extensions. The extension is then incomplete until the draft is saved once (which means finalizing the extension upload process, by adding a description and thumbnails). Any attempt to edit or submit a new extension will lead the user to the editing draft page. Note: We could add a new option to [x] Send for Review. The front-end even has a half-baked code for that. But should be tackled separately. ------------ Patch notes: * Originally when trying to finish an upload as a different user we woudl get a 403, now we get a 404. Reviewed-on: #54 Reviewed-by: Francesco Siddi <fsiddi@noreply.localhost>
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
import logging
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.db import transaction
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from .mixins import DraftMixin
|
|
from extensions.models import Version, Extension
|
|
from files.forms import FileForm
|
|
from files.models import File
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class UploadFileView(LoginRequiredMixin, DraftMixin, CreateView):
|
|
model = File
|
|
template_name = 'extensions/submit.html'
|
|
form_class = FileForm
|
|
|
|
def get_form_kwargs(self):
|
|
kwargs = super().get_form_kwargs()
|
|
kwargs['request'] = self.request
|
|
return kwargs
|
|
|
|
def get_success_url(self):
|
|
return self.extension.get_draft_url()
|
|
|
|
@transaction.atomic
|
|
def form_valid(self, form):
|
|
"""Create an extension and a version already, associated with the user."""
|
|
self.file = form.instance
|
|
|
|
parsed_extension_fields = self.file.parsed_extension_fields
|
|
if parsed_extension_fields:
|
|
# Try to look up extension by the same author and file info
|
|
extension = (
|
|
Extension.objects.authored_by(user_id=self.request.user.pk)
|
|
.filter(type=self.file.type, **parsed_extension_fields)
|
|
.first()
|
|
)
|
|
if extension:
|
|
logger.warning(
|
|
'Found existing extension pk=%s for file pk=%s',
|
|
extension.pk,
|
|
self.file.pk,
|
|
)
|
|
return False
|
|
|
|
# Make sure an extension has a user associated to it from the beginning, otherwise
|
|
# it will prevent it from being re-uploaded and yet not show on My Extensions.
|
|
self.extension = Extension.objects.update_or_create(
|
|
type=self.file.type, **parsed_extension_fields
|
|
)[0]
|
|
self.extension.authors.add(self.request.user)
|
|
self.extension.save()
|
|
|
|
# Need to save the form to be able to use the file to create the version.
|
|
self.object = self.file = form.save()
|
|
|
|
Version.objects.update_or_create(
|
|
extension=self.extension, file=self.file, **self.file.parsed_version_fields
|
|
)[0]
|
|
|
|
return super().form_valid(form)
|