Skip to content

Commit 614eacb

Browse files
committed
add handling "site" tag and additional retrieve permissions
note that this needs krischer/django-plugins#11 to work
1 parent c4ede74 commit 614eacb

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

src/jane/quakeml/plugins.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,94 @@ def filter_queryset_user_does_not_have_permission(self, queryset,
8383
return queryset
8484

8585

86+
def _site_magnitude_threshold_retrieve_permission(
87+
class_name, magnitude_threshold, site=None):
88+
"""
89+
Class factory that returns a quakeml retrieve permission based on a
90+
magnitude threshold, optionally only working on a specific site.
91+
If multiple of these restrictions are defined, all of them apply separately
92+
and the user must have all of them set, down to the lowest threshold
93+
restriction that is supposed to apply.
94+
"""
95+
class _SiteMagnitudeThresholdRetrievePermissionPlugin(
96+
RetrievePermissionPluginPoint):
97+
"""
98+
If user does not have this permission, any events below given magnitude
99+
threshold are filtered out (optionally only for a specific site).
100+
"""
101+
name = 'quakeml'
102+
title = 'Can See Magnitude <{} Events {}Permission'.format(
103+
magnitude_threshold, site and "At site='{}' ".format(site) or "")
104+
105+
# Permission codename and name according to Django's nomenclature.
106+
# XXX no idea if dots are allowed in codename, so replace them
107+
permission_codename = 'can_see_mag_lessthan_{}_site_{}_events'.format(
108+
magnitude_threshold, site or "any").replace(".", "_")
109+
permission_name = 'Can See Magnitude <{} Events{}'.format(
110+
magnitude_threshold, site and " At site='{}'".format(site) or "")
111+
112+
def filter_queryset_user_has_permission(self, queryset, model_type):
113+
# If the user has the permission: don't restrict queryset.
114+
return queryset
115+
116+
def filter_queryset_user_does_not_have_permission(self, queryset,
117+
model_type):
118+
# model_type can be document or document index.
119+
if model_type == "document":
120+
# XXX: Find a good way to do this.
121+
raise NotImplementedError()
122+
elif model_type == "index":
123+
# Modify the queryset to only contain indices that are above
124+
# given magnitude threshold.
125+
# XXX check what happens with events that have null for
126+
# XXX magnitude..
127+
kwargs = {}
128+
# if no site is specified, just do a normal filter by magnitude
129+
# threshold
130+
if site is None:
131+
kwargs["min_magnitude"] = magnitude_threshold
132+
negate = False
133+
# if site is specified, we need to search for events matching
134+
# both criteria and then invert the resulting queryset
135+
else:
136+
kwargs['site'] = site
137+
kwargs["max_magnitude"] = magnitude_threshold - 0.01
138+
negate = True
139+
queryset = queryset.model.objects.get_filtered_queryset(
140+
document_type="quakeml", queryset=queryset, negate=negate,
141+
**kwargs)
142+
else:
143+
raise NotImplementedError()
144+
return queryset
145+
146+
new_class = _SiteMagnitudeThresholdRetrievePermissionPlugin
147+
# Set the class type name.
148+
setattr(new_class, "__name__", class_name)
149+
return new_class
150+
151+
152+
# Retrieve permissions for small events, if users don't have these permissions
153+
# small events are not accessible to them
154+
MagnitudeLessThan1RetrievePermissionPlugin = \
155+
_site_magnitude_threshold_retrieve_permission(
156+
"MagnitudeLessThan1RetrievePermissionPlugin", magnitude_threshold=1.0)
157+
MagnitudeLessThan2RetrievePermissionPlugin = \
158+
_site_magnitude_threshold_retrieve_permission(
159+
"MagnitudeLessThan2RetrievePermissionPlugin", magnitude_threshold=2.0)
160+
161+
# Retrieve permissions for small events attributed to a specific site (e.g. a
162+
# specific deep geothermal project), if users don't have these permissions
163+
# small events that are attributed to that site are not accessible to them
164+
UnterhachingLessThan1RetrievePermissionPlugin = \
165+
_site_magnitude_threshold_retrieve_permission(
166+
"UnterhachingLessThan1RetrievePermissionPlugin",
167+
magnitude_threshold=1.0, site="geothermie_unterhaching")
168+
UnterhachingLessThan2RetrievePermissionPlugin = \
169+
_site_magnitude_threshold_retrieve_permission(
170+
"UnterhachingLessThan2RetrievePermissionPlugin",
171+
magnitude_threshold=2.0, site="geothermie_unterhaching")
172+
173+
86174
class QuakeMLIndexerPlugin(IndexerPluginPoint):
87175
"""
88176
Each document type can have one indexer.
@@ -114,7 +202,8 @@ class QuakeMLIndexerPlugin(IndexerPluginPoint):
114202
"author": "str",
115203
"public": "bool",
116204
"evaluation_mode": "str",
117-
"event_type": "str"
205+
"event_type": "str",
206+
"site": "str",
118207
}
119208

120209
def index(self, document):
@@ -160,6 +249,10 @@ def index(self, document):
160249
evaluation_mode = extra["evaluationMode"]["value"]
161250
else:
162251
evaluation_mode = None
252+
if "site" in extra:
253+
site = extra["site"]["value"]
254+
else:
255+
site = None
163256

164257
indices.append({
165258
"quakeml_id": str(event.resource_id),
@@ -181,6 +274,7 @@ def index(self, document):
181274
# fast queries using PostGIS.
182275
"geometry":
183276
[Point(org.longitude, org.latitude)] if org else None,
277+
"site": site,
184278
})
185279

186280
return indices

src/jane/static/web_gis/src/directives/map.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,14 @@ app.directive('openlayers3', function($q, $log, bing_key, $modal) {
482482
public = "not specified"
483483
}
484484

485+
var site = feature.get("site");
486+
if (site == null) {
487+
site = "not specified"
488+
}
489+
485490
tooltip_title += "\nAgency: " + feature.get("agency") +
486491
" | Author: " + author + " | Evaluation mode: " + evaluation_mode +
487-
" | Public: " + public;
492+
" | Public: " + public + " | Site: " + site;
488493

489494
if (feature.get('magnitude')) {
490495

@@ -561,6 +566,7 @@ app.directive('openlayers3', function($q, $log, bing_key, $modal) {
561566
modal.$scope.magnitude_type = feature.get("magnitude_type");
562567
modal.$scope.origin_time = feature.get("origin_time");
563568
modal.$scope.public = feature.get("public");
569+
modal.$scope.site = feature.get("site");
564570
modal.$scope.quakeml_id = feature.get("quakeml_id");
565571

566572

src/jane/static/web_gis/templates/event_modal.tpl.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ <h4 class="modal-title">Details for event {{title}}</h4>
2929
<dd>{{ evaluation_mode }}</dd>
3030
<dt>Public</dt>
3131
<dd>{{ public }}</dd>
32+
<dt>Site</dt>
33+
<dd>{{ site }}</dd>
3234
</dl>
3335
</div>
3436
<div class="row">

0 commit comments

Comments
 (0)