@@ -83,6 +83,94 @@ def filter_queryset_user_does_not_have_permission(self, queryset,
83
83
return queryset
84
84
85
85
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
+
86
174
class QuakeMLIndexerPlugin (IndexerPluginPoint ):
87
175
"""
88
176
Each document type can have one indexer.
@@ -114,7 +202,8 @@ class QuakeMLIndexerPlugin(IndexerPluginPoint):
114
202
"author" : "str" ,
115
203
"public" : "bool" ,
116
204
"evaluation_mode" : "str" ,
117
- "event_type" : "str"
205
+ "event_type" : "str" ,
206
+ "site" : "str" ,
118
207
}
119
208
120
209
def index (self , document ):
@@ -160,6 +249,10 @@ def index(self, document):
160
249
evaluation_mode = extra ["evaluationMode" ]["value" ]
161
250
else :
162
251
evaluation_mode = None
252
+ if "site" in extra :
253
+ site = extra ["site" ]["value" ]
254
+ else :
255
+ site = None
163
256
164
257
indices .append ({
165
258
"quakeml_id" : str (event .resource_id ),
@@ -181,6 +274,7 @@ def index(self, document):
181
274
# fast queries using PostGIS.
182
275
"geometry" :
183
276
[Point (org .longitude , org .latitude )] if org else None ,
277
+ "site" : site ,
184
278
})
185
279
186
280
return indices
0 commit comments