Changeset View
Changeset View
Standalone View
Standalone View
blog/admin.py
| Show First 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | def post_change_view(self, request: HttpRequest, object_id: int) -> HttpResponse: | ||||
| form = PostChangeForm(request.POST, request.FILES) | form = PostChangeForm(request.POST, request.FILES) | ||||
| if form.is_valid(): | if form.is_valid(): | ||||
| revision = form.save(commit=False) | revision = form.save(commit=False) | ||||
| post, revision = self._set_post_and_revision_fields(post, revision, request=request) | post, revision = self._set_post_and_revision_fields(post, revision, request=request) | ||||
| if not revision.thumbnail: | if not revision.thumbnail: | ||||
| # It is not possible to set an initial value in a FileField, so we use | # It is not possible to set an initial value in a FileField, so we use | ||||
| # the previous picture so as not to force the user to set it each time. | # the previous picture so as not to force the user to set it each time. | ||||
| revision.thumbnail = previous_revision.thumbnail | revision.thumbnail = previous_revision.thumbnail | ||||
| revision.storage_location = previous_revision.storage_location | |||||
| post.save() | post.save() | ||||
| revision.save(force_insert=True) | revision.save(force_insert=True) | ||||
| message = f'The post “{revision.title}” was changed successfully.' | message = f'The post “{revision.title}” was changed successfully.' | ||||
| self.message_user(request, message, messages.SUCCESS) | self.message_user(request, message, messages.SUCCESS) | ||||
| return redirect('admin:blog_post_changelist') | return redirect('admin:blog_post_changelist') | ||||
| else: | else: | ||||
| form = PostChangeForm(form.cleaned_data) | form = PostChangeForm(form.cleaned_data) | ||||
| return render(request, 'blog/admin_change_post.html', {'form': form}) | return render(request, 'blog/admin_change_post.html', {'form': form}) | ||||
| form = PostChangeForm(instance=previous_revision) | form = PostChangeForm(instance=previous_revision) | ||||
| return render(request, 'blog/admin_change_post.html', {'form': form}) | return render(request, 'blog/admin_change_post.html', {'form': form}) | ||||