|
24 | 24 | from django.utils.translation import ugettext_lazy as _
|
25 | 25 | from django.views.generic import ListView, TemplateView, View
|
26 | 26 | from formtools.wizard.views import SessionWizardView
|
27 |
| -from vanilla import CreateView, DeleteView, DetailView, GenericView, UpdateView |
| 27 | +from vanilla import ( |
| 28 | + CreateView, |
| 29 | + DeleteView, |
| 30 | + DetailView, |
| 31 | + FormView, |
| 32 | + GenericView, |
| 33 | + UpdateView, |
| 34 | +) |
28 | 35 |
|
29 | 36 | from readthedocs.builds.forms import VersionForm
|
30 | 37 | from readthedocs.builds.models import Version
|
@@ -559,53 +566,58 @@ def project_notifications_delete(request, project_slug):
|
559 | 566 | return HttpResponseRedirect(project_dashboard)
|
560 | 567 |
|
561 | 568 |
|
562 |
| -@login_required |
563 |
| -def project_translations(request, project_slug): |
564 |
| - """Project translations view and form view.""" |
565 |
| - project = get_object_or_404( |
566 |
| - Project.objects.for_admin_user(request.user), |
567 |
| - slug=project_slug, |
568 |
| - ) |
569 |
| - form = TranslationForm( |
570 |
| - data=request.POST or None, |
571 |
| - parent=project, |
572 |
| - user=request.user, |
573 |
| - ) |
| 569 | +class ProjectTranslationsMixin(ProjectAdminMixin, PrivateViewMixin): |
574 | 570 |
|
575 |
| - if request.method == 'POST' and form.is_valid(): |
576 |
| - form.save() |
577 |
| - project_dashboard = reverse( |
| 571 | + def get_success_url(self): |
| 572 | + return reverse( |
578 | 573 | 'projects_translations',
|
579 |
| - args=[project.slug], |
| 574 | + args=[self.get_project().slug], |
580 | 575 | )
|
581 |
| - return HttpResponseRedirect(project_dashboard) |
582 | 576 |
|
583 |
| - lang_projects = project.translations.all() |
584 | 577 |
|
585 |
| - return render( |
586 |
| - request, |
587 |
| - 'projects/project_translations.html', |
588 |
| - { |
589 |
| - 'form': form, |
590 |
| - 'project': project, |
591 |
| - 'lang_projects': lang_projects, |
592 |
| - }, |
593 |
| - ) |
| 578 | +class ProjectTranslations(ProjectTranslationsMixin, FormView): |
594 | 579 |
|
| 580 | + """Project translations view and form view.""" |
595 | 581 |
|
596 |
| -@login_required |
597 |
| -def project_translations_delete(request, project_slug, child_slug): |
598 |
| - project = get_object_or_404( |
599 |
| - Project.objects.for_admin_user(request.user), |
600 |
| - slug=project_slug, |
601 |
| - ) |
602 |
| - subproj = get_object_or_404( |
603 |
| - project.translations, |
604 |
| - slug=child_slug, |
605 |
| - ) |
606 |
| - project.translations.remove(subproj) |
607 |
| - project_dashboard = reverse('projects_translations', args=[project.slug]) |
608 |
| - return HttpResponseRedirect(project_dashboard) |
| 582 | + form_class = TranslationForm |
| 583 | + template_name = 'projects/project_translations.html' |
| 584 | + |
| 585 | + def form_valid(self, form): |
| 586 | + form.save() |
| 587 | + return HttpResponseRedirect(self.get_success_url()) |
| 588 | + |
| 589 | + def get_form(self, data=None, files=None, **kwargs): |
| 590 | + kwargs['parent'] = self.get_project() |
| 591 | + kwargs['user'] = self.request.user |
| 592 | + return self.form_class(data, files, **kwargs) |
| 593 | + |
| 594 | + def get_context_data(self, **kwargs): |
| 595 | + context = super().get_context_data(**kwargs) |
| 596 | + project = self.get_project() |
| 597 | + context['lang_projects'] = project.translations.all() |
| 598 | + return context |
| 599 | + |
| 600 | + |
| 601 | +class ProjectTranslationsDelete(ProjectTranslationsMixin, GenericView): |
| 602 | + |
| 603 | + http_method_names = ['get', 'post'] |
| 604 | + |
| 605 | + def get(self, request, *args, **kwargs): |
| 606 | + project = self.get_project() |
| 607 | + translation = self.get_translation() |
| 608 | + project.translations.remove(translation) |
| 609 | + return HttpResponseRedirect(self.get_success_url()) |
| 610 | + |
| 611 | + def post(self, request, *args, **kwargs): |
| 612 | + return self.get(request, *args, **kwargs) |
| 613 | + |
| 614 | + def get_translation(self): |
| 615 | + project = self.get_project() |
| 616 | + translation = get_object_or_404( |
| 617 | + project.translations, |
| 618 | + slug=self.kwargs['child_slug'], |
| 619 | + ) |
| 620 | + return translation |
609 | 621 |
|
610 | 622 |
|
611 | 623 | @login_required
|
|
0 commit comments