Skip to content

Commit

Permalink
allow download of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jan 8, 2024
1 parent da4dd60 commit 5ad58f6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions metadata_catalogue/maps/migrations/0007_layer_downloadable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.8 on 2024-01-08 10:53

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("maps", "0006_alter_map_slug"),
]

operations = [
migrations.AddField(
model_name="layer",
name="downloadable",
field=models.BooleanField(default=False),
),
]
12 changes: 9 additions & 3 deletions metadata_catalogue/maps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ class Layer(models.Model):
"maps.LayerGroup", on_delete=models.SET_NULL, related_name="layers", null=True, blank=True
)
group_order = models.IntegerField(default=0)
downloadable = models.BooleanField(default=False)

def __str__(self):
return f"{self.slug}{self.map}"

def get_download_url(self, request):
if self.source and self.downloadable:
return self.source.get_download_url(request)
return None

def save(self, *args, **kwargs):
if self.slug is None:
if self.source:
Expand Down Expand Up @@ -158,7 +164,7 @@ class Meta:
def get_metadata(self, request):
layers = []
for root in self.groups.order_by("order").all():
layers.append(root.as_layer_tree())
layers.append(root.as_layer_tree(request))

style_url = request.build_absolute_uri(
reverse(f"{settings.MAPS_API_PREFIX}:map_style", kwargs={"map_slug": self.slug})
Expand Down Expand Up @@ -220,7 +226,7 @@ class LayerGroup(MP_Node):
def __str__(self):
return f"{self.name} @ {self.map}"

def as_layer_tree(self):
def as_layer_tree(self, request):
current_group = {"id": f"group-{self.id}", "name": self.name, "children": [], "download": self.download_url}
for sub_group in self.get_children():
current_group["children"].append(sub_group.as_layer_tree())
Expand All @@ -230,7 +236,7 @@ def as_layer_tree(self):
{
"id": layer.slug,
"name": str(layer.name),
"download": None,
"download": layer.get_download_url(request),
}
)

Expand Down

0 comments on commit 5ad58f6

Please sign in to comment.