-
Notifications
You must be signed in to change notification settings - Fork 27
/
documents.py
44 lines (35 loc) · 1.4 KB
/
documents.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
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from core.common.utils import jsonify_safe, flatten_dict
from core.users.models import UserProfile
@registry.register_document
class UserProfileDocument(Document):
class Index:
name = 'user_profiles'
settings = {'number_of_shards': 1, 'number_of_replicas': 0}
last_update = fields.DateField(attr='updated_at')
date_joined = fields.DateField(attr='created_at')
username = fields.KeywordField(attr='username', normalizer='lowercase')
location = fields.KeywordField(attr='location', normalizer='lowercase')
company = fields.KeywordField(attr='company', normalizer='lowercase')
name = fields.KeywordField(attr='name', normalizer='lowercase')
extras = fields.ObjectField(dynamic=True)
org = fields.ListField(fields.KeywordField())
class Django:
model = UserProfile
fields = [
'is_active',
'is_superuser',
'is_staff',
]
@staticmethod
def prepare_extras(instance):
value = {}
if instance.extras:
value = jsonify_safe(instance.extras)
if isinstance(value, dict):
value = flatten_dict(value)
return value or {}
@staticmethod
def prepare_org(instance):
return list(instance.organizations.values_list('mnemonic', flat=True))