Debian Codegraph @

drf_haystack/fields.py

raw
1 # -*- coding: utf-8 -*-
2
3 from __future__ import absolute_import, unicode_literals
4
5 import six
6 from rest_framework import fields
7
8
9 class DRFHaystackFieldMixin(object):
10 prefix_field_names = False
11
12 def __init__(self, **kwargs):
13 self.prefix_field_names = kwargs.pop('prefix_field_names', False)
14 super(DRFHaystackFieldMixin, self).__init__(**kwargs)
15
16 def bind(self, field_name, parent):
17 """
18 Initializes the field name and parent for the field instance.
19 Called when a field is added to the parent serializer instance.
20 Taken from DRF and modified to support drf_haystack multiple index
21 functionality.
22 """
23
24 # In order to enforce a consistent style, we error if a redundant
25 # 'source' argument has been used. For example:
26 # my_field = serializer.CharField(source='my_field')
27 assert self.source != field_name, (
28 "It is redundant to specify `source='%s'` on field '%s' in "
29 "serializer '%s', because it is the same as the field name. "
30 "Remove the `source` keyword argument." %
31 (field_name, self.__class__.__name__, parent.__class__.__name__)
32 )
33
34 self.field_name = field_name
35 self.parent = parent
36
37 # `self.label` should default to being based on the field name.
38 if self.label is None:
39 self.label = field_name.replace('_', ' ').capitalize()
40
41 # self.source should default to being the same as the field name.
42 if self.source is None:
43 self.source = self.convert_field_name(field_name)
44
45 # self.source_attrs is a list of attributes that need to be looked up
46 # when serializing the instance, or populating the validated data.
47 if self.source == '*':
48 self.source_attrs = []
49 else:
50 self.source_attrs = self.source.split('.')
51
52 def convert_field_name(self, field_name):
53 if not self.prefix_field_names:
54 return field_name
55 return field_name.split("__")[-1]
56
57
58 class HaystackBooleanField(DRFHaystackFieldMixin, fields.BooleanField):
59 pass
60
61
62 class HaystackCharField(DRFHaystackFieldMixin, fields.CharField):
63 pass
64
65
66 class HaystackDateField(DRFHaystackFieldMixin, fields.DateField):
67 pass
68
69
70 class HaystackDateTimeField(DRFHaystackFieldMixin, fields.DateTimeField):
71 pass
72
73
74 class HaystackDecimalField(DRFHaystackFieldMixin, fields.DecimalField):
75 pass
76
77
78 class HaystackFloatField(DRFHaystackFieldMixin, fields.FloatField):
79 pass
80
81
82 class HaystackIntegerField(DRFHaystackFieldMixin, fields.IntegerField):
83 pass
84
85
86 class HaystackMultiValueField(DRFHaystackFieldMixin, fields.ListField):
87 pass
88
89
90 class FacetDictField(fields.DictField):
91 """
92 A special DictField which passes the key attribute down to the children's
93 ``to_representation()`` in order to let the serializer know what field they're
94 currently processing.
95 """
96
97 def to_representation(self, value):
98 return dict(
99 [(six.text_type(key), self.child.to_representation(key, val))
100 for key, val in value.items()
101 ]
102 )
103
104
105 class FacetListField(fields.ListField):
106 """
107 The ``FacetListField`` just pass along the key derived from
108 ``FacetDictField``.
109 """
110
111 def to_representation(self, key, data):
112 return [self.child.to_representation(key, item) for item in data]
113
114