forked from GamesDoneQuick/donation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlookups.py
176 lines (130 loc) · 4.37 KB
/
lookups.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
from ajax_select import LookupChannel
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.urls import reverse
from django.utils.html import escape
from django.utils.safestring import mark_safe
import tracker.search_filters as filters
import tracker.viewutil as viewutil
from tracker.models import (
Bid,
Country,
CountryRegion,
Donation,
Donor,
Event,
Prize,
Runner,
SpeedRun,
)
"""
In order to use these lookups properly with the admin, you will need to install/enable the 'ajax_select'
django module, and also add an AJAX_LOOKUP_CHANNELS table (the table of all
lookups used by this application are in tracker/ajax_lookup_channels.py)
They can be imported with the line:
from tracker.ajax_lookup_channels import AJAX_LOOKUP_CHANNELS
"""
class UserLookup(LookupChannel):
def __init__(self, *args, **kwargs):
self.model = get_user_model()
super(UserLookup, self).__init__(*args, **kwargs)
def get_query(self, q, request):
if not request.user.has_perm('tracker.can_search'):
raise PermissionDenied
return self.model.objects.filter(username__icontains=q)
def get_result(self, obj):
return obj.username
def format_match(self, obj):
return escape(obj.username)
def can_add(self, user, source_model):
# avoid in-line addition of users by accident
return False
class CountryLookup(LookupChannel):
model = Country
def get_query(self, q, request):
return Country.objects.filter(name__icontains=q)
def get_result(self, obj):
return str(obj)
def format_match(self, obj):
return escape(str(obj))
def can_add(self, user, source_model):
# Presumably, we don't want to add countries typically
return False
class CountryRegionLookup(LookupChannel):
model = CountryRegion
def get_query(self, q, request):
return CountryRegion.objects.filter(
Q(name__icontains=q) | Q(country__name__icontains=q)
)
def get_result(self, obj):
return str(obj)
def format_match(self, obj):
return escape(str(obj))
class GenericLookup(LookupChannel):
useLock = False
useEvent = False
extra_params = {}
def get_extra_params(self, request):
return self.extra_params
def get_query(self, q, request):
params = {'q': q}
params.update(self.get_extra_params(request))
event = viewutil.get_selected_event(request)
if event and self.useEvent:
params['event'] = event.id
model = getattr(self, 'modelName', self.model)
if self.useLock and not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
return filters.run_model_query(model, params, request.user)
def get_result(self, obj):
return str(obj)
def format_match(self, obj):
return escape(str(obj))
# returning the admin URL reduces the genericity of our solution a little bit, but this can be solved
# by using distinct lookups for admin/non-admin applications (which we should do regardless since
# non-admin search should be different)
def format_item_display(self, obj):
result = '<a href="{0}">{1}</a>'.format(
reverse(
'admin:tracker_{0}_change'.format(obj._meta.model_name), args=[obj.pk]
),
escape(str(obj)),
)
return mark_safe(result)
class BidLookup(GenericLookup):
useEvent = True
useLock = True
model = Bid
modelName = 'bid'
extra_params = {'feed': 'all'}
class AllBidLookup(GenericLookup):
useEvent = True
useLock = True
model = Bid
modelName = 'allbids'
extra_params = {'feed': 'all'}
class BidTargetLookup(GenericLookup):
model = Bid
modelName = 'bidtarget'
useEvent = True
useLock = True
extra_params = {'feed': 'all'}
class DonationLookup(GenericLookup):
model = Donation
useEvent = True
useLock = True
class DonorLookup(GenericLookup):
model = Donor
class PrizeLookup(GenericLookup):
model = Prize
useEvent = True
class RunLookup(GenericLookup):
model = SpeedRun
useEvent = True
useLock = True
class EventLookup(GenericLookup):
model = Event
useLock = True
class RunnerLookup(GenericLookup):
model = Runner