#########################################################################
#
# Copyright (C) 2020 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/>.
#
#########################################################################
import logging
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from guardian.shortcuts import get_anonymous_user
from geonode.base.models import ResourceBase
from geonode.client.hooks import hookset
[docs]
logger = logging.getLogger("geonode.geoapps.models")
[docs]
class GeoApp(ResourceBase):
"""
A GeoApp it is a generic container for every client applications the user might want to create or define.
"""
[docs]
PERMISSIONS = {
"write": [
"change_geoapp_data",
"change_geoapp_style",
]
}
[docs]
name = models.TextField(_("Name"), null=False, blank=False)
[docs]
last_modified = models.DateTimeField(auto_now_add=True)
# The last time the geoapp was modified.
[docs]
def __str__(self):
return f'{self.title} by {(self.owner.username if self.owner else "<Anonymous>")}'
@property
[docs]
def class_name(self):
return self.__class__.__name__
@property
[docs]
def sender(self):
return None
@property
[docs]
def center(self):
"""
.
A handy shortcut for the center_x and center_y properties as a tuple (read only)
"""
return (self.center_x, self.center_y)
@property
[docs]
def type(self):
_ct = self.polymorphic_ctype
_child = _ct.model_class().objects.filter(pk=self.id).first()
if _child and hasattr(_child, "app_type"):
return _child.app_type
return None
@property
[docs]
def is_public(self):
"""
.
Returns True if anonymous (public) user can view geoapp.
"""
user = get_anonymous_user()
return user.has_perm("base.view_resourcebase", obj=self.resourcebase_ptr)
@property
[docs]
def keywords_list(self):
keywords_qs = self.keywords.all()
if keywords_qs:
return [kw.name for kw in keywords_qs]
else:
return []
[docs]
def get_absolute_url(self):
return hookset.geoapp_detail_url(self)
@property
[docs]
def embed_url(self):
return reverse("geoapp_embed", kwargs={"geoappid": self.pk})