Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ProjectNotications views #6183

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions readthedocs/projects/urls/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
ProjectAdvertisingUpdate,
ProjectDashboard,
ProjectDelete,
ProjectNotications,
ProjectNoticationsDelete,
ProjectUpdate,
)

Expand Down Expand Up @@ -83,11 +85,13 @@
),
url(
r'^(?P<project_slug>[-\w]+)/notifications/$',
private.project_notifications, name='projects_notifications',
ProjectNotications.as_view(),
name='projects_notifications',
),
url(
r'^(?P<project_slug>[-\w]+)/notifications/delete/$',
private.project_notifications_delete, name='projects_notification_delete',
ProjectNoticationsDelete.as_view(),
name='projects_notification_delete',
),
url(
r'^(?P<project_slug>[-\w]+)/translations/$',
Expand Down
111 changes: 65 additions & 46 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,65 +488,84 @@ def project_users_delete(request, project_slug):
return HttpResponseRedirect(project_dashboard)


@login_required
def project_notifications(request, project_slug):
class ProjecNotificationsMixin(ProjectAdminMixin, PrivateViewMixin):

def get_success_url(self):
return reverse(
'projects_notifications',
args=[self.get_project().slug],
)


class ProjectNotications(ProjecNotificationsMixin, TemplateView):

"""Project notification view and form view."""
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)

email_form = EmailHookForm(data=None, project=project)
webhook_form = WebHookForm(data=None, project=project)
template_name = 'projects/project_notifications.html'
email_form = EmailHookForm
webhook_form = WebHookForm

def get_email_form(self):
project = self.get_project()
return self.email_form(
self.request.POST or None,
project=project,
)

if request.method == 'POST':
if 'email' in request.POST.keys():
email_form = EmailHookForm(data=request.POST, project=project)
def get_webhook_form(self):
project = self.get_project()
return self.webhook_form(
self.request.POST or None,
project=project,
)

def post(self, request, *args, **kwargs):
if 'email' in request.POST:
email_form = self.get_email_form()
if email_form.is_valid():
email_form.save()
elif 'url' in request.POST.keys():
webhook_form = WebHookForm(data=request.POST, project=project)
elif 'url' in request.POST:
webhook_form = self.get_webhook_form()
if webhook_form.is_valid():
webhook_form.save()
return HttpResponseRedirect(self.get_success_url())

emails = project.emailhook_notifications.all()
urls = project.webhook_notifications.all()
def get_context_data(self, **kwargs):
context = super().get_context_data()

return render(
request,
'projects/project_notifications.html',
{
'email_form': email_form,
'webhook_form': webhook_form,
'project': project,
'emails': emails,
'urls': urls,
},
)
project = self.get_project()
emails = project.emailhook_notifications.all()
urls = project.webhook_notifications.all()

context.update(
{
'email_form': self.get_email_form(),
'webhook_form': self.get_webhook_form(),
'emails': emails,
'urls': urls,
},
)
return context

@login_required
def project_notifications_delete(request, project_slug):
"""Project notifications delete confirmation view."""
if request.method != 'POST':
return HttpResponseNotAllowed('Only POST is allowed')
project = get_object_or_404(
Project.objects.for_admin_user(request.user),
slug=project_slug,
)
try:
project.emailhook_notifications.get(
email=request.POST.get('email'),
).delete()
except EmailHook.DoesNotExist:

class ProjectNoticationsDelete(ProjecNotificationsMixin, GenericView):

http_method_names = ['post']

def post(self, request, *args, **kwargs):
project = self.get_project()
try:
project.webhook_notifications.get(
url=request.POST.get('email'),
project.emailhook_notifications.get(
email=request.POST.get('email'),
).delete()
except WebHook.DoesNotExist:
raise Http404
project_dashboard = reverse('projects_notifications', args=[project.slug])
return HttpResponseRedirect(project_dashboard)
except EmailHook.DoesNotExist:
try:
project.webhook_notifications.get(
url=request.POST.get('email'),
).delete()
except WebHook.DoesNotExist:
raise Http404
return HttpResponseRedirect(self.get_success_url())


@login_required
Expand Down