Oleg Komarov
ac49e9d5f6
This change solves a blocker for an upcoming drop of Version.file field: calculating `Extension.latest_version` relied on having `VersionFiles` already populated, but it was triggered on `Version.save()`, which happened before the cross table could be populated. One functional change is that a Version object is now created in NewVersionView, immediately after the File is saved, and not in NewVersionFinalizeView, as before. Tests are rewritten to require only FileFactory, the Extension and Version objects are created as it is done in the actual production code. This loses a bit on the ergonomics of factories, but overall makes the state of test objects more consistent, often simplifying the test code. VersionFactory is no longer needed, and has been cleaned up. ExtensionFactory is only used in construct_fake_notifications, and could also be replaced with an in-memory object constructed manually. Reviewed-on: #191 Reviewed-by: Anna Sirota <annasirota@noreply.localhost>
42 lines
1.2 KiB
Python
42 lines
1.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 extensions.models import Extension
|
|
from files.forms import FileForm
|
|
from files.models import File
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class UploadFileView(LoginRequiredMixin, CreateView):
|
|
model = File
|
|
template_name = 'extensions/submit.html'
|
|
form_class = FileForm
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
drafts = Extension.objects.authored_by(self.request.user).filter(
|
|
status=Extension.STATUSES.DRAFT
|
|
)
|
|
context['drafts'] = drafts
|
|
return context
|
|
|
|
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."""
|
|
file = form.save()
|
|
self.extension = Extension.create_from_file(file)
|
|
self.extension.create_version_from_file(file)
|
|
return super().form_valid(form)
|