Refactor DomainDetailView

This commit is contained in:
Santiago L 2023-11-23 10:19:34 +01:00
parent 720c4dd8a2
commit f2aaf28422
3 changed files with 15 additions and 47 deletions

View file

@ -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:

View file

@ -19,7 +19,7 @@
</tr>
</thead>
<tbody>
{% for record in object.records %}
{% for record in object.records.all %}
<tr>
<td>{{ record.type }}</td>
<td>{{ record.value }}</td>

View file

@ -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):