From e8e1d2240e6c55b213a2b98bdd2c95ec6e84c8a9 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 23 Nov 2023 10:19:34 +0100 Subject: [PATCH] Refactor DomainDetailView --- orchestra/contrib/musician/api.py | 45 +++++-------------- .../templates/musician/domain_detail.html | 2 +- orchestra/contrib/musician/views.py | 15 +------ 3 files changed, 15 insertions(+), 47 deletions(-) diff --git a/orchestra/contrib/musician/api.py b/orchestra/contrib/musician/api.py index cb7f4d8c..de1ab265 100644 --- a/orchestra/contrib/musician/api.py +++ b/orchestra/contrib/musician/api.py @@ -217,6 +217,7 @@ class OrchestraConnector: def retrieve_domain(self, pk): + path = API_PATHS.get('domain-detail').format_map({'pk': pk}) url = urllib.parse.urljoin(self.base_url, path) @@ -226,31 +227,18 @@ class OrchestraConnector: return Domain.new_from_json(domain_json) def retrieve_domain_list(self): - output = self.retrieve_service_list(Domain) - websites = self.retrieve_website_list() + domains = self.retrieve_service_list(Domain) + domains = domains.prefetch_related("addresses", "websites") - domains = [] - for domain_json in output: - # filter querystring - querystring = "domain={}".format(domain_json['id']) - - # retrieve services associated to a domain - domain_json['addresses'] = self.retrieve_service_list( - Address.api_name, querystring) - - # retrieve websites (as they cannot be filtered by domain on the API we should do it here) - domain_json['websites'] = self.filter_websites_by_domain(websites, domain_json['id']) - - # TODO(@slamora): update when backend provides resource disk usage data - domain_json['usage'] = { - # 'usage': 300, - # 'total': 650, - # 'unit': 'MB', - # 'percent': 50, - } - - # append to list a Domain object - domains.append(Domain.new_from_json(domain_json)) + # TODO(@slamora): update when backend provides resource disk usage data + # initialize domain usage for every domain + # for domain in domains: + # domain.usage = { + # 'usage': 300, + # 'total': 650, + # 'unit': 'MB', + # 'percent': 50, + # } return domains @@ -258,15 +246,6 @@ class OrchestraConnector: qs = self.retrieve_service_list(Website) return qs - def filter_websites_by_domain(self, websites, domain_id): - matching = [] - for website in websites: - web_domains = [web_domain.id for web_domain in website.domains] - if domain_id in web_domains: - matching.append(website) - - return matching - def verify_credentials(self): """ Returns: diff --git a/orchestra/contrib/musician/templates/musician/domain_detail.html b/orchestra/contrib/musician/templates/musician/domain_detail.html index 761c331a..d8c96cce 100644 --- a/orchestra/contrib/musician/templates/musician/domain_detail.html +++ b/orchestra/contrib/musician/templates/musician/domain_detail.html @@ -19,7 +19,7 @@ - {% for record in object.records %} + {% for record in object.records.all %} {{ record.type }} {{ record.value }} diff --git a/orchestra/contrib/musician/views.py b/orchestra/contrib/musician/views.py index 7816eaad..bc880974 100644 --- a/orchestra/contrib/musician/views.py +++ b/orchestra/contrib/musician/views.py @@ -21,6 +21,7 @@ from django.views.generic.list import ListView from requests.exceptions import HTTPError from orchestra import get_version +from orchestra.contrib.domains.models import Domain # from .auth import login as auth_login from .auth import logout as auth_logout @@ -523,19 +524,7 @@ class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView): } def get_queryset(self): - # Return an empty list to avoid a request to retrieve all the - # user domains. We will get a 404 if the domain doesn't exists - # while invoking `get_object` - return [] - - def get_object(self, queryset=None): - if queryset is None: - queryset = self.get_queryset() - - pk = self.kwargs.get(self.pk_url_kwarg) - domain = self.orchestra.retrieve_domain(pk) - - return domain + return Domain.objects.filter(account=self.request.user) class LoginView(FormView):