diff --git a/apps/note/forms.py b/apps/note/forms.py index d74fa5b4..2e814be6 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -1,9 +1,33 @@ #!/usr/bin/env python +from dal import autocomplete from django import forms -from .models import TransactionTemplate +from .models import Transaction, TransactionTemplate class TransactionTemplateForm(forms.ModelForm): class Meta: model = TransactionTemplate 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, + }), + } + diff --git a/apps/note/urls.py b/apps/note/urls.py index 5e423d46..d4e390a1 100644 --- a/apps/note/urls.py +++ b/apps/note/urls.py @@ -5,11 +5,13 @@ from django.urls import path from . import views +from .models import Note app_name = 'note' urlpatterns = [ path('transfer/', views.TransactionCreate.as_view(), name='transfer'), path('buttons/create/',views.TransactionTemplateCreateView.as_view(),name='template_create'), path('buttons/update//',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'), ] diff --git a/apps/note/views.py b/apps/note/views.py index 08f4f630..716d16ff 100644 --- a/apps/note/views.py +++ b/apps/note/views.py @@ -2,12 +2,14 @@ # Copyright (C) 2018-2019 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from dal import autocomplete from django.contrib.auth.mixins import LoginRequiredMixin +from django.db.models import Q from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, ListView, DetailView, UpdateView -from .models import Transaction,TransactionTemplate -from .forms import TransactionTemplateForm +from .models import Transaction, TransactionTemplate, Note +from .forms import TransactionForm, TransactionTemplateForm 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 """ model = Transaction - fields = ('destination', 'amount', 'reason') + form_class = TransactionForm def get_context_data(self, **kwargs): """ @@ -27,6 +29,20 @@ class TransactionCreate(LoginRequiredMixin, CreateView): 'to one or others') 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): """ Create TransactionTemplate diff --git a/note_kfet/settings/base.py b/note_kfet/settings/base.py index 17283a95..d7061efd 100644 --- a/note_kfet/settings/base.py +++ b/note_kfet/settings/base.py @@ -52,6 +52,9 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', # API 'rest_framework', + # Autocomplete + 'dal', + 'dal_select2', # Note apps 'activity', diff --git a/requirements.txt b/requirements.txt index 30753fd2..3be0a4d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,11 +3,14 @@ chardet==3.0.4 defusedxml==0.6.0 Django==2.2.3 django-allauth==0.39.1 +django-autocomplete-light==3.3.0 django-crispy-forms==1.7.2 django-extensions==2.1.9 django-filter==2.2.0 django-guardian==2.1.0 django-polymorphic==2.0.3 +djangorestframework==3.9.0 +django-rest-polymorphic==0.1.8 django-reversion==3.0.3 django-tables2==2.1.0 docutils==0.14 @@ -20,6 +23,4 @@ requests==2.22.0 requests-oauthlib==1.2.0 six==1.12.0 sqlparse==0.3.0 -urllib3==1.25.3 -djangorestframework==3.9.0 -django-rest-polymorphic==0.1.8 +urllib3==1.25.3 \ No newline at end of file diff --git a/templates/note/transaction_form.html b/templates/note/transaction_form.html index ff8504bc..2a480fc8 100644 --- a/templates/note/transaction_form.html +++ b/templates/note/transaction_form.html @@ -35,3 +35,7 @@ SPDX-License-Identifier: GPL-2.0-or-later {% endblock %} + +{% block extracss %} + {{ form.media }} +{% endblock extracss %} diff --git a/templates/note/transactiontemplate_form.html b/templates/note/transactiontemplate_form.html index 3fc2dd8b..5911d5d2 100644 --- a/templates/note/transactiontemplate_form.html +++ b/templates/note/transactiontemplate_form.html @@ -9,3 +9,7 @@ {% endblock %} + +{% block extracss %} + {{ form.media }} +{% endblock extracss %} \ No newline at end of file