Skip to content

Commit 2723dc8

Browse files
committed
Refactor ProjectNotications views
Ref readthedocs#5856
1 parent 044f200 commit 2723dc8

File tree

2 files changed

+71
-48
lines changed

2 files changed

+71
-48
lines changed

readthedocs/projects/urls/private.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
ProjectAdvancedUpdate,
2727
ProjectAdvertisingUpdate,
2828
ProjectDashboard,
29+
ProjectNotications,
30+
ProjectNoticationsDelete,
2931
ProjectUpdate,
3032
)
3133

@@ -81,11 +83,13 @@
8183
),
8284
url(
8385
r'^(?P<project_slug>[-\w]+)/notifications/$',
84-
private.project_notifications, name='projects_notifications',
86+
ProjectNotications.as_view(),
87+
name='projects_notifications',
8588
),
8689
url(
8790
r'^(?P<project_slug>[-\w]+)/notifications/delete/$',
88-
private.project_notifications_delete, name='projects_notification_delete',
91+
ProjectNoticationsDelete.as_view(),
92+
name='projects_notification_delete',
8993
),
9094
url(
9195
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
@@ -498,65 +498,84 @@ def project_users_delete(request, project_slug):
498498
return HttpResponseRedirect(project_dashboard)
499499

500500

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

509-
email_form = EmailHookForm(data=None, project=project)
510-
webhook_form = WebHookForm(data=None, project=project)
514+
template_name = 'projects/project_notifications.html'
515+
email_form = EmailHookForm
516+
webhook_form = WebHookForm
511517

512-
if request.method == 'POST':
513-
if 'email' in request.POST.keys():
514-
email_form = EmailHookForm(data=request.POST, project=project)
518+
def get_email_form(self):
519+
project = self.get_project()
520+
return self.email_form(
521+
self.request.POST or None,
522+
project=project,
523+
)
524+
525+
def get_webhook_form(self):
526+
project = self.get_project()
527+
return self.webhook_form(
528+
self.request.POST or None,
529+
project=project,
530+
)
531+
532+
def post(self, request, *args, **kwargs):
533+
if 'email' in request.POST:
534+
email_form = self.get_email_form()
515535
if email_form.is_valid():
516536
email_form.save()
517-
elif 'url' in request.POST.keys():
518-
webhook_form = WebHookForm(data=request.POST, project=project)
537+
elif 'url' in request.POST:
538+
webhook_form = self.get_webhook_form()
519539
if webhook_form.is_valid():
520540
webhook_form.save()
541+
return HttpResponseRedirect(self.get_success_url())
521542

522-
emails = project.emailhook_notifications.all()
523-
urls = project.webhook_notifications.all()
543+
def get_context_data(self, **kwargs):
544+
context = super().get_context_data()
524545

525-
return render(
526-
request,
527-
'projects/project_notifications.html',
528-
{
529-
'email_form': email_form,
530-
'webhook_form': webhook_form,
531-
'project': project,
532-
'emails': emails,
533-
'urls': urls,
534-
},
535-
)
546+
project = self.get_project()
547+
emails = project.emailhook_notifications.all()
548+
urls = project.webhook_notifications.all()
536549

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

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

561580

562581
@login_required

0 commit comments

Comments
 (0)