@@ -498,65 +498,84 @@ def project_users_delete(request, project_slug):
498
498
return HttpResponseRedirect (project_dashboard )
499
499
500
500
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
+
503
512
"""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
- )
508
513
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
511
517
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 ()
515
535
if email_form .is_valid ():
516
536
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 ( )
519
539
if webhook_form .is_valid ():
520
540
webhook_form .save ()
541
+ return HttpResponseRedirect (self .get_success_url ())
521
542
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 ()
524
545
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 ()
536
549
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
537
559
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 ()
552
567
try :
553
- project .webhook_notifications .get (
554
- url = request .POST .get ('email' ),
568
+ project .emailhook_notifications .get (
569
+ email = request .POST .get ('email' ),
555
570
).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 ())
560
579
561
580
562
581
@login_required
0 commit comments