2018-10-17 09:39:31 +00:00
|
|
|
from dal import autocomplete
|
|
|
|
|
|
|
|
from .models import Tag
|
|
|
|
|
|
|
|
|
|
|
|
class AuthenticatedUserCreateMixin():
|
|
|
|
''' By default, autocomplete-light only allows creation of
|
|
|
|
choices if the user has add permission on the model. However,
|
|
|
|
we need to allow any authenticated user to create tags
|
|
|
|
'''
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
print("i am called", request.user)
|
|
|
|
return request.user.is_authenticated
|
|
|
|
|
|
|
|
|
|
|
|
class TagAutocomplete(
|
|
|
|
AuthenticatedUserCreateMixin, autocomplete.Select2QuerySetView):
|
|
|
|
def get_queryset(self):
|
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
return Tag.objects.none()
|
|
|
|
|
|
|
|
qs = Tag.objects.all()
|
|
|
|
|
|
|
|
if self.q:
|
|
|
|
qs = qs.filter(name__icontains=self.q)
|
|
|
|
|
2018-10-20 07:48:55 +00:00
|
|
|
return qs
|