Skip to content

Commit 0dc78b0

Browse files
authored
Merge pull request #6183 from stsewd/refactor-project-notifications-views
Refactor ProjectNotications views
2 parents 307aad4 + 416849e commit 0dc78b0

File tree

2 files changed

+71
-49
lines changed

2 files changed

+71
-49
lines changed

readthedocs/projects/urls/private.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
ProjectAdvertisingUpdate,
2828
ProjectDashboard,
2929
ProjectDelete,
30+
ProjectNotications,
31+
ProjectNoticationsDelete,
3032
ProjectRedirects,
3133
ProjectRedirectsDelete,
3234
ProjectUpdate,
3335
ProjectUsersCreateList,
3436
ProjectUsersDelete,
3537
)
3638

37-
3839
urlpatterns = [
3940
url(r'^$', ProjectDashboard.as_view(), name='projects_dashboard'),
4041
url(
@@ -89,11 +90,13 @@
8990
),
9091
url(
9192
r'^(?P<project_slug>[-\w]+)/notifications/$',
92-
private.project_notifications, name='projects_notifications',
93+
ProjectNotications.as_view(),
94+
name='projects_notifications',
9395
),
9496
url(
9597
r'^(?P<project_slug>[-\w]+)/notifications/delete/$',
96-
private.project_notifications_delete, name='projects_notification_delete',
98+
ProjectNoticationsDelete.as_view(),
99+
name='projects_notification_delete',
97100
),
98101
url(
99102
r'^(?P<project_slug>[-\w]+)/translations/$',

readthedocs/projects/views/private.py

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -496,65 +496,84 @@ def post(self, request, *args, **kwargs):
496496
return HttpResponseRedirect(self.get_success_url())
497497

498498

499-
@login_required
500-
def project_notifications(request, project_slug):
499+
class ProjecNotificationsMixin(ProjectAdminMixin, PrivateViewMixin):
500+
501+
def get_success_url(self):
502+
return reverse(
503+
'projects_notifications',
504+
args=[self.get_project().slug],
505+
)
506+
507+
508+
class ProjectNotications(ProjecNotificationsMixin, TemplateView):
509+
501510
"""Project notification view and form view."""
502-
project = get_object_or_404(
503-
Project.objects.for_admin_user(request.user),
504-
slug=project_slug,
505-
)
506511

507-
email_form = EmailHookForm(data=None, project=project)
508-
webhook_form = WebHookForm(data=None, project=project)
512+
template_name = 'projects/project_notifications.html'
513+
email_form = EmailHookForm
514+
webhook_form = WebHookForm
509515

510-
if request.method == 'POST':
511-
if 'email' in request.POST.keys():
512-
email_form = EmailHookForm(data=request.POST, project=project)
516+
def get_email_form(self):
517+
project = self.get_project()
518+
return self.email_form(
519+
self.request.POST or None,
520+
project=project,
521+
)
522+
523+
def get_webhook_form(self):
524+
project = self.get_project()
525+
return self.webhook_form(
526+
self.request.POST or None,
527+
project=project,
528+
)
529+
530+
def post(self, request, *args, **kwargs):
531+
if 'email' in request.POST:
532+
email_form = self.get_email_form()
513533
if email_form.is_valid():
514534
email_form.save()
515-
elif 'url' in request.POST.keys():
516-
webhook_form = WebHookForm(data=request.POST, project=project)
535+
elif 'url' in request.POST:
536+
webhook_form = self.get_webhook_form()
517537
if webhook_form.is_valid():
518538
webhook_form.save()
539+
return HttpResponseRedirect(self.get_success_url())
519540

520-
emails = project.emailhook_notifications.all()
521-
urls = project.webhook_notifications.all()
541+
def get_context_data(self, **kwargs):
542+
context = super().get_context_data()
522543

523-
return render(
524-
request,
525-
'projects/project_notifications.html',
526-
{
527-
'email_form': email_form,
528-
'webhook_form': webhook_form,
529-
'project': project,
530-
'emails': emails,
531-
'urls': urls,
532-
},
533-
)
544+
project = self.get_project()
545+
emails = project.emailhook_notifications.all()
546+
urls = project.webhook_notifications.all()
534547

548+
context.update(
549+
{
550+
'email_form': self.get_email_form(),
551+
'webhook_form': self.get_webhook_form(),
552+
'emails': emails,
553+
'urls': urls,
554+
},
555+
)
556+
return context
535557

536-
@login_required
537-
def project_notifications_delete(request, project_slug):
538-
"""Project notifications delete confirmation view."""
539-
if request.method != 'POST':
540-
return HttpResponseNotAllowed('Only POST is allowed')
541-
project = get_object_or_404(
542-
Project.objects.for_admin_user(request.user),
543-
slug=project_slug,
544-
)
545-
try:
546-
project.emailhook_notifications.get(
547-
email=request.POST.get('email'),
548-
).delete()
549-
except EmailHook.DoesNotExist:
558+
559+
class ProjectNoticationsDelete(ProjecNotificationsMixin, GenericView):
560+
561+
http_method_names = ['post']
562+
563+
def post(self, request, *args, **kwargs):
564+
project = self.get_project()
550565
try:
551-
project.webhook_notifications.get(
552-
url=request.POST.get('email'),
566+
project.emailhook_notifications.get(
567+
email=request.POST.get('email'),
553568
).delete()
554-
except WebHook.DoesNotExist:
555-
raise Http404
556-
project_dashboard = reverse('projects_notifications', args=[project.slug])
557-
return HttpResponseRedirect(project_dashboard)
569+
except EmailHook.DoesNotExist:
570+
try:
571+
project.webhook_notifications.get(
572+
url=request.POST.get('email'),
573+
).delete()
574+
except WebHook.DoesNotExist:
575+
raise Http404
576+
return HttpResponseRedirect(self.get_success_url())
558577

559578

560579
@login_required

0 commit comments

Comments
 (0)