Skip to content

Refactor views ProjectUsers #6178

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 1 commit into from
Oct 2, 2019
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: 6 additions & 2 deletions readthedocs/projects/urls/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
ProjectAdvertisingUpdate,
ProjectDashboard,
ProjectUpdate,
ProjectUsersCreateList,
ProjectUsersDelete,
)


Expand Down Expand Up @@ -69,12 +71,14 @@
name='projects_delete',
),
url(
r'^(?P<project_slug>[-\w]+)/users/$', private.project_users,
r'^(?P<project_slug>[-\w]+)/users/$',
ProjectUsersCreateList.as_view(),
name='projects_users',
),
url(
r'^(?P<project_slug>[-\w]+)/users/delete/$',
private.project_users_delete, name='projects_users_delete',
ProjectUsersDelete.as_view(),
name='projects_users_delete',
),
url(
r'^(?P<project_slug>[-\w]+)/notifications/$',
Expand Down
80 changes: 44 additions & 36 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db.models import Count
from django.http import (
Http404,
Expand All @@ -24,7 +23,14 @@
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView, TemplateView, View
from formtools.wizard.views import SessionWizardView
from vanilla import CreateView, DeleteView, DetailView, GenericView, UpdateView
from vanilla import (
CreateView,
DeleteView,
DetailView,
FormView,
GenericView,
UpdateView,
)

from readthedocs.builds.forms import VersionForm
from readthedocs.builds.models import Version
Expand Down Expand Up @@ -455,47 +461,49 @@ class ProjectRelationshipDelete(ProjectRelationshipMixin, DeleteView):
http_method_names = ['post']


@login_required
def project_users(request, project_slug):
"""Project users view and form view."""
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)
class ProjectUsersMixin(ProjectAdminMixin, PrivateViewMixin):

form = UserForm(data=request.POST or None, project=project)
form_class = UserForm

if request.method == 'POST' and form.is_valid():
def get_queryset(self):
project = self.get_project()
return project.users.all()

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


class ProjectUsersCreateList(ProjectUsersMixin, FormView):
Copy link
Member

Choose a reason for hiding this comment

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

Why we don't use CreateView for these?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because this view does two things, list and create. CreatView makes this a model view, we don't want that here.


template_name = 'projects/project_users.html'

def form_valid(self, form):
form.save()
project_dashboard = reverse('projects_users', args=[project.slug])
return HttpResponseRedirect(project_dashboard)
return HttpResponseRedirect(self.get_success_url())

users = project.users.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['users'] = self.get_queryset()
return context

return render(
request,
'projects/project_users.html',
{'form': form, 'project': project, 'users': users},
)

class ProjectUsersDelete(ProjectUsersMixin, GenericView):

@login_required
def project_users_delete(request, project_slug):
if request.method != 'POST':
return HttpResponseNotAllowed('Only POST is allowed')
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)
user = get_object_or_404(
User.objects.all(),
username=request.POST.get('username'),
)
if user == request.user:
raise Http404
project.users.remove(user)
project_dashboard = reverse('projects_users', args=[project.slug])
return HttpResponseRedirect(project_dashboard)
http_method_names = ['post']

def post(self, request, *args, **kwargs):
username = self.request.POST.get('username')
user = get_object_or_404(
self.get_queryset(),
username=username,
)
if user == request.user:
raise Http404

project = self.get_project()
project.users.remove(user)

return HttpResponseRedirect(self.get_success_url())


@login_required
Expand Down