-
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.
Browse files
Browse the repository at this point in the history
[Fixes #12226] Directory assets --------- Co-authored-by: etj <[email protected]>
- Loading branch information
1 parent
216e3dd
commit 618a936
Showing
14 changed files
with
286 additions
and
75 deletions.
There are no files selected for viewing
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,55 @@ | ||
import json | ||
import logging | ||
from django.db import models | ||
from django.forms import widgets | ||
from django.contrib import admin | ||
|
||
from geonode.assets.local import LocalAssetHandler | ||
from geonode.assets.models import LocalAsset | ||
from geonode.base.models import Link | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PrettyJSONWidget(widgets.Textarea): | ||
|
||
def format_value(self, value): | ||
try: | ||
value = json.dumps(json.loads(value), indent=2, sort_keys=True) | ||
# these lines will try to adjust size of TextArea to fit to content | ||
row_lengths = [len(r) for r in value.split("\n")] | ||
self.attrs["rows"] = min(max(len(row_lengths) + 2, 10), 30) | ||
self.attrs["cols"] = min(max(max(row_lengths) + 2, 40), 120) | ||
return value | ||
except Exception as e: | ||
logger.warning("Error while formatting JSON: {}".format(e)) | ||
return super(PrettyJSONWidget, self).format_value(value) | ||
|
||
|
||
@admin.register(LocalAsset) | ||
class LocalAssetAdmin(admin.ModelAdmin): | ||
model = LocalAsset | ||
|
||
list_display = ("id", "title", "type", "owner", "created_formatted", "managed", "links", "link0") | ||
list_display_links = ("id", "title") | ||
|
||
formfield_overrides = {models.JSONField: {"widget": PrettyJSONWidget}} | ||
|
||
def created_formatted(self, obj): | ||
return obj.created.strftime("%Y-%m-%d %H:%M:%S") | ||
|
||
def links(self, obj): | ||
return Link.objects.filter(asset=obj).count() | ||
|
||
def link0(self, obj): | ||
link = Link.objects.filter(asset=obj).first() | ||
return f"{link.link_type} {link.extension}: {link.name}" if link else None | ||
|
||
def managed(self, obj) -> bool: | ||
try: | ||
return LocalAssetHandler._is_file_managed(obj.location[0]) | ||
except Exception as e: | ||
logger.error(f"Bad location for asset obj: {e}") | ||
return None | ||
|
||
managed.boolean = True |
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
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
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
Oops, something went wrong.