import os
import random

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from crispy_forms.bootstrap import StrictButton
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
from django.core.urlresolvers import reverse


class SearchForm(forms.Form):
    q = forms.CharField(
        label=_('Recherche'),
        max_length=150,
        required=False,
        widget=forms.TextInput(
            attrs={
                'type': 'search',
                'required': 'required'
            }
        )
    )

    choices = sorted(
        [(k, v[0]) for k, v in settings.ZDS_APP['search']['search_groups'].items()],
        key=lambda pair: pair[1]
    )

    models = forms.MultipleChoiceField(
        label='',
        widget=forms.CheckboxSelectMultiple,
        required=False,
        choices=choices
    )

    category = forms.CharField(widget=forms.HiddenInput, required=False)
    subcategory = forms.CharField(widget=forms.HiddenInput, required=False)
    from_library = forms.CharField(widget=forms.HiddenInput, required=False)

    def __init__(self, *args, **kwargs):

        super(SearchForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_id = 'search-form'
        self.helper.form_class = 'clearfix'
        self.helper.form_method = 'get'
        self.helper.form_action = reverse('search:query')

        try:
            with open(os.path.join(settings.BASE_DIR, 'suggestions.txt'), 'r') as suggestions_file:
                suggestions = ', '.join(random.sample(suggestions_file.readlines(), 5)) + '…'
        except OSError:
            suggestions = _('Mathématiques, Droit, UDK, Langues, Python…')

        self.fields['q'].widget.attrs['placeholder'] = suggestions

        self.helper.layout = Layout(
            Field('q'),
            StrictButton('', type='submit', css_class='ico-after ico-search', title=_('Rechercher')),
            Field('category'),
            Field('subcategory'),
            Field('from_library')
        )

zds/searchv2/forms.py
import os
import random

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from crispy_forms.bootstrap import StrictButton
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
from django.core.urlresolvers import reverse


class SearchForm(forms.Form):
    q = forms.CharField(
        label=_('Recherche'),
        max_length=150,
        required=False,
        widget=forms.TextInput(
            attrs={
                'type': 'search',
                'required': 'required'
            }
        )
    )

    choices = sorted(
        [(k, v[0]) for k, v in settings.ZDS_APP['search']['search_groups'].items()],
        key=lambda pair: pair[1]
    )

    models = forms.MultipleChoiceField(
        label='',
        widget=forms.CheckboxSelectMultiple(
            attrs={
                'class': 'search-filters',
                'form': 'search-form'
            }
        ),
        required=False,
        choices=choices
    )

    category = forms.CharField(widget=forms.HiddenInput, required=False)
    subcategory = forms.CharField(widget=forms.HiddenInput, required=False)
    from_library = forms.CharField(widget=forms.HiddenInput, required=False)

    def __init__(self, *args, **kwargs):

        super(SearchForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.form_id = 'search-form'
        self.helper.form_class = 'clearfix'
        self.helper.form_method = 'get'
        self.helper.form_action = reverse('search:query')

        try:
            with open(os.path.join(settings.BASE_DIR, 'suggestions.txt'), 'r') as suggestions_file:
                suggestions = ', '.join(random.sample(suggestions_file.readlines(), 5)) + '…'
        except OSError:
            suggestions = _('Mathématiques, Droit, UDK, Langues, Python…')

        self.fields['q'].widget.attrs['placeholder'] = suggestions

        self.helper.layout = Layout(
            Field('q'),
            StrictButton('', type='submit', css_class='ico-after ico-search', title=_('Rechercher')),
            Field('category'),
            Field('subcategory'),
            Field('from_library')
        )

zds/searchv2/forms.py
diff --git a/templates/searchv2/search.html b/templates/searchv2/search.html
index 62ed74fe6d..5618f244fe 100644
--- a/templates/searchv2/search.html
+++ b/templates/searchv2/search.html
@@ -27,16 +27,7 @@
                 {% for radio in form.models %}
                     <li>
 
-                        <input id="{{ radio.id_for_label }}"
-                               form="search-form"
-                               name="models"
-                               type="checkbox"
-                               value="{{ radio.choice_value }}"
-
-                               {% if radio.choice_value in form.cleaned_data.models %}
-                                   checked
-                               {% endif %}
-                                >
+                        {{ radio.tag }}
 
                         <label for="{{ radio.id_for_label }}">{{ radio.choice_label }}</label>
                     </li>
diff --git a/zds/searchv2/forms.py b/zds/searchv2/forms.py
index e3d010aece..52a6e4052d 100644
--- a/zds/searchv2/forms.py
+++ b/zds/searchv2/forms.py
@@ -31,7 +31,12 @@ class SearchForm(forms.Form):
 
     models = forms.MultipleChoiceField(
         label='',
-        widget=forms.CheckboxSelectMultiple,
+        widget=forms.CheckboxSelectMultiple(
+            attrs={
+                'class': 'search-filters',
+                'form': 'search-form'
+            }
+        ),
         required=False,
         choices=choices
     )

Erreur 403 après recherche dans la bibliothèque
> Bonjour,
> 
> Je tombe sur une erreur 403 "Vous n’avez pas les droits suffisants pour accéder à cette page." après une recherche dans les cours présents dans la bibliothèque.
Voilà comment elle est apparue :
> 
> - Arrivé sur le site déconnecté
> - Je suis allé dans "Développement web" depuis le bandeau en haut du site
> - A partir de là je me suis connecté à mon compte (même onglet, bouton classique), ce qui m'a ramené sur la page
> - Puis j'ai fait une recherche "PHP" dans la barre de rechercher, ce qui m'a amené sur [ce lien](https://zestedesavoir.com/rechercher/?q=PHP&models=content&from_library=on&category=informatique&subcategory=site-web)
> 
> L'erreur 403 se produit lorsque je coche 1 à 3 cases (sous la barre de recherche), pour filtrer les résultats, et que je clique à nouveau sur le bouton "rechercher" dans la barre.
> 
> Voilà [un exemple de lien](https://zestedesavoir.com/rechercher/?q=PHP&category=informatique&subcategory=site-web&from_library=on&models=) provoquant une erreur 403 de mon côté.
> 
> Bye

Sujet : https://zestedesavoir.com/forums/sujet/11609/erreur-403-apres-recherche-dans-la-bibliotheque/
*Envoyé depuis Zeste de Savoir*
