@@ -496,65 +496,84 @@ def post(self, request, *args, **kwargs):
496
496
return HttpResponseRedirect (self .get_success_url ())
497
497
498
498
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
+
501
510
"""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
- )
506
511
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
509
515
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 ()
513
533
if email_form .is_valid ():
514
534
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 ( )
517
537
if webhook_form .is_valid ():
518
538
webhook_form .save ()
539
+ return HttpResponseRedirect (self .get_success_url ())
519
540
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 ()
522
543
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 ()
534
547
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
535
557
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 ()
550
565
try :
551
- project .webhook_notifications .get (
552
- url = request .POST .get ('email' ),
566
+ project .emailhook_notifications .get (
567
+ email = request .POST .get ('email' ),
553
568
).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 ())
558
577
559
578
560
579
@login_required
0 commit comments