added search on Sujet with django-watson
This commit is contained in:
@@ -2,3 +2,5 @@
|
|||||||
|
|
||||||
* django-bootstrap3
|
* django-bootstrap3
|
||||||
* django_select2
|
* django_select2
|
||||||
|
|
||||||
|
* django-watson
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
from django.views.generic.edit import FormMixin, ProcessFormView
|
|
||||||
from django import forms
|
|
||||||
|
|
||||||
from django.shortcuts import redirect
|
|
||||||
|
|
||||||
class SearchForm(forms.Form):
|
|
||||||
|
|
||||||
search_text = forms.CharField(64, widget=forms.widgets.TextInput(attrs={'placeholder':"NotYetImplemented"}))
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
kwargs['prefix'] = 'search'
|
|
||||||
return super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
def clean(self):
|
|
||||||
super().clean()
|
|
||||||
#Do search and store result
|
|
||||||
self.result = None
|
|
||||||
|
|
||||||
def is_valid(self):
|
|
||||||
valid = super().is_valid()
|
|
||||||
if not self.result:
|
|
||||||
return False
|
|
||||||
return valid
|
|
||||||
|
|
||||||
class SearchFormMixin(FormMixin):
|
|
||||||
""" Add 'search_form' to context.
|
|
||||||
Made compatible with NoteFormMixin using 'prefix' argument in get_form
|
|
||||||
"""
|
|
||||||
def get_form(self, prefix):
|
|
||||||
if prefix == 'search':
|
|
||||||
return SearchForm()
|
|
||||||
return super().get_form(prefix)
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
|
||||||
context = super().get_context_data(**kwargs)
|
|
||||||
context['search_form'] = self.get_form('search')
|
|
||||||
return context
|
|
||||||
|
|
||||||
""" What to do ?
|
|
||||||
|
|
||||||
- We shall use 'django-watson', need to install it
|
|
||||||
|
|
||||||
- Create view to parse search_form data then redirect
|
|
||||||
- Link form in menu_sujets to this view
|
|
||||||
"""
|
|
||||||
|
|
||||||
class SearchFormProcessView(ProcessFormView):
|
|
||||||
pass
|
|
||||||
@@ -3,12 +3,6 @@
|
|||||||
<a href="{% url 'sujets:liste' %}">Liste des sujets
|
<a href="{% url 'sujets:liste' %}">Liste des sujets
|
||||||
<span class="pull-right">{% bootstrap_icon "list" %}</span></a>
|
<span class="pull-right">{% bootstrap_icon "list" %}</span></a>
|
||||||
</li>
|
</li>
|
||||||
{% if search_form %}
|
|
||||||
<form class="nav navbar-form app-menu text-center" method="POST" action="search/">
|
|
||||||
{% bootstrap_form search_form show_label=False %}
|
|
||||||
{% bootstrap_button 'Chercher' button_type='submit' %}
|
|
||||||
</form>
|
|
||||||
{% endif %}
|
|
||||||
{% if user.is_superuser %}
|
{% if user.is_superuser %}
|
||||||
<li class="dropdown app-menu">
|
<li class="dropdown app-menu">
|
||||||
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
|
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ from sujets.models import Sujet
|
|||||||
from .forms import *
|
from .forms import *
|
||||||
from notes.mixins import NoteFormMixin
|
from notes.mixins import NoteFormMixin
|
||||||
from notes.forms import AutoNoteForm
|
from notes.forms import AutoNoteForm
|
||||||
from .search import SearchFormMixin, SearchFormProcessView
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
from website import decorators as website
|
from website import decorators as website
|
||||||
webpage = website.webpage(
|
webpage = website.webpage(
|
||||||
@@ -18,7 +17,7 @@ webpage = website.webpage(
|
|||||||
|
|
||||||
|
|
||||||
@webpage
|
@webpage
|
||||||
class IndexView(SearchFormMixin, NoteFormMixin, generic.TemplateView):
|
class IndexView(NoteFormMixin, generic.TemplateView):
|
||||||
class PageInfo:
|
class PageInfo:
|
||||||
title = "Suivi des bénéficiaires"
|
title = "Suivi des bénéficiaires"
|
||||||
header = "Suivi"
|
header = "Suivi"
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
default_app_config = 'sujets.apps.SujetsConfig'
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from watson import search as watson
|
||||||
|
|
||||||
class SujetsConfig(AppConfig):
|
class SujetsConfig(AppConfig):
|
||||||
name = 'sujets'
|
name = 'sujets'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
Sujet = self.get_model("Sujet")
|
||||||
|
watson.register(Sujet, fields=('nom', 'prenom', 'surnom'))
|
||||||
|
|||||||
@@ -5,7 +5,28 @@
|
|||||||
<div class="panel-heading text-center">
|
<div class="panel-heading text-center">
|
||||||
<h3 class="panel-title">Sujets rencontrés</h3>
|
<h3 class="panel-title">Sujets rencontrés</h3>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="panel-body">
|
||||||
|
<form action="" method="POST" class="form form-group">{% csrf_token %}
|
||||||
|
<div class="input-group">
|
||||||
|
<span class="input-group-addon" id="basic-addon1">
|
||||||
|
<span class=" glyphicon glyphicon-search"></span>
|
||||||
|
</span>
|
||||||
|
<input type="text" name="q" class="form-control" placeholder="Chercher un sujet" aria-describedby="basic-addon1">
|
||||||
|
<span class="input-group-btn">
|
||||||
|
<button type="submit" class="btn btn-primary">Chercher</button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{% if query_text %}<div class="well well-sm text-center">
|
||||||
|
<p class="label label-warning">'{{query_text}}'</p>
|
||||||
|
<p class="label label-info">
|
||||||
|
{% if not object_list %}Aucun résultat
|
||||||
|
{% else %} {{ object_list.count }} résultat(s)
|
||||||
|
{% endif %}
|
||||||
|
</p>
|
||||||
|
</div>{% endif %}
|
||||||
|
</div>
|
||||||
|
{% if object_list %}
|
||||||
<!-- Table -->
|
<!-- Table -->
|
||||||
<table class="table table-striped table-hover">
|
<table class="table table-striped table-hover">
|
||||||
{% for sujet in object_list %}
|
{% for sujet in object_list %}
|
||||||
@@ -15,15 +36,15 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
{% if is_paginated %}
|
{% if is_paginated %}
|
||||||
<div class="panel-footer text-center">
|
<div class="panel-footer text-center">
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
{% for num in page_obj.paginator.page_range %}
|
{% for num in page_obj.paginator.page_range %}
|
||||||
<li {% if page_obj.number == num %} class="active" {%endif%}><a href="?page={{num}}">{{num}}</a></li>
|
<li {% if page_obj.number == num %} class="active" {%endif%}><a href="?page={{num}}">{{num}}</a></li>
|
||||||
{%endfor%}
|
{%endfor%}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-lg-3 col-md-3">
|
<div class="col-lg-3 col-md-3">
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render, redirect
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
|
||||||
from .models import Sujet
|
from .models import Sujet
|
||||||
@@ -38,7 +38,18 @@ class SujetListView(generic.ListView):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.insert_menu("suivi/menu_sujets.html")
|
self.insert_menu("suivi/menu_sujets.html")
|
||||||
|
def post(self, request, **kwargs):
|
||||||
|
from watson import search as watson
|
||||||
|
search_text = request.POST.get('q')
|
||||||
|
results = watson.filter(Sujet, search_text)
|
||||||
|
if results.count() == 1:
|
||||||
|
return redirect(results[0].get_absolute_url())
|
||||||
|
self.queryset = results
|
||||||
|
return self.get(request, **kwargs)
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super().get_context_data(**kwargs)
|
||||||
|
context['query_text'] = self.request.POST.get('q', None)
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
@webpage
|
@webpage
|
||||||
|
|||||||
Reference in New Issue
Block a user