mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-29 10:10:19 +00:00
Moved pagekit url creation to pagekite module
plus some minor cleanup
This commit is contained in:
parent
8955e41f15
commit
c04897be8b
@ -223,10 +223,10 @@ def subcommand_add_service(arguments):
|
|||||||
|
|
||||||
root = get_new_service_path(params['protocol'])
|
root = get_new_service_path(params['protocol'])
|
||||||
# TODO: after adding a service, augeas fails writing the config;
|
# 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)
|
path = convert_augeas_path_to_filepath(root)
|
||||||
with open(path, 'a') as servicefile:
|
with open(path, 'a') as servicefile:
|
||||||
line = "service_on = %s" % deconstruct_params(params)
|
line = "\nservice_on = %s\n" % deconstruct_params(params)
|
||||||
servicefile.write(line)
|
servicefile.write(line)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
{% load bootstrap %}
|
{% load bootstrap %}
|
||||||
|
|
||||||
{% load plinth_extras %}
|
{% load pagekite_extras %}
|
||||||
|
|
||||||
{% block page_head %}
|
{% block page_head %}
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
@ -51,14 +51,14 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="list-group">
|
<div class="list-group">
|
||||||
{% for service in custom_services %}
|
{% 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">
|
<div class="list-group-item clearfix">
|
||||||
<span class="service">
|
<span class="service">
|
||||||
<span title="Forwards {{ service_link }} to {{ service.backend_host }}:{{ service.backend_port }}">
|
<span title="Connects {{ service_url }} to {{ service.backend_host }}:{{ service.backend_port }}">
|
||||||
{% if service_link|slice:":4" == "http" %}
|
{% if service_url|slice:":4" == "http" %}
|
||||||
<a href="{{ service_link }}">{{ service_link }}</a>
|
<a href="{{ service_url }}">{{ service_url }}</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ service_link }}
|
{{ service_url }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
44
plinth/modules/pagekite/templatetags/pagekite_extras.py
Normal file
44
plinth/modules/pagekite/templatetags/pagekite_extras.py
Normal 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
|
||||||
@ -71,19 +71,6 @@ def get_kite_details():
|
|||||||
'kite_secret': kite_details[1]}
|
'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():
|
def get_pagekite_config():
|
||||||
"""
|
"""
|
||||||
Return the current PageKite configuration by executing various actions.
|
Return the current PageKite configuration by executing various actions.
|
||||||
@ -127,10 +114,23 @@ def get_pagekite_services():
|
|||||||
predefined[name] = True
|
predefined[name] = True
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
custom.append(prepare_params_for_display(params))
|
custom.append(params)
|
||||||
return predefined, custom
|
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):
|
def _run(arguments, superuser=True):
|
||||||
"""Run a given command and raise exception if there was an error"""
|
"""Run a given command and raise exception if there was an error"""
|
||||||
command = 'pagekite'
|
command = 'pagekite'
|
||||||
|
|||||||
@ -25,7 +25,8 @@ from django.views.generic import View, TemplateView
|
|||||||
from django.views.generic.edit import FormView
|
from django.views.generic.edit import FormView
|
||||||
|
|
||||||
from plinth import package
|
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
|
from .forms import ConfigurationForm, DefaultServiceForm, CustomServiceForm
|
||||||
|
|
||||||
subsubmenu = [{'url': reverse_lazy('pagekite:index'),
|
subsubmenu = [{'url': reverse_lazy('pagekite:index'),
|
||||||
@ -80,7 +81,8 @@ class CustomServiceView(ContextMixin, TemplateView):
|
|||||||
unused, custom_services = get_pagekite_services()
|
unused, custom_services = get_pagekite_services()
|
||||||
for service in custom_services:
|
for service in custom_services:
|
||||||
service['form'] = CustomServiceForm(initial=service)
|
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())
|
context.update(get_kite_details())
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@ -91,7 +93,6 @@ class CustomServiceView(ContextMixin, TemplateView):
|
|||||||
return self.render_to_response(context)
|
return self.render_to_response(context)
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
unused, custom_services = get_pagekite_services()
|
|
||||||
form = CustomServiceForm(request.POST, prefix="custom")
|
form = CustomServiceForm(request.POST, prefix="custom")
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save(request)
|
form.save(request)
|
||||||
|
|||||||
@ -15,7 +15,6 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
import copy
|
|
||||||
import os
|
import os
|
||||||
from django import template
|
from django import template
|
||||||
|
|
||||||
@ -59,21 +58,3 @@ def show_subsubmenu(context, menu):
|
|||||||
"""Mark the active menu item and display the subsubmenu"""
|
"""Mark the active menu item and display the subsubmenu"""
|
||||||
menu = mark_active_menuitem(menu, context['request'].path)
|
menu = mark_active_menuitem(menu, context['request'].path)
|
||||||
return {'subsubmenu': menu}
|
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
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user