|
7 | 7 | from django.conf import settings
|
8 | 8 | from django.contrib import messages
|
9 | 9 | from django.contrib.auth.decorators import login_required
|
10 |
| -from django.contrib.auth.models import User |
11 | 10 | from django.db.models import Count
|
12 | 11 | from django.http import (
|
13 | 12 | Http404,
|
|
24 | 23 | from django.utils.translation import ugettext_lazy as _
|
25 | 24 | from django.views.generic import ListView, TemplateView, View
|
26 | 25 | from formtools.wizard.views import SessionWizardView
|
27 |
| -from vanilla import CreateView, DeleteView, DetailView, GenericView, UpdateView |
| 26 | +from vanilla import ( |
| 27 | + CreateView, |
| 28 | + DeleteView, |
| 29 | + DetailView, |
| 30 | + FormView, |
| 31 | + GenericView, |
| 32 | + UpdateView, |
| 33 | +) |
28 | 34 |
|
29 | 35 | from readthedocs.builds.forms import VersionForm
|
30 | 36 | from readthedocs.builds.models import Version
|
@@ -455,47 +461,49 @@ class ProjectRelationshipDelete(ProjectRelationshipMixin, DeleteView):
|
455 | 461 | http_method_names = ['post']
|
456 | 462 |
|
457 | 463 |
|
458 |
| -@login_required |
459 |
| -def project_users(request, project_slug): |
460 |
| - """Project users view and form view.""" |
461 |
| - project = get_object_or_404( |
462 |
| - Project.objects.for_admin_user(request.user), |
463 |
| - slug=project_slug, |
464 |
| - ) |
| 464 | +class ProjectUsersMixin(ProjectAdminMixin, PrivateViewMixin): |
465 | 465 |
|
466 |
| - form = UserForm(data=request.POST or None, project=project) |
| 466 | + form_class = UserForm |
467 | 467 |
|
468 |
| - if request.method == 'POST' and form.is_valid(): |
| 468 | + def get_queryset(self): |
| 469 | + project = self.get_project() |
| 470 | + return project.users.all() |
| 471 | + |
| 472 | + def get_success_url(self): |
| 473 | + return reverse('projects_users', args=[self.get_project().slug]) |
| 474 | + |
| 475 | + |
| 476 | +class ProjectUsersCreateList(ProjectUsersMixin, FormView): |
| 477 | + |
| 478 | + template_name = 'projects/project_users.html' |
| 479 | + |
| 480 | + def form_valid(self, form): |
469 | 481 | form.save()
|
470 |
| - project_dashboard = reverse('projects_users', args=[project.slug]) |
471 |
| - return HttpResponseRedirect(project_dashboard) |
| 482 | + return HttpResponseRedirect(self.get_success_url()) |
472 | 483 |
|
473 |
| - users = project.users.all() |
| 484 | + def get_context_data(self, **kwargs): |
| 485 | + context = super().get_context_data(**kwargs) |
| 486 | + context['users'] = self.get_queryset() |
| 487 | + return context |
474 | 488 |
|
475 |
| - return render( |
476 |
| - request, |
477 |
| - 'projects/project_users.html', |
478 |
| - {'form': form, 'project': project, 'users': users}, |
479 |
| - ) |
480 | 489 |
|
| 490 | +class ProjectUsersDelete(ProjectUsersMixin, GenericView): |
481 | 491 |
|
482 |
| -@login_required |
483 |
| -def project_users_delete(request, project_slug): |
484 |
| - if request.method != 'POST': |
485 |
| - return HttpResponseNotAllowed('Only POST is allowed') |
486 |
| - project = get_object_or_404( |
487 |
| - Project.objects.for_admin_user(request.user), |
488 |
| - slug=project_slug, |
489 |
| - ) |
490 |
| - user = get_object_or_404( |
491 |
| - User.objects.all(), |
492 |
| - username=request.POST.get('username'), |
493 |
| - ) |
494 |
| - if user == request.user: |
495 |
| - raise Http404 |
496 |
| - project.users.remove(user) |
497 |
| - project_dashboard = reverse('projects_users', args=[project.slug]) |
498 |
| - return HttpResponseRedirect(project_dashboard) |
| 492 | + http_method_names = ['post'] |
| 493 | + |
| 494 | + def post(self, request, *args, **kwargs): |
| 495 | + username = self.request.POST.get('username') |
| 496 | + user = get_object_or_404( |
| 497 | + self.get_queryset(), |
| 498 | + username=username, |
| 499 | + ) |
| 500 | + if user == request.user: |
| 501 | + raise Http404 |
| 502 | + |
| 503 | + project = self.get_project() |
| 504 | + project.users.remove(user) |
| 505 | + |
| 506 | + return HttpResponseRedirect(self.get_success_url()) |
499 | 507 |
|
500 | 508 |
|
501 | 509 | @login_required
|
|
0 commit comments