-
Notifications
You must be signed in to change notification settings - Fork 1
/
lookups.py
163 lines (131 loc) · 5.01 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
from ajax_select import LookupChannel
from django.urls import reverse
from django.utils.html import escape
from django.db.models import Q
from django.utils.safestring import mark_safe
from django.contrib.auth import get_user_model
from tracker.models import *
import tracker.viewutil as viewutil
import tracker.filters as filters
"""
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):
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):
def __init__(self, *args, **kwargs):
self.model = Country
super(CountryLookup,self).__init__(*args, **kwargs)
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):
def __init__(self, *args, **kwargs):
self.model = CountryRegion
super(CountryRegionLookup, self).__init__(*args, **kwargs)
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):
def get_query(self,q,request):
params = {'q': q}
event = viewutil.get_selected_event(request)
if event and self.useEvent:
params['event'] = event.id
model = self.model
if hasattr(self, 'modelName'):
model = self.modelName
if self.useLock and not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
return filters.run_model_query(model, params, user=request.user, mode='admin')
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):
def __init__(self, *args, **kwargs):
self.model = Bid
self.modelName = 'bid'
self.useEvent = True
self.useLock = True
super(BidLookup,self).__init__(*args, **kwargs)
class AllBidLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Bid
self.modelName = 'allbids'
self.useEvent = True
self.useLock = True
super(AllBidLookup,self).__init__(*args, **kwargs)
class BidTargetLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Bid
self.modelName = 'bidtarget'
self.useEvent = True
self.useLock = True
super(BidTargetLookup,self).__init__(*args, **kwargs)
class DonationLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Donation
self.useEvent = True
self.useLock = True
super(DonationLookup,self).__init__(*args, **kwargs)
class DonorLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Donor
self.useEvent = False
self.useLock = False
super(DonorLookup,self).__init__(*args, **kwargs)
class PrizeLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Prize
self.useEvent = True
self.useLock = False
super(PrizeLookup,self).__init__(*args, **kwargs)
class RunLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = SpeedRun
self.useEvent = True
self.useLock = True
super(RunLookup,self).__init__(*args, **kwargs)
class EventLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Event
self.useEvent = False
self.useLock = True
super(EventLookup,self).__init__(*args, **kwargs)
class RunnerLookup(GenericLookup):
def __init__(self, *args, **kwargs):
self.model = Runner
self.useEvent = False
self.useLock = False
super(RunnerLookup,self).__init__(*args, **kwargs)