mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
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:
parent
cd65c04719
commit
4156afa7cf
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.urls import reverse, reverse_lazy
|
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 django.views.generic.edit import FormView
|
||||||
|
|
||||||
from plinth.modules import pagekite
|
from plinth.modules import pagekite
|
||||||
@ -37,37 +38,22 @@ class DeleteServiceView(ContextMixin, View):
|
|||||||
return HttpResponseRedirect(reverse('pagekite:index'))
|
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'
|
template_name = 'pagekite_custom_services.html'
|
||||||
|
success_url = reverse_lazy('pagekite:index')
|
||||||
|
|
||||||
def get_context_data(self, *args, **kwargs):
|
def get_context_data(self, *args, **kwargs):
|
||||||
context = super(AddCustomServiceView,
|
"""Return additional context for rendering the template."""
|
||||||
self).get_context_data(*args, **kwargs)
|
context = super().get_context_data(*args, **kwargs)
|
||||||
unused, custom_services = utils.get_pagekite_services()
|
context['title'] = _('Add custom PageKite service')
|
||||||
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 context
|
return context
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def form_valid(self, form):
|
||||||
context = self.get_context_data(**kwargs)
|
"""Add a custom service."""
|
||||||
form = AddCustomServiceForm(prefix="custom")
|
form.save(self.request)
|
||||||
context['form'] = form
|
return super().form_valid(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'))
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationView(ContextMixin, FormView):
|
class ConfigurationView(ContextMixin, FormView):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user