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): def retrieve_domain(self, pk):
path = API_PATHS.get('domain-detail').format_map({'pk': pk}) path = API_PATHS.get('domain-detail').format_map({'pk': pk})
url = urllib.parse.urljoin(self.base_url, path) url = urllib.parse.urljoin(self.base_url, path)
@ -226,31 +227,18 @@ class OrchestraConnector:
return Domain.new_from_json(domain_json) return Domain.new_from_json(domain_json)
def retrieve_domain_list(self): def retrieve_domain_list(self):
output = self.retrieve_service_list(Domain) domains = self.retrieve_service_list(Domain)
websites = self.retrieve_website_list() domains = domains.prefetch_related("addresses", "websites")
domains = [] # TODO(@slamora): update when backend provides resource disk usage data
for domain_json in output: # initialize domain usage for every domain
# filter querystring # for domain in domains:
querystring = "domain={}".format(domain_json['id']) # domain.usage = {
# 'usage': 300,
# retrieve services associated to a domain # 'total': 650,
domain_json['addresses'] = self.retrieve_service_list( # 'unit': 'MB',
Address.api_name, querystring) # 'percent': 50,
# }
# 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))
return domains return domains
@ -258,15 +246,6 @@ class OrchestraConnector:
qs = self.retrieve_service_list(Website) qs = self.retrieve_service_list(Website)
return qs 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): def verify_credentials(self):
""" """
Returns: Returns:

View file

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

View file

@ -21,6 +21,7 @@ from django.views.generic.list import ListView
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
from orchestra import get_version from orchestra import get_version
from orchestra.contrib.domains.models import Domain
# from .auth import login as auth_login # from .auth import login as auth_login
from .auth import logout as auth_logout from .auth import logout as auth_logout
@ -523,19 +524,7 @@ class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView):
} }
def get_queryset(self): def get_queryset(self):
# Return an empty list to avoid a request to retrieve all the return Domain.objects.filter(account=self.request.user)
# 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
class LoginView(FormView): class LoginView(FormView):