|
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,
|
@@ -452,47 +451,49 @@ class ProjectRelationshipDelete(ProjectRelationshipMixin, DeleteView):
|
452 | 451 | http_method_names = ['post']
|
453 | 452 |
|
454 | 453 |
|
455 |
| -@login_required |
456 |
| -def project_users(request, project_slug): |
457 |
| - """Project users view and form view.""" |
458 |
| - project = get_object_or_404( |
459 |
| - Project.objects.for_admin_user(request.user), |
460 |
| - slug=project_slug, |
461 |
| - ) |
| 454 | +class ProjectUsersMixin(ProjectAdminMixin, PrivateViewMixin): |
462 | 455 |
|
463 |
| - form = UserForm(data=request.POST or None, project=project) |
| 456 | + form_class = UserForm |
464 | 457 |
|
465 |
| - if request.method == 'POST' and form.is_valid(): |
| 458 | + def get_queryset(self): |
| 459 | + project = self.get_project() |
| 460 | + return project.users.all() |
| 461 | + |
| 462 | + def get_success_url(self): |
| 463 | + return reverse('projects_users', args=[self.get_project().slug]) |
| 464 | + |
| 465 | + |
| 466 | +class ProjectUsersCreateList(ProjectUsersMixin, FormView): |
| 467 | + |
| 468 | + template_name = 'projects/project_users.html' |
| 469 | + |
| 470 | + def form_valid(self, form): |
466 | 471 | form.save()
|
467 |
| - project_dashboard = reverse('projects_users', args=[project.slug]) |
468 |
| - return HttpResponseRedirect(project_dashboard) |
| 472 | + return HttpResponseRedirect(self.get_success_url()) |
469 | 473 |
|
470 |
| - users = project.users.all() |
| 474 | + def get_context_data(self, **kwargs): |
| 475 | + context = super().get_context_data(**kwargs) |
| 476 | + context['users'] = self.get_queryset() |
| 477 | + return context |
471 | 478 |
|
472 |
| - return render( |
473 |
| - request, |
474 |
| - 'projects/project_users.html', |
475 |
| - {'form': form, 'project': project, 'users': users}, |
476 |
| - ) |
477 | 479 |
|
| 480 | +class ProjectUsersDelete(ProjectUsersMixin, GenericView): |
478 | 481 |
|
479 |
| -@login_required |
480 |
| -def project_users_delete(request, project_slug): |
481 |
| - if request.method != 'POST': |
482 |
| - return HttpResponseNotAllowed('Only POST is allowed') |
483 |
| - project = get_object_or_404( |
484 |
| - Project.objects.for_admin_user(request.user), |
485 |
| - slug=project_slug, |
486 |
| - ) |
487 |
| - user = get_object_or_404( |
488 |
| - User.objects.all(), |
489 |
| - username=request.POST.get('username'), |
490 |
| - ) |
491 |
| - if user == request.user: |
492 |
| - raise Http404 |
493 |
| - project.users.remove(user) |
494 |
| - project_dashboard = reverse('projects_users', args=[project.slug]) |
495 |
| - return HttpResponseRedirect(project_dashboard) |
| 482 | + http_method_names = ['post'] |
| 483 | + |
| 484 | + def post(self, request, *args, **kwargs): |
| 485 | + username = self.request.POST.get('username') |
| 486 | + user = get_object_or_404( |
| 487 | + self.get_queryset(), |
| 488 | + username=username, |
| 489 | + ) |
| 490 | + if user == request.user: |
| 491 | + raise Http404 |
| 492 | + |
| 493 | + project = self.get_project() |
| 494 | + project.users.remove(user) |
| 495 | + |
| 496 | + return HttpResponseRedirect(self.get_success_url()) |
496 | 497 |
|
497 | 498 |
|
498 | 499 | @login_required
|
|
0 commit comments