Skip to content

Refactor views projects #6171

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

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion readthedocs/projects/urls/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ProjectAdvancedUpdate,
ProjectAdvertisingUpdate,
ProjectDashboard,
ProjectDelete,
ProjectUpdate,
)

Expand Down Expand Up @@ -65,7 +66,8 @@
private.project_version_detail, name='project_version_detail',
),
url(
r'^(?P<project_slug>[-\w]+)/delete/$', private.project_delete,
r'^(?P<project_slug>[-\w]+)/delete/$',
ProjectDelete.as_view(),
name='projects_delete',
),
url(
Expand Down
65 changes: 26 additions & 39 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,53 @@ def get_context_data(self, **kwargs):
return context


class ProjectUpdate(ProjectSpamMixin, PrivateViewMixin, UpdateView):
class ProjectMixin(PrivateViewMixin):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just with the name of the class, I'm not 100% sure if all the Project views need to inherit from this class or not. I guess, yes. Although, it may be good to reflect what the mixin is about in the name of the class, or if that's tricky, in a docstring.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of them inherit this mixin but ProjectDasboard, because it's very different, it doesn't share anything in common.


form_class = UpdateProjectForm
model = Project
success_message = _('Project settings updated')
template_name = 'projects/project_edit.html'
lookup_url_kwarg = 'project_slug'
lookup_field = 'slug'
context_object_name = 'project'

def get_queryset(self):
return self.model.objects.for_admin_user(self.request.user)


class ProjectUpdate(ProjectSpamMixin, ProjectMixin, UpdateView):

form_class = UpdateProjectForm
success_message = _('Project settings updated')
template_name = 'projects/project_edit.html'

def get_success_url(self):
return reverse('projects_detail', args=[self.object.slug])


class ProjectAdvancedUpdate(ProjectSpamMixin, PrivateViewMixin, UpdateView):
class ProjectAdvancedUpdate(ProjectSpamMixin, ProjectMixin, UpdateView):

form_class = ProjectAdvancedForm
model = Project
success_message = _('Project settings updated')
template_name = 'projects/project_advanced.html'
lookup_url_kwarg = 'project_slug'
lookup_field = 'slug'

def get_queryset(self):
return self.model.objects.for_admin_user(self.request.user)

def get_success_url(self):
return reverse('projects_detail', args=[self.object.slug])


class ProjectDelete(ProjectMixin, DeleteView):

success_message = _('Project deleted')
template_name = 'projects/project_delete.html'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['is_superproject'] = (
self.object.subprojects.all().exists()
)
return context

def get_success_url(self):
return reverse('projects_dashboard')


@login_required
def project_version_detail(request, project_slug, version_slug):
"""Project version detail page."""
Expand Down Expand Up @@ -183,34 +198,6 @@ def project_version_detail(request, project_slug, version_slug):
)


@login_required
def project_delete(request, project_slug):
"""
Project delete confirmation view.

Make a project as deleted on POST, otherwise show a form asking for
confirmation of delete.
"""
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)

context = {
'project': project,
'is_superproject': project.subprojects.all().exists()
}

if request.method == 'POST':
# Delete the project and all related files
project.delete()
messages.success(request, _('Project deleted'))
project_dashboard = reverse('projects_dashboard')
return HttpResponseRedirect(project_dashboard)

return render(request, 'projects/project_delete.html', context)


class ImportWizardView(
ProjectImportMixin, ProjectSpamMixin, PrivateViewMixin,
SessionWizardView,
Expand Down