-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
models.py
155 lines (123 loc) · 4.68 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#########################################################################
#
# 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.conf import settings
from django.db import models
from django.urls import reverse
from django.db.models import signals
from django.utils.translation import ugettext_lazy as _
from django_jsonfield_backport.models import JSONField
from guardian.shortcuts import get_anonymous_user
from geonode.base.models import ResourceBase, resourcebase_post_save
logger = logging.getLogger("geonode.geoapps.models")
class GeoApp(ResourceBase):
"""
A GeoApp it is a generic container for every client applications the
user might want to create or define.
"""
PERMISSIONS = {
'write': [
'change_geoapp_data',
'change_geoapp_style',
]
}
name = models.TextField(_('Name'), unique=True, db_index=True)
# viewer configuration
zoom = models.IntegerField(_('zoom'), null=True, blank=True)
# The zoom level to use when initially loading this geoapp. Zoom levels start
# at 0 (most zoomed out) and each increment doubles the resolution.
projection = models.CharField(_('projection'), max_length=32, null=True, blank=True)
# The projection used for this geoapp. This is stored as a string with the
# projection's SRID.
center_x = models.FloatField(_('center X'), null=True, blank=True)
# The x coordinate to center on when loading this geoapp. Its interpretation
# depends on the projection.
center_y = models.FloatField(_('center Y'), null=True, blank=True)
# The y coordinate to center on when loading this geoapp. Its interpretation
# depends on the projection.
last_modified = models.DateTimeField(auto_now_add=True)
# The last time the geoapp was modified.
urlsuffix = models.CharField(_('Site URL'), max_length=255, null=True, blank=True)
# Alphanumeric alternative to referencing geoapps by id, appended to end of
# URL instead of id, ie http://domain/geoapps/someview
data = models.OneToOneField(
"GeoAppData",
related_name="data",
null=True,
blank=True,
on_delete=models.CASCADE)
def __str__(self):
return f'{self.title} by {(self.owner.username if self.owner else "<Anonymous>")}'
@property
def class_name(self):
return self.__class__.__name__
@property
def sender(self):
return None
@property
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
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
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
def keywords_list(self):
keywords_qs = self.keywords.all()
if keywords_qs:
return [kw.name for kw in keywords_qs]
else:
return []
def get_absolute_url(self):
geoapp_view = (
reverse("geoapp_detail", None, [str(self.id)]) if settings.GEONODE_APPS_ENABLE else reverse("home")
)
return geoapp_view
@property
def embed_url(self):
return reverse('geoapp_embed', kwargs={'geoappid': self.pk})
class Meta(ResourceBase.Meta):
pass
class GeoAppData(models.Model):
blob = JSONField(
null=False,
default=dict)
resource = models.ForeignKey(
GeoApp,
null=False,
blank=False,
on_delete=models.CASCADE)
# signals.pre_delete.connect(pre_delete_app, sender=GeoApp)
signals.post_save.connect(resourcebase_post_save, sender=GeoApp)