Skip to content

Commit b5ab688

Browse files
committed
Fix home view
1 parent 51347d6 commit b5ab688

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

isimip_data/home/utils.py

+29-28
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,36 @@
44
def get_home_links(tree, product, category_sector_resource):
55
links = []
66

7-
for simulation_round, simulation_round_tree in tree.tree_dict.items():
8-
category_sector_resource_tree = simulation_round_tree.get('items', {}).get(product, {}).get('items', {})
9-
if isinstance(category_sector_resource, str):
10-
# this is a category or a sector
11-
if category_sector_resource_tree.get(category_sector_resource):
12-
links.append(
13-
(
14-
simulation_round,
15-
reverse('search', args=[
16-
f'tree/{simulation_round}/{product}/{category_sector_resource}'
17-
])
7+
if tree:
8+
for simulation_round, simulation_round_tree in tree.tree_dict.items():
9+
category_sector_resource_tree = simulation_round_tree.get('items', {}).get(product, {}).get('items', {})
10+
if isinstance(category_sector_resource, str):
11+
# this is a category or a sector
12+
if category_sector_resource_tree.get(category_sector_resource):
13+
links.append(
14+
(
15+
simulation_round,
16+
reverse('search', args=[
17+
f'tree/{simulation_round}/{product}/{category_sector_resource}'
18+
])
19+
)
1820
)
19-
)
20-
else:
21-
# this is a resource
22-
for resource_path in category_sector_resource.paths:
23-
resource_simulation_round, resource_product, resource_publication = resource_path.split('/')
24-
if resource_simulation_round == simulation_round and resource_product == product:
25-
tree_publication = category_sector_resource_tree.get(resource_publication) or \
26-
category_sector_resource_tree.get(resource_publication.lower())
27-
if tree_publication:
28-
publication = tree_publication.get('specifier')
29-
links.append(
30-
(
31-
simulation_round,
32-
reverse('search', args=[
33-
f'tree/{simulation_round}/{product}/{publication}'
34-
])
21+
else:
22+
# this is a resource
23+
for resource_path in category_sector_resource.paths:
24+
resource_simulation_round, resource_product, resource_publication = resource_path.split('/')
25+
if resource_simulation_round == simulation_round and resource_product == product:
26+
tree_publication = category_sector_resource_tree.get(resource_publication) or \
27+
category_sector_resource_tree.get(resource_publication.lower())
28+
if tree_publication:
29+
publication = tree_publication.get('specifier')
30+
links.append(
31+
(
32+
simulation_round,
33+
reverse('search', args=[
34+
f'tree/{simulation_round}/{product}/{publication}'
35+
])
36+
)
3537
)
36-
)
3738

3839
return links

isimip_data/home/views.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
from django.contrib.postgres.aggregates import ArrayAgg
2-
from django.db.models import Q
31
from django.shortcuts import render
42

53
from isimip_data.metadata.models import Resource, Tree
64

75

86
def home(request):
97
tree = Tree.objects.using('metadata').first()
10-
11-
# get all tree_path for DerivedOutputData and construct a Q object for all resources for DerivedOutputData
12-
q = Q()
13-
for simulation_round in tree.tree_dict.keys():
14-
q |= Q(paths_agg__icontains=f'{simulation_round}/DerivedOutputData/')
15-
16-
resources = Resource.objects.using('metadata').annotate(paths_agg=ArrayAgg('paths')).filter(q).order_by('paths')
8+
resources = Resource.objects.using('metadata').filter_by_tree(tree, product='DerivedOutputData')
179

1810
return render(request, 'home/home.html', {
1911
'tree': tree,

isimip_data/metadata/managers.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.contrib.postgres.aggregates import ArrayAgg
12
from django.db import models
23

34

@@ -24,3 +25,22 @@ class IdentifierManager(models.Manager):
2425

2526
def get_queryset(self):
2627
return IdentifierQuerySet(self.model, using=self._db)
28+
29+
30+
class ResourceQuerySet(models.QuerySet):
31+
32+
def filter_by_tree(self, tree, product=None):
33+
if tree:
34+
if product is not None:
35+
# get all tree_path for the product and construct a Q object for all resources
36+
q = models.Q()
37+
for simulation_round in tree.tree_dict.keys():
38+
q |= models.Q(paths_agg__icontains=f'{simulation_round}/{product}/')
39+
40+
return self.annotate(paths_agg=ArrayAgg('paths')).filter(q).order_by('paths')
41+
42+
43+
class ResourceManager(models.Manager):
44+
45+
def get_queryset(self):
46+
return ResourceQuerySet(self.model, using=self._db)

isimip_data/metadata/models.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.utils.functional import cached_property
99

1010
from .constants import RIGHTS
11-
from .managers import DatasetManager, IdentifierManager
11+
from .managers import DatasetManager, IdentifierManager, ResourceManager
1212
from .utils import get_json_ld_name, get_terms_of_use, merge_identifiers, merge_specifiers, prettify_specifiers
1313

1414

@@ -203,6 +203,8 @@ def get_absolute_url(self):
203203

204204
class Resource(models.Model):
205205

206+
objects = ResourceManager()
207+
206208
id = models.UUIDField(primary_key=True)
207209
doi = models.TextField()
208210
title = models.TextField()

0 commit comments

Comments
 (0)