Commit e3bc5c67 authored by narendra kumar's avatar narendra kumar

fix 1524

names: Implement API for retrieving domain names
parent cb43d5ad
Pipeline #45202 passed with stages
in 15 minutes and 12 seconds
......@@ -27,14 +27,14 @@ from django.templatetags.static import static
from plinth import frontpage, module_loader
from plinth.modules import names
from plinth.modules.names import api
def access_info(request, **kwargs):
"""API view to return a list of domains and types."""
domains = [{
'domain': domain,
'type': domain_type
} for domain_type, domains in names.domains.items() for domain in domains]
} for domain_type, domains in api.get_domain_type_and_domains_as_list_of_tupple() for domain in domains]
response = {'domains': domains}
return HttpResponse(json.dumps(response), content_type='application/json')
......
......@@ -25,6 +25,7 @@ from plinth import action_utils, actions, cfg, frontpage
from plinth import service as service_module
from plinth.menu import main_menu
from plinth.modules import names
from plinth.modules.names import api
from plinth.signals import domain_added, domain_removed, domainname_change
from plinth.utils import format_lazy
......@@ -85,10 +86,7 @@ def init():
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.install(managed_packages)
domains = [
domain for domains_of_a_type in names.domains.values()
for domain in domains_of_a_type
]
domains = api.get_domain_names_list()
helper.call('post', actions.superuser_run, 'cockpit', ['setup'] + domains)
global service
if service is None:
......
......@@ -27,6 +27,7 @@ from plinth import action_utils, actions, cfg, module_loader
from plinth.errors import ActionError
from plinth.menu import main_menu
from plinth.modules import config, names
from plinth.modules.names import api
from plinth.signals import domain_added, domain_removed, domainname_change
from plinth.utils import format_lazy
......@@ -89,7 +90,7 @@ def diagnose():
"""Run diagnostics and return the results."""
results = []
for domain_type, domains in names.domains.items():
for domain_type, domains in api.get_domain_type_and_domains_as_list_of_tupple():
if domain_type == 'hiddenservice':
continue
......@@ -208,7 +209,7 @@ def get_status():
}
status['current_domain'] = current_domain
for domain_type, domains in names.domains.items():
for domain_type, domains in api.get_domain_type_and_domains_as_list_of_tupple():
# XXX: Remove when Let's Encrypt supports .onion addresses
if domain_type == 'hiddenservice':
continue
......
......@@ -24,6 +24,7 @@ from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth.modules import minetest, names
from plinth.views import ServiceView
from plinth.modules.names import api
from . import description, get_configuration, managed_services
from .forms import MinetestForm
......@@ -50,8 +51,7 @@ class MinetestServiceView(ServiceView): # pylint: disable=too-many-ancestors
"""Add service to the context data."""
context = super().get_context_data(*args, **kwargs)
# Filter out onion addresses and get a unique list of domains
domains = set(domain for domains in names.domains.values()
for domain in domains if not domain.endswith('.onion'))
domains = api.get_domain_names_set()
context['domains'] = domains
return context
......
......@@ -29,6 +29,7 @@ from django.views.decorators.http import require_POST
from plinth import actions
from plinth.modules import monkeysphere, names
from plinth.modules.names import api
publish_process = None
......@@ -109,15 +110,12 @@ def get_keys(fingerprint=None):
['host-show-keys'] + fingerprint)
keys = json.loads(output)['keys']
domains = [
domain for domains_of_a_type in names.domains.values()
for domain in domains_of_a_type
]
domains = api.get_domain_names_set()
for key in keys.values():
key['imported_domains'] = set(key.get('imported_domains', []))
key['available_domains'] = set(key.get('available_domains', []))
if '*' in key['available_domains']:
key['available_domains'] = set(domains)
key['available_domains'] = domains
key['all_domains'] = sorted(key['available_domains'].union(
key['imported_domains']))
......
#
# This file is part of FreedomBox.
#
# 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/>.
#
"""
api to fetch domain information
"""
from plinth.modules import names
def get_domain_names_set(filter_for_service:str=None):
domains = set(domain for domains in names.domains.values()
for domain in domains if not domain.endswith('.onion')
and does_domain_belong_to_service(domains[domain], filter_for_service))
return domains
def get_domain_names_list(filter_for_service:str=None):
domains = list(get_domain_names_set(filter_for_service))
return domains
def get_domain_type_and_domains_as_list_of_tupple(filter_for_service: str = None):
domains = set(
(domain_type, domains) for domain_type, domains in names.domains.items()
for domain in domains if not domain.endswith('.onion')
and does_domain_belong_to_service(domains[domain], filter_for_service)
)
return domains
def does_domain_belong_to_service(services,filter_for_service:str):
if filter_for_service is None:
return True
elif filter_for_service in services:
return True
else:
return False
......@@ -25,7 +25,7 @@ import random
import re
import string
from distutils.version import LooseVersion
import plinth.modules.api as api
import ruamel.yaml
from django.utils.functional import lazy
......@@ -80,7 +80,7 @@ def get_domain_names():
domain_names = []
for domain_type, domains in names.domains.items():
for domain_type, domains in api.get_domain_type_and_domains_as_list_of_tupple():
if domain_type == 'hiddenservice':
continue
for domain in domains:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment