mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-06-21 18:08:21 +02:00
Autocomplete
This commit is contained in:
@ -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,
|
||||
}),
|
||||
}
|
||||
|
||||
|
@ -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/<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'),
|
||||
]
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user