|
1 | 1 | from django.contrib import messages |
| 2 | +from django.contrib.auth.mixins import UserPassesTestMixin |
| 3 | + |
2 | 4 | from django.views.generic import CreateView, UpdateView, DetailView, ListView |
3 | 5 | from django.urls import reverse |
4 | 6 | from django.http import Http404 |
5 | 7 |
|
6 | 8 | from pydotorg.mixins import LoginRequiredMixin |
7 | 9 |
|
8 | 10 | from .models import Nomination, Nominee, Election |
9 | | -from .forms import NominationForm, NominationCreateForm |
| 11 | +from .forms import NominationForm, NominationCreateForm, NominationAcceptForm |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class ElectionsList(ListView): |
@@ -119,10 +121,40 @@ def get_context_data(self, **kwargs): |
119 | 121 | return context |
120 | 122 |
|
121 | 123 |
|
122 | | -class NominationEdit(LoginRequiredMixin, NominationMixin, UpdateView): |
| 124 | +class NominationEdit(LoginRequiredMixin, NominationMixin, UserPassesTestMixin, UpdateView): |
123 | 125 | model = Nomination |
124 | 126 | form_class = NominationForm |
125 | 127 |
|
| 128 | + def test_func(self): |
| 129 | + return self.request.user == self.get_object().nominator |
| 130 | + |
| 131 | + def get_success_url(self): |
| 132 | + next_url = self.request.POST.get("next") |
| 133 | + if next_url: |
| 134 | + return next_url |
| 135 | + |
| 136 | + elif self.object.pk: |
| 137 | + return reverse( |
| 138 | + "nominations:nomination_detail", |
| 139 | + kwargs={"election": self.object.election.slug, "pk": self.object.id}, |
| 140 | + ) |
| 141 | + |
| 142 | + else: |
| 143 | + return super().get_success_url() |
| 144 | + |
| 145 | + def get_context_data(self, **kwargs): |
| 146 | + context = super().get_context_data(**kwargs) |
| 147 | + return context |
| 148 | + |
| 149 | + |
| 150 | +class NominationAccept(LoginRequiredMixin, NominationMixin, UserPassesTestMixin, UpdateView): |
| 151 | + model = Nomination |
| 152 | + form_class = NominationAcceptForm |
| 153 | + template_name_suffix = '_accept_form' |
| 154 | + |
| 155 | + def test_func(self): |
| 156 | + return self.request.user == self.get_object().nominee.user |
| 157 | + |
126 | 158 | def get_success_url(self): |
127 | 159 | next_url = self.request.POST.get("next") |
128 | 160 | if next_url: |
|
0 commit comments