pagekite: Simplify code for form adding custom service

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2020-03-11 12:55:00 -07:00 committed by Veiko Aasa
parent cd65c04719
commit 4156afa7cf
No known key found for this signature in database
GPG Key ID: 478539CAE680674E

View File

@ -2,7 +2,8 @@
from django.http import HttpResponseRedirect
from django.urls import reverse, reverse_lazy
from django.views.generic import TemplateView, View
from django.utils.translation import ugettext as _
from django.views.generic import View
from django.views.generic.edit import FormView
from plinth.modules import pagekite
@ -37,37 +38,22 @@ class DeleteServiceView(ContextMixin, View):
return HttpResponseRedirect(reverse('pagekite:index'))
class AddCustomServiceView(ContextMixin, TemplateView):
class AddCustomServiceView(FormView):
"""View to add a new custom PageKite service."""
form_class = AddCustomServiceForm
template_name = 'pagekite_custom_services.html'
success_url = reverse_lazy('pagekite:index')
def get_context_data(self, *args, **kwargs):
context = super(AddCustomServiceView,
self).get_context_data(*args, **kwargs)
unused, custom_services = utils.get_pagekite_services()
for service in custom_services:
service['form'] = AddCustomServiceForm(initial=service)
context['custom_services'] = [
utils.prepare_service_for_display(service)
for service in custom_services
]
context.update(utils.get_kite_details())
"""Return additional context for rendering the template."""
context = super().get_context_data(*args, **kwargs)
context['title'] = _('Add custom PageKite service')
return context
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
form = AddCustomServiceForm(prefix="custom")
context['form'] = form
return self.render_to_response(context)
def post(self, request):
form = AddCustomServiceForm(request.POST, prefix="custom")
if form.is_valid():
form.save(request)
form = AddCustomServiceForm(prefix="custom")
context = self.get_context_data()
context['form'] = form
return HttpResponseRedirect(reverse('pagekite:index'))
def form_valid(self, form):
"""Add a custom service."""
form.save(self.request)
return super().form_valid(form)
class ConfigurationView(ContextMixin, FormView):