Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ProjectTranslations views #6185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions readthedocs/projects/urls/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
ProjectDelete,
ProjectRedirects,
ProjectRedirectsDelete,
ProjectTranslationsDelete,
ProjectTranslationsListAndCreate,
ProjectUpdate,
ProjectUsersCreateList,
ProjectUsersDelete,
)


urlpatterns = [
url(r'^$', ProjectDashboard.as_view(), name='projects_dashboard'),
url(
Expand Down Expand Up @@ -97,11 +98,12 @@
),
url(
r'^(?P<project_slug>[-\w]+)/translations/$',
private.project_translations, name='projects_translations',
ProjectTranslationsListAndCreate.as_view(),
name='projects_translations',
),
url(
r'^(?P<project_slug>[-\w]+)/translations/delete/(?P<child_slug>[-\w]+)/$', # noqa
private.project_translations_delete,
ProjectTranslationsDelete.as_view(),
name='projects_translations_delete',
),
url(
Expand Down
85 changes: 45 additions & 40 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,53 +557,58 @@ def project_notifications_delete(request, project_slug):
return HttpResponseRedirect(project_dashboard)


@login_required
def project_translations(request, project_slug):
"""Project translations view and form view."""
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)
form = TranslationForm(
data=request.POST or None,
parent=project,
user=request.user,
)
class ProjectTranslationsMixin(ProjectAdminMixin, PrivateViewMixin):

if request.method == 'POST' and form.is_valid():
form.save()
project_dashboard = reverse(
def get_success_url(self):
return reverse(
'projects_translations',
args=[project.slug],
args=[self.get_project().slug],
)
return HttpResponseRedirect(project_dashboard)

lang_projects = project.translations.all()

return render(
request,
'projects/project_translations.html',
{
'form': form,
'project': project,
'lang_projects': lang_projects,
},
)
class ProjectTranslationsListAndCreate(ProjectTranslationsMixin, FormView):

"""Project translations view and form view."""

@login_required
def project_translations_delete(request, project_slug, child_slug):
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)
subproj = get_object_or_404(
project.translations,
slug=child_slug,
)
project.translations.remove(subproj)
project_dashboard = reverse('projects_translations', args=[project.slug])
return HttpResponseRedirect(project_dashboard)
form_class = TranslationForm
template_name = 'projects/project_translations.html'

def form_valid(self, form):
form.save()
return HttpResponseRedirect(self.get_success_url())

def get_form(self, data=None, files=None, **kwargs):
kwargs['parent'] = self.get_project()
kwargs['user'] = self.request.user
return self.form_class(data, files, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
project = self.get_project()
context['lang_projects'] = project.translations.all()
return context


class ProjectTranslationsDelete(ProjectTranslationsMixin, GenericView):

http_method_names = ['get', 'post']
stsewd marked this conversation as resolved.
Show resolved Hide resolved

def get(self, request, *args, **kwargs):
project = self.get_project()
translation = self.get_translation(kwargs['child_slug'])
project.translations.remove(translation)
return HttpResponseRedirect(self.get_success_url())

def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)

def get_translation(self, slug):
project = self.get_project()
translation = get_object_or_404(
project.translations,
slug=slug,
)
return translation


class ProjectRedirectsMixin(ProjectAdminMixin, PrivateViewMixin):
Expand Down