-
Notifications
You must be signed in to change notification settings - Fork 11
/
fields.py
540 lines (394 loc) · 15.5 KB
/
fields.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# PATCH to [virtual environment
# path]/lib/python2.7/site-packages/haystack/fields.py encoding: utf-8
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import re
from inspect import ismethod
from django.template import loader
from django.utils import datetime_safe, six
from haystack.exceptions import SearchFieldError
from haystack.utils import get_model_ct_tuple
from haystack.utils.geo import Point, ensure_point
class NotProvided:
pass
# Note that dates in the full ISO 8601 format will be accepted as long as the
# hour/minute/second components are zeroed for compatibility with search
# backends which lack a date time distinct from datetime:
DATE_REGEX = re.compile(
r'^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})(?:|T00:00:00Z?)$')
DATETIME_REGEX = re.compile(
r'^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
r'(T|\s+)(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}).*?$'
)
# All the SearchFields variants.
class SearchField:
"""The base implementation of a search field."""
field_type = None
def __init__(self,
model_attr=None,
use_template=False,
template_name=None,
document=False,
indexed=True,
stored=True,
faceted=False,
default=NotProvided,
null=False,
index_fieldname=None,
facet_class=None,
boost=1.0,
weight=None):
# Track what the index thinks this field is called.
self.instance_name = None
self.model_attr = model_attr
self.use_template = use_template
self.template_name = template_name
self.document = document
self.indexed = indexed
self.stored = stored
self.faceted = faceted
self._default = default
self.null = null
self.index_fieldname = index_fieldname
self.boost = weight or boost
self.is_multivalued = False
# We supply the facet_class for making it easy to create a faceted
# field based off of this field.
self.facet_class = facet_class
if self.facet_class is None:
self.facet_class = FacetCharField
self.set_instance_name(None)
def set_instance_name(self, instance_name):
self.instance_name = instance_name
if self.index_fieldname is None:
self.index_fieldname = self.instance_name
def has_default(self):
"""Returns a boolean of whether this field has a default value."""
return self._default is not NotProvided
@property
def default(self):
"""Returns the default value for the field."""
if callable(self._default):
return self._default()
return self._default
def prepare(self, obj):
"""
Takes data from the provided object and prepares it for storage in the
index.
"""
# Give priority to a template.
if self.use_template:
return self.prepare_template(obj)
if self.model_attr is not None:
attrs = self.split_model_attr_lookups()
current_objects = [obj]
values = self.resolve_attributes_lookup(current_objects, attrs)
if len(values) == 1:
return values[0]
return values
if self.has_default():
return self.default
return None
def resolve_attributes_lookup(self, current_objects, attributes):
"""
Recursive method that looks, for one or more objects, for an attribute
that can be multiple objects (relations) deep.
"""
values = []
for current_object in current_objects:
if not hasattr(current_object, attributes[0]):
raise SearchFieldError(
"The model '%s' does not have a model_attr '%s'." %
(repr(current_object), attributes[0]))
if len(attributes) > 1:
current_objects_in_attr = self.get_iterable_objects(
getattr(current_object, attributes[0]))
return self.resolve_attributes_lookup(current_objects_in_attr,
attributes[1:])
current_object = getattr(current_object, attributes[0])
if current_object is None:
if self.has_default():
current_object = self._default
elif self.null:
current_object = None
else:
raise SearchFieldError(
"The model '%s' combined with model_attr '%s' "
"returned None, but doesn't allow a default or null "
"value." % (repr(current_object), self.model_attr))
if callable(current_object):
values.append(current_object())
else:
values.append(current_object)
return values
def split_model_attr_lookups(self):
"""Returns list of nested attributes for looking through the
relation."""
return self.model_attr.split('__')
@classmethod
def get_iterable_objects(cls, current_objects):
"""
Returns iterable of objects that contain data. For example, resolves
Django ManyToMany relationship so the attributes of the related models
can then be accessed.
"""
if current_objects is None:
return []
if hasattr(current_objects, 'all'):
# i.e, Django ManyToMany relationships
if ismethod(current_objects.all):
return current_objects.all()
return []
if not hasattr(current_objects, '__iter__'):
current_objects = [current_objects]
return current_objects
def prepare_template(self, obj):
"""
Flattens an object for indexing.
This loads a template
(``search/indexes/{app_label}/{model_name}_{field_name}.txt``) and
returns the result of rendering that template. ``object`` will be in
its context.
"""
if self.instance_name is None and self.template_name is None:
raise SearchFieldError(
"This field requires either its instance_name variable to be "
"populated or an explicit template_name in order to load the "
"correct template."
)
if self.template_name is not None:
template_names = self.template_name
if not isinstance(template_names, (list, tuple)):
template_names = [template_names]
else:
app_label, model_name = get_model_ct_tuple(obj)
template_names = [
'search/indexes/%s/%s_%s.txt' %
(app_label, model_name, self.instance_name)
]
t = loader.select_template(template_names)
return t.render({'object': obj})
def convert(self, value):
"""
Handles conversion between the data found and the type of the field.
Extending classes should override this method and provide correct
data coercion.
"""
return value
class CharField(SearchField):
field_type = 'string'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetCharField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
return six.text_type(value)
class LocationField(SearchField):
field_type = 'location'
def prepare(self, obj):
value = super().prepare(obj)
if value is None or value == []:
return None
pnt = ensure_point(value)
pnt_lng, pnt_lat = pnt.get_coords()
return "%s,%s" % (pnt_lat, pnt_lng)
def convert(self, value):
if value is None or value == []:
return None
if hasattr(value, 'geom_type'):
value = ensure_point(value)
return value
if isinstance(value, six.string_types):
lat, lng = value.split(',')
elif isinstance(value, (list, tuple)):
# GeoJSON-alike
lat, lng = value[1], value[0]
elif isinstance(value, dict):
lat = value.get('lat', 0)
lng = value.get('lon', 0)
value = Point(float(lng), float(lat))
return value
class NgramField(CharField):
field_type = 'ngram'
def __init__(self, **kwargs):
if kwargs.get('faceted') is True:
raise SearchFieldError("%s can not be faceted." %
self.__class__.__name__)
super().__init__(**kwargs)
class EdgeNgramField(NgramField):
field_type = 'edge_ngram'
class IntegerField(SearchField):
field_type = 'integer'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetIntegerField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
return int(value)
class FloatField(SearchField):
field_type = 'float'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetFloatField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
return float(value)
class DecimalField(SearchField):
field_type = 'string'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetDecimalField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
return six.text_type(value)
class BooleanField(SearchField):
field_type = 'boolean'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetBooleanField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
return bool(value)
class DateField(SearchField):
field_type = 'date'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetDateField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
if isinstance(value, six.string_types):
match = DATE_REGEX.search(value)
if match:
data = match.groupdict()
return datetime_safe.date(
int(data['year']), int(data['month']), int(data['day']))
raise SearchFieldError(
"Date provided to '%s' field doesn't appear to be a valid "
"date string: '%s'"
% (self.instance_name, value))
return value
class DateTimeField(SearchField):
field_type = 'datetime'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetDateTimeField
super().__init__(**kwargs)
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
if isinstance(value, six.string_types):
match = DATETIME_REGEX.search(value)
if match:
data = match.groupdict()
return datetime_safe.datetime(int(data['year']),
int(data['month']),
int(data['day']),
int(data['hour']),
int(data['minute']),
int(data['second']))
raise SearchFieldError(
"Datetime provided to '%s' field doesn't appear to be a valid "
"datetime string: '%s'"
% (self.instance_name, value))
return value
class MultiValueField(SearchField):
field_type = 'string'
def __init__(self, **kwargs):
if kwargs.get('facet_class') is None:
kwargs['facet_class'] = FacetMultiValueField
if kwargs.get('use_template') is True:
raise SearchFieldError(
"'%s' fields can not use templates to prepare their data." %
self.__class__.__name__)
super().__init__(**kwargs)
self.is_multivalued = True
def prepare(self, obj):
return self.convert(super().prepare(obj))
def convert(self, value):
if value is None or value == []:
return None
if hasattr(value, '__iter__') and not isinstance(value, six.text_type):
return value
return [value]
class FacetField(SearchField):
"""
``FacetField`` is slightly different than the other fields because it can
work in conjunction with other fields as its data source.
Accepts an optional ``facet_for`` kwarg, which should be the field name
(not ``index_fieldname``) of the field it should pull data from.
"""
instance_name = None
def __init__(self, **kwargs):
handled_kwargs = self.handle_facet_parameters(kwargs)
super().__init__(**handled_kwargs)
def handle_facet_parameters(self, kwargs):
if kwargs.get('faceted', False):
raise SearchFieldError(
"FacetField (%s) does not accept the 'faceted' argument." %
self.instance_name)
if not kwargs.get('null', True):
raise SearchFieldError(
"FacetField (%s) does not accept False for the 'null' "
"argument."
% self.instance_name)
if not kwargs.get('indexed', True):
raise SearchFieldError(
"FacetField (%s) does not accept False for the 'indexed' "
"argument."
% self.instance_name)
if kwargs.get('facet_class'):
raise SearchFieldError(
"FacetField (%s) does not accept the 'facet_class' argument." %
self.instance_name)
self.facet_for = None
self.facet_class = None
# Make sure the field is nullable.
kwargs['null'] = True
if 'facet_for' in kwargs:
self.facet_for = kwargs['facet_for']
del kwargs['facet_for']
return kwargs
def get_facet_for_name(self):
return self.facet_for or self.instance_name
class FacetCharField(FacetField, CharField):
pass
class FacetIntegerField(FacetField, IntegerField):
pass
class FacetFloatField(FacetField, FloatField):
pass
class FacetDecimalField(FacetField, DecimalField):
pass
class FacetBooleanField(FacetField, BooleanField):
pass
class FacetDateField(FacetField, DateField):
pass
class FacetDateTimeField(FacetField, DateTimeField):
pass
class FacetMultiValueField(FacetField, MultiValueField):
pass