added search on Sujet with django-watson
This commit is contained in:
@@ -2,3 +2,5 @@
|
||||
|
||||
* django-bootstrap3
|
||||
* 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
|
||||
<span class="pull-right">{% bootstrap_icon "list" %}</span></a>
|
||||
</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 %}
|
||||
<li class="dropdown app-menu">
|
||||
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
|
||||
|
||||
@@ -6,7 +6,6 @@ from sujets.models import Sujet
|
||||
from .forms import *
|
||||
from notes.mixins import NoteFormMixin
|
||||
from notes.forms import AutoNoteForm
|
||||
from .search import SearchFormMixin, SearchFormProcessView
|
||||
# Create your views here.
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
@@ -18,7 +17,7 @@ webpage = website.webpage(
|
||||
|
||||
|
||||
@webpage
|
||||
class IndexView(SearchFormMixin, NoteFormMixin, generic.TemplateView):
|
||||
class IndexView(NoteFormMixin, generic.TemplateView):
|
||||
class PageInfo:
|
||||
title = "Suivi des bénéficiaires"
|
||||
header = "Suivi"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
default_app_config = 'sujets.apps.SujetsConfig'
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
from watson import search as watson
|
||||
|
||||
class SujetsConfig(AppConfig):
|
||||
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">
|
||||
<h3 class="panel-title">Sujets rencontrés</h3>
|
||||
</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 class="table table-striped table-hover">
|
||||
{% for sujet in object_list %}
|
||||
@@ -15,15 +36,15 @@
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% if is_paginated %}
|
||||
<div class="panel-footer text-center">
|
||||
{% if is_paginated %}
|
||||
<div class="panel-footer text-center">
|
||||
<ul class="pagination">
|
||||
{% for num in page_obj.paginator.page_range %}
|
||||
<li {% if page_obj.number == num %} class="active" {%endif%}><a href="?page={{num}}">{{num}}</a></li>
|
||||
{%endfor%}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<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 .models import Sujet
|
||||
@@ -38,7 +38,18 @@ class SujetListView(generic.ListView):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user