-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,026 additions
and
107 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2016 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
from django.apps import AppConfig | ||
|
||
from geonode.notifications_helper import NotificationsAppConfigBase | ||
|
||
|
||
class BaseAppConfig(NotificationsAppConfigBase, AppConfig): | ||
name = "geonode.assets" | ||
|
||
def ready(self): | ||
super().ready() | ||
run_setup_hooks() | ||
|
||
|
||
def run_setup_hooks(*args, **kwargs): | ||
from geonode.assets.handlers import asset_handler_registry | ||
|
||
asset_handler_registry.init_registry() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import logging | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponse | ||
from django.utils.module_loading import import_string | ||
|
||
from geonode.assets.models import Asset | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AssetHandlerInterface: | ||
|
||
def handled_asset_class(self): | ||
raise NotImplementedError() | ||
|
||
def create(self, title, description, type, owner, *args, **kwargs): | ||
raise NotImplementedError() | ||
|
||
def remove_data(self, asset: Asset): | ||
raise NotImplementedError() | ||
|
||
def replace_data(self, asset: Asset, files: list): | ||
raise NotImplementedError() | ||
|
||
def clone(self, asset: Asset) -> Asset: | ||
""" | ||
Creates a copy in the DB and copies the underlying data as well | ||
""" | ||
raise NotImplementedError() | ||
|
||
def create_link_url(self, asset: Asset) -> str: | ||
raise NotImplementedError() | ||
|
||
def get_download_handler(self, asset: Asset): | ||
raise NotImplementedError() | ||
|
||
def get_storage_manager(self, asset): | ||
raise NotImplementedError() | ||
|
||
|
||
class AssetDownloadHandlerInterface: | ||
|
||
def create_response(self, asset: Asset, attachment: bool = False, basename=None) -> HttpResponse: | ||
raise NotImplementedError() | ||
|
||
|
||
class AssetHandlerRegistry: | ||
_registry = {} | ||
_default_handler = None | ||
|
||
def init_registry(self): | ||
self.register_asset_handlers() | ||
self.set_default_handler() | ||
|
||
def register_asset_handlers(self): | ||
for module_path in settings.ASSET_HANDLERS: | ||
handler = import_string(module_path) | ||
self.register(handler) | ||
logger.info(f"Registered Asset handlers: {', '.join(settings.ASSET_HANDLERS)}") | ||
|
||
def set_default_handler(self): | ||
# check if declared class is registered | ||
for handler in self._registry.values(): | ||
if ".".join([handler.__class__.__module__, handler.__class__.__name__]) == settings.DEFAULT_ASSET_HANDLER: | ||
self._default_handler = handler | ||
break | ||
|
||
if self._default_handler is None: | ||
logger.error(f"Could not set default asset handler class {settings.DEFAULT_ASSET_HANDLER}") | ||
else: | ||
logger.info(f"Default Asset handler {settings.DEFAULT_ASSET_HANDLER}") | ||
|
||
def register(self, asset_handler_class): | ||
self._registry[asset_handler_class.handled_asset_class()] = asset_handler_class() | ||
|
||
def get_default_handler(self) -> AssetHandlerInterface: | ||
return self._default_handler | ||
|
||
def get_handler(self, asset): | ||
asset_cls = asset if isinstance(asset, type) else asset.__class__ | ||
ret = self._registry.get(asset_cls, None) | ||
if not ret: | ||
logger.warning(f"Could not find handler for asset {asset_cls}::{asset.__class__}") | ||
logger.warning("available asset types:") | ||
for k, v in self._registry.items(): | ||
logger.warning(f"{k} --> {v}") | ||
return ret | ||
|
||
|
||
asset_handler_registry = AssetHandlerRegistry() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import datetime | ||
import logging | ||
import os | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponse | ||
from django.urls import reverse | ||
from django_downloadview import DownloadResponse | ||
|
||
from geonode.assets.handlers import asset_handler_registry, AssetHandlerInterface, AssetDownloadHandlerInterface | ||
from geonode.assets.models import LocalAsset | ||
from geonode.storage.manager import storage_manager | ||
from geonode.utils import build_absolute_uri | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LocalAssetHandler(AssetHandlerInterface): | ||
@staticmethod | ||
def handled_asset_class(): | ||
return LocalAsset | ||
|
||
def get_download_handler(self, asset): | ||
return LocalAssetDownloadHandler() | ||
|
||
def get_storage_manager(self, asset): | ||
return storage_manager | ||
|
||
def create(self, title, description, type, owner, files=None, clone_files=False, *args, **kwargs): | ||
if not files: | ||
raise ValueError("File(s) expected") | ||
|
||
if clone_files: | ||
prefix = datetime.datetime.now().strftime("%Y%m%d%H%M%S") | ||
files = storage_manager.copy_files_list(files, dir=settings.ASSETS_ROOT, dir_prefix=prefix) | ||
# TODO: please note the copy_files_list will make flat any directory structure | ||
|
||
asset = LocalAsset( | ||
title=title, | ||
description=description, | ||
type=type, | ||
owner=owner, | ||
created=datetime.datetime.now(), | ||
location=files, | ||
) | ||
asset.save() | ||
return asset | ||
|
||
def remove_data(self, asset: LocalAsset): | ||
removed_dir = set() | ||
for file in asset.location: | ||
if file.startswith(settings.ASSETS_ROOT): | ||
logger.info(f"Removing asset file {file}") | ||
storage_manager.delete(file) | ||
removed_dir.add(os.path.dirname(file)) | ||
else: | ||
logger.info(f"Not removing asset file outside asset directory {file}") | ||
|
||
for dir in removed_dir: | ||
if not os.listdir(dir): | ||
logger.info(f"Removing empty asset directory {dir}") | ||
os.remove(dir) | ||
|
||
def replace_data(self, asset: LocalAsset, files: list): | ||
self.remove_data(asset) | ||
asset.location = files | ||
asset.save() | ||
|
||
def clone(self, asset: LocalAsset) -> LocalAsset: | ||
prefix = datetime.datetime.now().strftime("%Y%m%d%H%M%S") | ||
asset.location = storage_manager.copy_files_list(asset.location, dir=settings.ASSETS_ROOT, dir_prefix=prefix) | ||
asset.pk = None | ||
asset.save() | ||
return asset | ||
|
||
def create_download_url(self, asset) -> str: | ||
return build_absolute_uri(reverse("assets-download", args=(asset.pk,))) | ||
|
||
def create_link_url(self, asset) -> str: | ||
return build_absolute_uri(reverse("assets-link", args=(asset.pk,))) | ||
|
||
|
||
class LocalAssetDownloadHandler(AssetDownloadHandlerInterface): | ||
|
||
def create_response(self, asset: LocalAsset, attachment: bool = False, basename=None) -> HttpResponse: | ||
if not asset.location: | ||
return HttpResponse("Asset does not contain any data", status=500) | ||
|
||
if len(asset.location) > 1: | ||
logger.warning("TODO: Asset contains more than one file. Download needs to be implemented") | ||
|
||
file0 = asset.location[0] | ||
filename = os.path.basename(file0) | ||
orig_base, ext = os.path.splitext(filename) | ||
outname = f"{basename or orig_base}{ext}" | ||
|
||
if storage_manager.exists(file0): | ||
logger.info(f"Returning file {file0} with name {outname}") | ||
|
||
return DownloadResponse( | ||
storage_manager.open(file0).file, | ||
basename=f"{outname}", | ||
attachment=attachment, | ||
) | ||
else: | ||
logger.warning(f"Internal file {file0} not found for asset {asset.id}") | ||
return HttpResponse(f"Internal file not found for asset {asset.id}", status=500) | ||
|
||
|
||
asset_handler_registry.register(LocalAssetHandler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Generated by Django 4.2.9 on 2024-04-24 10:02 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("base", "0090_alter_resourcebase_polymorphic_ctype"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Asset", | ||
fields=[ | ||
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("title", models.CharField(max_length=255)), | ||
("description", models.TextField(blank=True, null=True)), | ||
("type", models.CharField(max_length=255)), | ||
("created", models.DateTimeField(default=django.utils.timezone.now)), | ||
("owner", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
( | ||
"polymorphic_ctype", | ||
models.ForeignKey( | ||
editable=False, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="polymorphic_%(app_label)s.%(class)s_set+", | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name_plural": "Assets", | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name="LocalAsset", | ||
fields=[ | ||
( | ||
"asset_ptr", | ||
models.OneToOneField( | ||
auto_created=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
parent_link=True, | ||
primary_key=True, | ||
serialize=False, | ||
to="assets.asset", | ||
), | ||
), | ||
("location", models.JSONField(blank=True, default=list)), | ||
], | ||
options={ | ||
"verbose_name_plural": "Local assets", | ||
}, | ||
bases=("assets.asset",), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from django.db import models | ||
from django.utils.timezone import now | ||
from polymorphic.managers import PolymorphicManager | ||
from polymorphic.models import PolymorphicModel | ||
from django.db.models import signals | ||
from django.contrib.auth import get_user_model | ||
|
||
|
||
class Asset(PolymorphicModel): | ||
""" | ||
A generic data linked to a ResourceBase | ||
""" | ||
|
||
title = models.CharField(max_length=255, null=False, blank=False) | ||
description = models.TextField(null=True, blank=True) | ||
type = models.CharField(max_length=255, null=False, blank=False) | ||
owner = models.ForeignKey(get_user_model(), null=False, blank=False, on_delete=models.CASCADE) | ||
created = models.DateTimeField(default=now) | ||
|
||
objects = PolymorphicManager() | ||
|
||
class Meta: | ||
verbose_name_plural = "Assets" | ||
|
||
def __str__(self) -> str: | ||
return super().__str__() | ||
|
||
|
||
class LocalAsset(Asset): | ||
""" | ||
Local resource, will replace the files | ||
""" | ||
|
||
location = models.JSONField(default=list, blank=True) | ||
|
||
class Meta: | ||
verbose_name_plural = "Local assets" | ||
|
||
def __str__(self) -> str: | ||
return super().__str__() | ||
|
||
|
||
def cleanup_asset_data(instance, *args, **kwargs): | ||
from geonode.assets.handlers import asset_handler_registry | ||
|
||
asset_handler_registry.get_handler(instance).remove_data(instance) | ||
|
||
|
||
signals.post_delete.connect(cleanup_asset_data, sender=LocalAsset) |
Oops, something went wrong.