File: serializers.py

package info (click to toggle)
drf-haystack 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: python: 2,608; makefile: 147
file content (63 lines) | stat: -rw-r--r-- 1,853 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

from datetime import datetime, timedelta
from rest_framework.serializers import HyperlinkedIdentityField

from drf_haystack.serializers import HaystackSerializer, HaystackFacetSerializer, HighlighterMixin
from .search_indexes import MockPersonIndex, MockLocationIndex


class SearchSerializer(HaystackSerializer):

    class Meta:
        index_classes = [MockPersonIndex, MockLocationIndex]
        fields = [
            "firstname", "lastname", "birthdate", "full_name", "text",
            "address", "city", "zip_code", "highlighted"
        ]


class HighlighterSerializer(HighlighterMixin, HaystackSerializer):

    highlighter_css_class = "my-highlighter-class"
    highlighter_html_tag = "em"

    class Meta:
        index_classes = [MockPersonIndex, MockLocationIndex]
        fields = [
            "firstname", "lastname", "full_name",
            "address", "city", "zip_code", "coordinates"
        ]


class MoreLikeThisSerializer(HaystackSerializer):

    more_like_this = HyperlinkedIdentityField(view_name="search-person-mlt-more-like-this", read_only=True)

    class Meta:
        index_classes = [MockPersonIndex]
        fields = [
            "firstname", "lastname", "full_name",
            "autocomplete"
        ]


class MockPersonFacetSerializer(HaystackFacetSerializer):

    serialize_objects = True

    class Meta:
        index_classes = [MockPersonIndex]
        fields = ["firstname", "lastname", "created", "letters"]
        field_options = {
            "firstname": {},
            "lastname": {},
            "created": {
                "start_date": datetime.now() - timedelta(days=3 * 365),
                "end_date": datetime.now(),
                "gap_by": "day",
                "gap_amount": 10
            }
        }