Autocomplete

This commit is contained in:
Yohann D'ANELLO 2020-02-08 20:23:17 +01:00
parent db218a2783
commit c053235996
7 changed files with 62 additions and 8 deletions

View File

@ -1,9 +1,33 @@
#!/usr/bin/env python #!/usr/bin/env python
from dal import autocomplete
from django import forms from django import forms
from .models import TransactionTemplate from .models import Transaction, TransactionTemplate
class TransactionTemplateForm(forms.ModelForm): class TransactionTemplateForm(forms.ModelForm):
class Meta: class Meta:
model = TransactionTemplate model = TransactionTemplate
fields ='__all__' fields ='__all__'
widgets = {
'destination': autocomplete.ModelSelect2(url='note:note_autocomplete',
attrs={
'data-placeholder': 'Note ...',
'data-minimum-input-length': 1,
}),
}
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = ('destination', 'reason', 'amount',)
widgets = {
'destination': autocomplete.ModelSelect2(url='note:note_autocomplete',
attrs={
'data-placeholder': 'Note ...',
'data-minimum-input-length': 1,
}),
}

View File

@ -5,11 +5,13 @@
from django.urls import path from django.urls import path
from . import views from . import views
from .models import Note
app_name = 'note' app_name = 'note'
urlpatterns = [ urlpatterns = [
path('transfer/', views.TransactionCreate.as_view(), name='transfer'), path('transfer/', views.TransactionCreate.as_view(), name='transfer'),
path('buttons/create/',views.TransactionTemplateCreateView.as_view(),name='template_create'), path('buttons/create/',views.TransactionTemplateCreateView.as_view(),name='template_create'),
path('buttons/update/<int:pk>/',views.TransactionTemplateUpdateView.as_view(),name='template_update'), path('buttons/update/<int:pk>/',views.TransactionTemplateUpdateView.as_view(),name='template_update'),
path('buttons/',views.TransactionTemplateListView.as_view(),name='template_list') path('buttons/',views.TransactionTemplateListView.as_view(),name='template_list'),
path('note-autocomplete/', views.NoteAutocomplete.as_view(model=Note),name='note_autocomplete'),
] ]

View File

@ -2,12 +2,14 @@
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay # Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from dal import autocomplete
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, ListView, DetailView, UpdateView from django.views.generic import CreateView, ListView, DetailView, UpdateView
from .models import Transaction,TransactionTemplate from .models import Transaction, TransactionTemplate, Note
from .forms import TransactionTemplateForm from .forms import TransactionForm, TransactionTemplateForm
class TransactionCreate(LoginRequiredMixin, CreateView): class TransactionCreate(LoginRequiredMixin, CreateView):
""" """
@ -16,7 +18,7 @@ class TransactionCreate(LoginRequiredMixin, CreateView):
TODO: If user have sufficient rights, they can transfer from an other note TODO: If user have sufficient rights, they can transfer from an other note
""" """
model = Transaction model = Transaction
fields = ('destination', 'amount', 'reason') form_class = TransactionForm
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
""" """
@ -27,6 +29,20 @@ class TransactionCreate(LoginRequiredMixin, CreateView):
'to one or others') 'to one or others')
return context return context
class NoteAutocomplete(autocomplete.Select2QuerySetView):
"""
Auto complete note by aliases
"""
def get_queryset(self):
qs = Note.objects.all()
if self.q:
qs = qs.filter(Q(alias__name__regex=self.q) | Q(alias__normalized_name__regex=self.q))
return qs
class TransactionTemplateCreateView(LoginRequiredMixin,CreateView): class TransactionTemplateCreateView(LoginRequiredMixin,CreateView):
""" """
Create TransactionTemplate Create TransactionTemplate

View File

@ -52,6 +52,9 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
# API # API
'rest_framework', 'rest_framework',
# Autocomplete
'dal',
'dal_select2',
# Note apps # Note apps
'activity', 'activity',

View File

@ -3,11 +3,14 @@ chardet==3.0.4
defusedxml==0.6.0 defusedxml==0.6.0
Django==2.2.3 Django==2.2.3
django-allauth==0.39.1 django-allauth==0.39.1
django-autocomplete-light==3.3.0
django-crispy-forms==1.7.2 django-crispy-forms==1.7.2
django-extensions==2.1.9 django-extensions==2.1.9
django-filter==2.2.0 django-filter==2.2.0
django-guardian==2.1.0 django-guardian==2.1.0
django-polymorphic==2.0.3 django-polymorphic==2.0.3
djangorestframework==3.9.0
django-rest-polymorphic==0.1.8
django-reversion==3.0.3 django-reversion==3.0.3
django-tables2==2.1.0 django-tables2==2.1.0
docutils==0.14 docutils==0.14
@ -20,6 +23,4 @@ requests==2.22.0
requests-oauthlib==1.2.0 requests-oauthlib==1.2.0
six==1.12.0 six==1.12.0
sqlparse==0.3.0 sqlparse==0.3.0
urllib3==1.25.3 urllib3==1.25.3
djangorestframework==3.9.0
django-rest-polymorphic==0.1.8

View File

@ -35,3 +35,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
<input type="submit" value="{% trans 'Transfer' %}"> <input type="submit" value="{% trans 'Transfer' %}">
</form> </form>
{% endblock %} {% endblock %}
{% block extracss %}
{{ form.media }}
{% endblock extracss %}

View File

@ -9,3 +9,7 @@
<button class="btn btn-primary" type="submit">Submit</button> <button class="btn btn-primary" type="submit">Submit</button>
</form> </form>
{% endblock %} {% endblock %}
{% block extracss %}
{{ form.media }}
{% endblock extracss %}