forked from MapStory/mapstory-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplesearch.py
97 lines (82 loc) · 3 KB
/
simplesearch.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
from mapstory.models import get_view_cnts
from mapstory.models import get_ratings
from mapstory.models import Topic
from mapstory.models import Section
from mapstory.models import ContactDetail
from mapstory.models import Org
from mapstory.models import ProfileIncomplete
from geonode.maps.models import Layer
from geonode.maps.models import Map
display_names = {
'map' : 'MapStory',
'layer' : 'StoryLayer',
'user' : 'StoryTeller'
}
def extra_context(context):
context['sections'] = Section.objects.all()
def process_search_results(normalizers):
'''Generic content types are _much_ faster to deal with in bulk'''
if not normalizers: return normalizers
model = type(normalizers[0].o)
cnts = get_view_cnts(model)
ratings = get_ratings(model)
for n in normalizers:
n.views = cnts.get(n.o.id, 0)
n.rating = float(ratings.get(n.o.id, 0))
return normalizers
owner_query_fields = ['blurb','organization','biography']
def owner_query(query, kw):
if kw['bysection']: return None
superuser = kw['user'] and kw['user'].is_superuser
if 'org' == kw.get('subtype'):
# make sure any 'bad' profiles get ignored
q = Org.objects.select_related().filter(user__isnull=False)
else:
q = ContactDetail.objects.all().select_related()
if 'owner' == kw.get('subtype'):
q = q.filter(org__isnull=True)
# make sure any 'bad' profiles get ignored
q = q.filter(user__isnull=False)
# don't fetch these, they won't be used
# causes error when using on Org...
q = q.defer('blurb', 'biography')
# super users can see incomplete profiles
if not superuser:
user = kw['user']
incomplete = ProfileIncomplete.objects.all()
if user:
# if the user is searching, show their profile even if incomplete
incomplete = incomplete.exclude(user__username=user.username)
q = q.exclude(user__id__in=incomplete.values('user'))
return q
def owner_rank_rules():
return (ContactDetail,
['blurb', 5, 2],
['biography', 5, 2])
def _initial_query(model, kw):
user = kw['user']
if user and user.is_superuser:
q = model.objects.all()
else:
q = model.objects.filter(publish__status='Public')
if user:
q = q | model.objects.filter(owner=user)
return q
def layer_query(query, kw):
q = _initial_query(Layer, kw)
q = q.only('title','date')
bysection = kw.get('bysection')
if bysection:
q = q.filter(topic__in=Topic.objects.filter(section__id=bysection))
# @todo once supported via UI - this should be an OR with the section filter
bytopic = kw.get('bytopic')
if bytopic:
q = q.filter(topic_category = bytopic)
return q
def map_query(query, kw):
q = _initial_query(Map, kw)
q = q.only('title','last_modified')
bysection = kw.get('bysection')
if bysection:
q = q.filter(topic__in=Topic.objects.filter(section__id=bysection))
return q