-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathviews.py
154 lines (118 loc) · 4.66 KB
/
views.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
# -*- coding: utf-8 -*-
import django_filters
from rest_framework import viewsets
from serializers import CountrySerializer, StateSerializer, CitySerializer, CityAliasSerializer, CountryAliasSerializer
from models import Country, CountryAlias, State, City, CityAlias
from rest_framework import filters
from dal import autocomplete
class CountryFilter(filters.FilterSet):
name = django_filters.CharFilter(name="name", lookup_type='lowermatch')
alias = django_filters.CharFilter(name="countryalias__name", lookup_type='lowermatch')
class Meta:
model = Country
fields = ('id', 'name', 'acronym',)
class CountryViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
queryset = Country.objects.all()
serializer_class = CountrySerializer
filter_class = CountryFilter
def get_queryset(self):
qs = super(CountryViewSet, self).get_queryset()
return qs.distinct()
#
# django_filters for CoutryAliasViewSet
#
class CountryAliasFilter(filters.FilterSet):
name = django_filters.CharFilter(name="name", lookup_type='lowermatch')
class Meta:
model = CountryAlias
fields = ('id', 'name', 'country', 'country__name', 'country__acronym',)
class CountryAliasViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CountryAliasFilter
queryset = CountryAlias.objects.all()\
.select_related('country')
serializer_class = CountryAliasSerializer
def get_queryset(self):
qs = super(CountryAliasViewSet, self).get_queryset()
if 'name' in self.request.REQUEST:
value = self.request.REQUEST.get('name', '')
field = '"%s"."name"' % CountryAlias._meta.db_table
function = 'levenshtein(%s, %s)' % ("%s", field)
qs = qs.extra(
select={'weight': function, },
select_params=(value,)
).distinct().order_by('weight')
return qs
class StateViewSet(viewsets.ReadOnlyModelViewSet):
queryset = State.objects.all()
serializer_class = StateSerializer
#
# django_filters for CityViewSet
#
class CityFilter(filters.FilterSet):
alias = django_filters.CharFilter(name="cityalias__name", lookup_type='lowermatch')
class Meta:
model = City
fields = ('id', 'name', 'country__acronym', 'alias',)
class CityViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CityFilter
queryset = City.objects.all()\
.select_related('state', 'country')\
.order_by('name') \
.distinct()
serializer_class = CitySerializer
def get_queryset(self):
qs = super(CityViewSet, self).get_queryset()
if 'alias' in self.request.REQUEST:
value = self.request.REQUEST.get('alias', '')
field = '"%s"."name"' % City._meta.db_table
function = 'levenshtein(%s, %s)' % ("%s", field)
qs = qs.extra(
select={'weight': function, },
select_params=(value,)
).distinct().order_by('weight')
return qs
#
# django_filters for CityViewSet
#
class CityAliasFilter(filters.FilterSet):
name = django_filters.CharFilter(name="name", lookup_type='lowermatch')
class Meta:
model = CityAlias
fields = ('id', 'name', 'city__name', 'city__country',
'city__country__acronym',)
class CityAliasViewSet(viewsets.ReadOnlyModelViewSet):
filter_backends = (filters.DjangoFilterBackend,)
filter_class = CityAliasFilter
queryset = CityAlias.objects.all()\
.select_related('city', 'city__country')\
.order_by('city__name')
serializer_class = CityAliasSerializer
def get_queryset(self):
qs = super(CityAliasViewSet, self).get_queryset()
if 'name' in self.request.REQUEST:
value = self.request.REQUEST.get('name', '')
field = '"%s"."name"' % CityAlias._meta.db_table
function = 'levenshtein(%s, %s)' % ("%s", field)
qs = qs.extra(
select={'weight': function, },
select_params=(value,)
).distinct().order_by('weight')
return qs
#
# django-autocomplete-light for admin
#
class CountryAutocompleteView(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Country.objects.all()
if self.q:
qs = qs.filter(name__lowermatch=self.q)
return qs
class CityAutocompleteView(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = City.objects.all()
if self.q:
qs = qs.filter(name__lowermatch=self.q)
return qs