Moved pagekit url creation to pagekite module

plus some minor cleanup
This commit is contained in:
fonfon 2015-01-21 12:59:47 +00:00
parent 8955e41f15
commit c04897be8b
6 changed files with 70 additions and 44 deletions

View File

@ -223,10 +223,10 @@ def subcommand_add_service(arguments):
root = get_new_service_path(params['protocol'])
# TODO: after adding a service, augeas fails writing the config;
# so do it manually here
# so add the service_on entry manually instead
path = convert_augeas_path_to_filepath(root)
with open(path, 'a') as servicefile:
line = "service_on = %s" % deconstruct_params(params)
line = "\nservice_on = %s\n" % deconstruct_params(params)
servicefile.write(line)

View File

@ -20,7 +20,7 @@
{% load bootstrap %}
{% load plinth_extras %}
{% load pagekite_extras %}
{% block page_head %}
<style type="text/css">
@ -51,14 +51,14 @@
{% endif %}
<div class="list-group">
{% for service in custom_services %}
{% create_pagekite_service_link service kite_name as service_link %}
{% create_pagekite_service_url service kite_name as service_url %}
<div class="list-group-item clearfix">
<span class="service">
<span title="Forwards {{ service_link }} to {{ service.backend_host }}:{{ service.backend_port }}">
{% if service_link|slice:":4" == "http" %}
<a href="{{ service_link }}">{{ service_link }}</a>
<span title="Connects {{ service_url }} to {{ service.backend_host }}:{{ service.backend_port }}">
{% if service_url|slice:":4" == "http" %}
<a href="{{ service_url }}">{{ service_url }}</a>
{% else %}
{{ service_link }}
{{ service_url }}
{% endif %}
</span>
</span>

View File

@ -0,0 +1,44 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from django import template
from plinth.modules.pagekite.util import prepare_service_for_display
register = template.Library()
@register.assignment_tag
def create_pagekite_service_url(service, kite_name):
"""Create a URL out of a pagekite service
Parameters: - service: the service params dictionary
- kite_name: kite name from the pagekite configuration, not
from the service params
"""
# add extra information if it's missing
if 'subdomains' not in service:
service = prepare_service_for_display(service)
urlparams = {'protocol': service['protocol']}
if service['subdomains']:
urlparams['kite_name'] = "*.%s" % kite_name
else:
urlparams['kite_name'] = kite_name
url = "{protocol}://{kite_name}".format(**urlparams)
if 'frontend_port' in service and service['frontend_port']:
url = "%s:%s" % (url, service['frontend_port'])
return url

View File

@ -71,19 +71,6 @@ def get_kite_details():
'kite_secret': kite_details[1]}
def prepare_params_for_display(params):
"""Add extra information to display a custom service:
- protocol is split into 'protocol' and 'frontend_port'
- we try to detect whether 'subdomains' are supported (as boolean)
"""
protocol = params['protocol']
if '/' in protocol:
params['protocol'], params['frontend_port'] = protocol.split('/')
params['subdomains'] = params['kitename'].startswith('*.')
return params
def get_pagekite_config():
"""
Return the current PageKite configuration by executing various actions.
@ -127,10 +114,23 @@ def get_pagekite_services():
predefined[name] = True
break
else:
custom.append(prepare_params_for_display(params))
custom.append(params)
return predefined, custom
def prepare_service_for_display(service):
""" Add extra information that is used when displaying a service
- protocol is split into 'protocol' and 'frontend_port'
- detect whether 'subdomains' are supported (as boolean)
"""
protocol = service['protocol']
if '/' in protocol:
service['protocol'], service['frontend_port'] = protocol.split('/')
service['subdomains'] = service['kitename'].startswith('*.')
return service
def _run(arguments, superuser=True):
"""Run a given command and raise exception if there was an error"""
command = 'pagekite'

View File

@ -25,7 +25,8 @@ from django.views.generic import View, TemplateView
from django.views.generic.edit import FormView
from plinth import package
from .util import get_pagekite_config, get_pagekite_services, get_kite_details
from .util import get_pagekite_config, get_pagekite_services, \
get_kite_details, prepare_service_for_display
from .forms import ConfigurationForm, DefaultServiceForm, CustomServiceForm
subsubmenu = [{'url': reverse_lazy('pagekite:index'),
@ -80,7 +81,8 @@ class CustomServiceView(ContextMixin, TemplateView):
unused, custom_services = get_pagekite_services()
for service in custom_services:
service['form'] = CustomServiceForm(initial=service)
context['custom_services'] = custom_services
context['custom_services'] = [prepare_service_for_display(service)
for service in custom_services]
context.update(get_kite_details())
return context
@ -91,7 +93,6 @@ class CustomServiceView(ContextMixin, TemplateView):
return self.render_to_response(context)
def post(self, request):
unused, custom_services = get_pagekite_services()
form = CustomServiceForm(request.POST, prefix="custom")
if form.is_valid():
form.save(request)

View File

@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import copy
import os
from django import template
@ -59,21 +58,3 @@ def show_subsubmenu(context, menu):
"""Mark the active menu item and display the subsubmenu"""
menu = mark_active_menuitem(menu, context['request'].path)
return {'subsubmenu': menu}
@register.assignment_tag
def create_pagekite_service_link(service, kite_name):
"""Create a link (URL) out of a pagekite service
Parameters: - service: the params dictionary
- kite_name: kite name (from the pagekite configuration)
"""
params = {'protocol': service['protocol']}
if 'subdomains' in service and service['subdomains']:
params['kite_name'] = "*.%s" % kite_name
else:
params['kite_name'] = kite_name
link = "{protocol}://{kite_name}".format(**params)
if 'frontend_port' in service and service['frontend_port']:
link = "%s:%s" % (link, service['frontend_port'])
return link