src/p4l/search/views.py
author ymh <ymh.work@gmail.com>
Thu, 06 Nov 2014 02:07:07 +0100
changeset 175 1ff6533ba75d
parent 161 216b3f9582aa
permissions -rw-r--r--
upgrade documentation on deploiement
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
126
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     2
#
131
f1854630734f change copyright notice
ymh <ymh.work@gmail.com>
parents: 126
diff changeset
     3
# Copyright IRI (c) 2013
126
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     4
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     5
# contact@iri.centrepompidou.fr
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     6
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     7
# This software is governed by the CeCILL-B license under French law and
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     8
# abiding by the rules of distribution of free software.  You can  use, 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
     9
# modify and/ or redistribute the software under the terms of the CeCILL-B
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    10
# license as circulated by CEA, CNRS and INRIA at the following URL
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    11
# "http://www.cecill.info". 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    12
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    13
# As a counterpart to the access to the source code and  rights to copy,
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    14
# modify and redistribute granted by the license, users are provided only
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    15
# with a limited warranty  and the software's author,  the holder of the
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    16
# economic rights,  and the successive licensors  have only  limited
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    17
# liability. 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    18
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    19
# In this respect, the user's attention is drawn to the risks associated
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    20
# with loading,  using,  modifying and/or developing or reproducing the
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    21
# software by the user in light of its specific status of free software,
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    22
# that may mean  that it is complicated to manipulate,  and  that  also
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    23
# therefore means  that it is reserved for developers  and  experienced
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    24
# professionals having in-depth computer knowledge. Users are therefore
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    25
# encouraged to load and test the software's suitability as regards their
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    26
# requirements in conditions enabling the security of their systems and/or 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    27
# data to be ensured and,  more generally, to use and operate it in the 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    28
# same conditions as regards security. 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    29
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    30
# The fact that you are presently reading this means that you have had
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    31
# knowledge of the CeCILL-B license and that you accept its terms.
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    32
#
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
126
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 117
diff changeset
    34
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
from django.conf import settings
161
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    36
from django.core.paginator import InvalidPage
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    37
from django.http.response import Http404
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
from django.template.context import RequestContext
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
from haystack.query import SearchQuerySet
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
from haystack.views import SearchView, search_view_factory
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
from p4l.search.forms import RecordSearchForm
161
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    43
from p4l.utils import P4lPaginator
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
class RecordSearchView(SearchView):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    def __init__(self, template=None, load_all=True, form_class=None, searchqueryset=None, context_class=RequestContext, results_per_page=None):
146
3d20cdc9be3d Change notice list order on a search to the order returned by the indexation engine. Fix #5 (https://github.com/IRI-Research/plan4learning/issues/5)
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    49
        record_searchQuerySet = SearchQuerySet()
117
0a4e7d6ebe80 - Do not create object for new
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    50
        template = "p4l/home.html"
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        results_per_page= settings.NB_RECORDS_BY_PAGE
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        form_class = RecordSearchForm
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        SearchView.__init__(self, template=template, load_all=False, form_class=form_class, searchqueryset=record_searchQuerySet, context_class=context_class, results_per_page=results_per_page)
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    @classmethod
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    def as_view(cls):
146
3d20cdc9be3d Change notice list order on a search to the order returned by the indexation engine. Fix #5 (https://github.com/IRI-Research/plan4learning/issues/5)
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    57
        return search_view_factory(view_class=cls)
161
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    58
    
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    59
    def build_page(self):
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    60
        """
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    61
        Paginates the results appropriately.
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    62
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    63
        In case someone does not want to use Django's built-in pagination, it
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    64
        should be a simple matter to override this method to do what they would
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    65
        like.
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    66
        """
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    67
        try:
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    68
            page_no = int(self.request.GET.get('page', 1))
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    69
        except (TypeError, ValueError):
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    70
            raise Http404("Not a valid number for page.")
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    71
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    72
        if page_no < 1:
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    73
            raise Http404("Pages should be 1 or greater.")
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    74
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    75
        start_offset = (page_no - 1) * self.results_per_page
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    76
        self.results[start_offset:start_offset + self.results_per_page]
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    77
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    78
        paginator = P4lPaginator(self.results, self.results_per_page)
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    79
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    80
        try:
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    81
            page = paginator.page(page_no)
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    82
        except InvalidPage:
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    83
            raise Http404("No such page!")
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    84
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    85
        return (paginator, page)
216b3f9582aa improve paginator on list.
ymh <ymh.work@gmail.com>
parents: 146
diff changeset
    86