|
10 | 10 | from django.shortcuts import get_object_or_404, render
|
11 | 11 | from django.urls import reverse, reverse_lazy
|
12 | 12 | from django.utils.translation import ugettext_lazy as _
|
13 |
| -from vanilla import DeleteView, DetailView, UpdateView |
| 13 | +from vanilla import DeleteView, DetailView, FormView, GenericView, UpdateView |
14 | 14 |
|
15 | 15 | from readthedocs.core.mixins import PrivateViewMixin
|
16 | 16 | from readthedocs.payments.mixins import StripeMixin
|
@@ -98,45 +98,57 @@ def post(self, request, *args, **kwargs):
|
98 | 98 | return resp
|
99 | 99 |
|
100 | 100 |
|
101 |
| -@login_required |
102 |
| -def projects(request): |
103 |
| - gold_user = get_object_or_404(GoldUser, user=request.user) |
104 |
| - gold_projects = gold_user.projects.all() |
| 101 | +class GoldProjectsMixin(PrivateViewMixin): |
105 | 102 |
|
106 |
| - if request.method == 'POST': |
107 |
| - form = GoldProjectForm( |
108 |
| - active_user=request.user, |
109 |
| - data=request.POST, |
110 |
| - user=gold_user, |
111 |
| - projects=gold_projects, |
| 103 | + def get_gold_user(self): |
| 104 | + return get_object_or_404(GoldUser, user=self.request.user) |
| 105 | + |
| 106 | + def get_gold_projects(self): |
| 107 | + return self.get_gold_user().projects.all() |
| 108 | + |
| 109 | + def get_success_url(self): |
| 110 | + return reverse('gold_projects') |
| 111 | + |
| 112 | + |
| 113 | +class GoldProjectsListCreate(GoldProjectsMixin, FormView): |
| 114 | + |
| 115 | + """Gold Project list view and form view.""" |
| 116 | + |
| 117 | + form_class = GoldProjectForm |
| 118 | + template_name = 'gold/projects.html' |
| 119 | + |
| 120 | + def form_valid(self, form): |
| 121 | + to_add = Project.objects.get(slug=form.cleaned_data['project']) |
| 122 | + gold_user = self.get_gold_user() |
| 123 | + gold_user.projects.add(to_add) |
| 124 | + return HttpResponseRedirect(self.get_success_url()) |
| 125 | + |
| 126 | + def get_form(self, data=None, files=None, **kwargs): |
| 127 | + kwargs['user'] = self.get_gold_user() |
| 128 | + kwargs['projects'] = self.get_gold_projects() |
| 129 | + return self.form_class(self.request.user, data, files, **kwargs) |
| 130 | + |
| 131 | + def get_context_data(self, **kwargs): |
| 132 | + context = super().get_context_data(**kwargs) |
| 133 | + context['gold_user'] = self.get_gold_user() |
| 134 | + context['publishable'] = settings.STRIPE_PUBLISHABLE |
| 135 | + context['user'] = self.request.user |
| 136 | + context['projects'] = self.get_gold_projects() |
| 137 | + return context |
| 138 | + |
| 139 | + |
| 140 | +class GoldProjectRemove(GoldProjectsMixin, GenericView): |
| 141 | + |
| 142 | + http_method_names = ['post'] |
| 143 | + |
| 144 | + def post(self, request, *args, **kwargs): |
| 145 | + # pylint: disable=unused-argument |
| 146 | + gold_user = self.get_gold_user() |
| 147 | + |
| 148 | + project = get_object_or_404( |
| 149 | + Project.objects.all(), |
| 150 | + slug=self.kwargs.get('project_slug') |
112 | 151 | )
|
113 |
| - if form.is_valid(): |
114 |
| - to_add = Project.objects.get(slug=form.cleaned_data['project']) |
115 |
| - gold_user.projects.add(to_add) |
116 |
| - return HttpResponseRedirect(reverse('gold_projects')) |
117 |
| - else: |
118 |
| - # HACK: active_user=request.user is passed |
119 |
| - # as argument to get the currently active |
120 |
| - # user in the GoldProjectForm which is used |
121 |
| - # to filter the choices based on the user. |
122 |
| - form = GoldProjectForm(active_user=request.user) |
123 |
| - |
124 |
| - return render( |
125 |
| - request, |
126 |
| - 'gold/projects.html', |
127 |
| - { |
128 |
| - 'form': form, |
129 |
| - 'gold_user': gold_user, |
130 |
| - 'publishable': settings.STRIPE_PUBLISHABLE, |
131 |
| - 'user': request.user, |
132 |
| - 'projects': gold_projects, |
133 |
| - }, |
134 |
| - ) |
135 |
| - |
136 |
| - |
137 |
| -@login_required |
138 |
| -def projects_remove(request, project_slug): |
139 |
| - gold_user = get_object_or_404(GoldUser, user=request.user) |
140 |
| - project = get_object_or_404(Project.objects.all(), slug=project_slug) |
141 |
| - gold_user.projects.remove(project) |
142 |
| - return HttpResponseRedirect(reverse('gold_projects')) |
| 152 | + gold_user.projects.remove(project) |
| 153 | + |
| 154 | + return HttpResponseRedirect(self.get_success_url()) |
0 commit comments